Zip Large Files With PowerShell
This is a quick note for anybody looking for a solution to zip large files over 2G. In PowerShell, you can typically use the Expand-Archive
or Compress-Archive
to zip files. This method typically works except when the file or files you need to zip is zip happens to be larger than 2 Gigs. To get around this problem, the dotnet library System.IO.Compression
needs to be used.
ZipFile.Open Method (System.IO.Compression) | Microsoft Learn
Compress a Zip package
1
2
3
4
5
6
7
8
9
Add-Type -Assembly System.IO.Compression.FileSystem
$ZIPFileName = "c:\ZipTarget\master.zip"
$folderName = "C:\ExpandLocation\largeFile.bin"
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
$zip = [System.IO.Compression.ZipFile]::Open($ZIPFileName,'Create')
$fileName = [System.IO.Path]::GetFileName($FolderName)
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$folderName,$fileName,'Optimal') | Out-Null
$zip.Dispose()
Expand a zip package
1
2
3
4
5
6
Add-Type -Assembly System.IO.Compression.FileSystem
$ZIPFileName = "c:\ZipTarget\master.zip"
$FolderName = "c:\ExpandLocation\"
$zip = [System.IO.Compression.ZipFile]::Open($ZIPFileName,'Read')
[Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($zip,$FolderName) | Out-Null
In this article it describes diffent modes that can be run while defalting a zip package.
ZipArchiveMode Enum (System.IO.Compression) | Microsoft Learn
Multiple Mode are available when calling the library.
- Create Create used to generate the zip package and does not have a 2G limit.
- Read Read will allow the extraction process to bypass the 2G buffer size and write the stream to disk.
- Update Update has a limited buffer size of 2G this option should not be utilized.
This post is licensed under CC BY 4.0 by the author.