Repository navigation
Add support for reading and writing zip files. #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
al45tair
wants to merge
4
commits into
swiftlang:main
Choose a base branch
from
al45tair:pr/zip-file-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
96d6ca5
Add support for reading and writing zip files
al45tair de79004
[Build] Fix build scripts.
al45tair 8f59c22
Make the Zip code have `package` scope instead of being `public`.
al45tair c055aee
We can't use `Span` in this repo. Add shims.
al45tair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # | ||
| # This source file is part of the Swift.org open source project | ||
| # | ||
| # Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| # Licensed under Apache License v2.0 with Runtime Library Exception | ||
| # | ||
| # See https://swift.org/LICENSE.txt for license information | ||
| # See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| # | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Fetches a pinned zlib release and builds it as a static library on Windows. | ||
|
|
||
| .DESCRIPTION | ||
| Downloads the source tarball for a specific zlib release then configures | ||
| and builds with CMake (Visual Studio generator by default, or whatever | ||
| -Generator specifies). Only the static target is built. Output goes to | ||
| <OutDir>\include and <OutDir>\lib. | ||
|
|
||
| .PARAMETER WorkDir | ||
| Scratch directory for the download, extracted source and build tree. | ||
|
|
||
| .PARAMETER OutDir | ||
| Where the headers and zlib static library are installed. | ||
|
|
||
| .PARAMETER Config | ||
| Release or Debug. | ||
|
|
||
| .PARAMETER Arch | ||
| Target architecture for the Visual Studio generator: x64, Win32 or ARM64. | ||
|
|
||
| .PARAMETER StaticRuntime | ||
| Link the static MSVC runtime (/MT) instead of the DLL runtime (/MD). | ||
|
|
||
| .PARAMETER Generator | ||
| CMake generator. Defaults to the newest Visual Studio found by CMake. | ||
|
|
||
| .PARAMETER Version | ||
| The zlib release to build, without the leading 'v'. Defaults to 1.3.2. | ||
|
|
||
| .EXAMPLE | ||
| .\buildzlib.ps1 -OutDir C:\libs\zlib -StaticRuntime | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [string]$WorkDir = (Join-Path $PWD 'zlib-build'), | ||
| [string]$OutDir = (Join-Path $PWD 'zlib-install'), | ||
| [ValidateSet('Release', 'Debug')][string]$Config = 'Release', | ||
| [ValidateSet('x64', 'Win32', 'ARM64')][string]$Arch = 'x64', | ||
| [switch]$StaticRuntime, | ||
| [string]$Generator, | ||
| [string]$Version = '1.3.2' | ||
| ) | ||
|
|
||
| $ErrorActionPreference = 'Stop' | ||
| $ProgressPreference = 'SilentlyContinue' # Invoke-WebRequest is very slow otherwise | ||
| [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 | ||
|
|
||
| function Invoke-Native { | ||
| & $args[0] $args[1..($args.Length - 1)] | ||
| if ($LASTEXITCODE -ne 0) { throw "'$($args -join ' ')' failed with exit code $LASTEXITCODE" } | ||
| } | ||
|
|
||
| foreach ($tool in 'cmake', 'tar') { | ||
| if (-not (Get-Command $tool -ErrorAction SilentlyContinue)) { | ||
| throw "Required tool '$tool' not found on PATH. Install CMake and run from a VS Developer prompt if needed." | ||
| } | ||
| } | ||
|
|
||
| # 1. Choose the version | ||
| $zlibVersion = $Version.TrimStart('v') | ||
| Write-Host "zlib version: $zlibVersion" | ||
|
|
||
| # 2. Download and extract into fresh directories | ||
| $downloadDir = Join-Path $WorkDir "download-$zlibVersion" | ||
| $srcRoot = Join-Path $WorkDir "src-$zlibVersion" | ||
| $buildDir = Join-Path $WorkDir "build-$zlibVersion-$Arch" | ||
| foreach ($d in $downloadDir, $srcRoot, $buildDir) { | ||
| if (Test-Path $d) { Remove-Item $d -Recurse -Force } | ||
| New-Item -ItemType Directory -Path $d | Out-Null | ||
| } | ||
|
|
||
| $url = "https://github.com/madler/zlib/releases/download/v$zlibVersion/zlib-$zlibVersion.tar.gz" | ||
| $archive = Join-Path $downloadDir "zlib-$zlibVersion.tar.gz" | ||
| Write-Host "Downloading $url" | ||
| Invoke-WebRequest $url -OutFile $archive | ||
|
|
||
| # Paths are passed as arguments; nothing is run from inside the extracted tree. | ||
| Invoke-Native tar -xzf $archive -C $srcRoot | ||
| $srcDir = (Get-ChildItem $srcRoot -Directory | Select-Object -First 1).FullName | ||
| if (-not $srcDir) { throw 'Archive did not extract to a directory.' } | ||
|
|
||
| # 3. Configure | ||
| $cmakeArgs = @('-S', $srcDir, '-B', $buildDir) | ||
| if ($Generator) { $cmakeArgs += @('-G', $Generator) } | ||
| if (-not $Generator -or $Generator -like 'Visual Studio*') { $cmakeArgs += @('-A', $Arch) } | ||
| $cmakeArgs += '-DZLIB_BUILD_SHARED=OFF' # ignored by older releases; we only build zlibstatic anyway | ||
| if ($StaticRuntime) { | ||
| $cmakeArgs += '-DCMAKE_POLICY_DEFAULT_CMP0091=NEW' | ||
| $cmakeArgs += '-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>' | ||
| } | ||
| Invoke-Native cmake @cmakeArgs | ||
|
|
||
| # 4. Build only the static library | ||
| Invoke-Native cmake --build $buildDir --config $Config --target zlibstatic --parallel | ||
|
|
||
| # 5. Install headers and library | ||
| $incOut = Join-Path $OutDir 'include' | ||
| $libOut = Join-Path $OutDir 'lib' | ||
| New-Item -ItemType Directory -Force -Path $incOut, $libOut | Out-Null | ||
|
|
||
| # zconf.h is generated into the build tree; the source copy is renamed zconf.h.included | ||
| Copy-Item (Join-Path $srcDir 'zlib.h') $incOut -Force | ||
| $zconf = Join-Path $buildDir 'zconf.h' | ||
| if (-not (Test-Path $zconf)) { $zconf = Join-Path $srcDir 'zconf.h' } | ||
| Copy-Item $zconf $incOut -Force | ||
|
|
||
| # Multi-config generators put the lib under <build>\<Config>; single-config in <build>. | ||
| # Pre-1.3.2, the output is named zlibstatic or, for debug, zlibstaticd. | ||
| # As of zlib-1.3.2, the output is named zs or, for debug, zsd. | ||
| # Normalize to zlib[d] | ||
| $built = Get-ChildItem $buildDir -Recurse -Filter 'z*.lib' | | ||
| Where-Object { $_.Directory.Name -in $Config, (Split-Path $buildDir -Leaf) } | | ||
| Select-Object -First 1 | ||
| if (-not $built) { $built = Get-ChildItem $buildDir -Recurse -Filter 'z*.lib' | Select-Object -First 1 } | ||
| if (-not $built) { throw 'Could not find the built static library.' } | ||
|
|
||
| $destName = $built.Name -replace '^(zlibstatic|zs)', 'zlib' | ||
| Copy-Item $built.FullName (Join-Path $libOut $destName) -Force | ||
|
|
||
| # Write a pkgconfig file | ||
| $pcName = Join-Path $OutDir 'zlib.pc' | ||
|
|
||
| # pkg-config treats backslash as an escape character, so use forward slashes | ||
| $pcLib = $libOut -replace '\\', '/' | ||
| $pcInc = $incOut -replace '\\', '/' | ||
| # Quote paths containing whitespace, so they stay a single flag argument | ||
| if ($pcLib -match '\s') { $pcLib = "`"$pcLib`"" } | ||
| if ($pcInc -match '\s') { $pcInc = "`"$pcInc`"" } | ||
| $pcLibName = [System.IO.Path]::GetFileNameWithoutExtension($destName) | ||
|
|
||
| @" | ||
| Name: zlib | ||
| Version: $zlibVersion | ||
| Description: zlib compression library | ||
| Libs: -L$pcLib -l$pcLibName | ||
| Cflags: -I$pcInc | ||
| "@ | Out-File $pcName -Encoding ascii | ||
|
|
||
| Write-Host '' | ||
| Write-Host "zlib $zlibVersion ($Config, $Arch) built successfully." | ||
| Write-Host " Headers: $incOut" | ||
| Write-Host " Library: $(Join-Path $libOut $destName)" | ||
| Write-Host " pkgconfig: $pcName" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "indentation": { | ||
| "spaces": 4 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2026 Apple Inc. and the Swift project authors | ||
| Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
|
||
| See https://swift.org/LICENSE.txt for license information | ||
| See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| */ | ||
|
|
||
| #include <zlib.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module CZLib { | ||
| header "CZLib.h" | ||
| export * | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather that we don't commit any formatting configuration files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drat. I hadn't intended to add that (again). I should add it to
.gitignoresor similar, maybe.