Last active
August 27, 2023 07:08
-
-
Save santisq/92a7684feff9f22d8a41130d5dfee922 to your computer and use it in GitHub Desktop.
split and merge file into zip chunks
This file contains 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
$destination = 'path\to\destination' | |
$inFS = [System.IO.File]::OpenWrite($destination) | |
Get-ChildItem -Filter temp* | ForEach-Object { | |
$stream = $_.OpenRead() | |
$zip = [System.IO.Compression.ZipArchive]::new($stream, [System.IO.Compression.ZipArchiveMode]::Read) | |
$entry = $zip.GetEntry($_.Name) | |
$zipFS = $entry.Open() | |
$zipFS.CopyTo($inFS) | |
$zipFS, $zip, $stream | ForEach-Object Dispose | |
} | |
$inFS.Dispose() |
This file contains 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
$inFile = Get-Item path\to\filetosplit | |
$inFS = $inFile.OpenRead() | |
$buffer = [byte[]]::new(200mb) | |
$chunk = 0 | |
while($readBytes = $inFS.Read($buffer, 0, $buffer.Length)) { | |
$name = '{0}-part {1:D2}.{2}' -f $inFile.BaseName, $chunk, $inFile.Extension | |
$file = Join-Path $pwd.Path -ChildPath $name | |
$outFS = [System.IO.File]::OpenWrite($file + $inFile.Extension) | |
$zip = [System.IO.Compression.ZipArchive]::new($outFS, [System.IO.Compression.ZipArchiveMode]::Create) | |
$entry = $zip.CreateEntry($name + $inFile.Extension) | |
$zipFS = $entry.Open() | |
$zipFS.Write($buffer, 0, $readBytes) | |
$chunk++ | |
$zipFS, $zip, $outFS | ForEach-Object Dispose | |
} | |
$inFS.Dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment