Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active August 27, 2023 07:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santisq/92a7684feff9f22d8a41130d5dfee922 to your computer and use it in GitHub Desktop.
Save santisq/92a7684feff9f22d8a41130d5dfee922 to your computer and use it in GitHub Desktop.
split and merge file into zip chunks
$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()
$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