Home > Back-end >  [IO.Compression.ZipFile]::CreateFromDirectory() include directory in archive
[IO.Compression.ZipFile]::CreateFromDirectory() include directory in archive

Time:10-25

I need to compress and extract some zip files with a rather large source, often 30GB or more, so I can't use the native PowerShell cmdlets. I am looking at using [IO.Compression.ZipFile] static methods, and liking the fact that it works with big source folders, and works faster in general. But I am running into one issue. I want to include the source folder IN the archive, like Compress-Archive does

$sourcePath = '\\px\Rollouts\ADSK\2023\Revit_2023\Deployment\Revit_2023'
Compress-Archive -Path:$sourcePath -DestinationPath:$zipPath 

the Revit_2023 folder is part of the archive, while with

[IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $zipPath)

only the CONTENTS of the Revit_2023 folder is archived. When extracting I could create a folder with the name of the Zip file as the folder name and extract to that, but I can imagine a scenario where I really want the Zip file named differently, perhaps with a date or something. But in looking at the docs here it looks like this behavior is the only option for a simple implementation, and doing what I want to do is going to require managing entries myself. And even that I am not sure about, since it looks like entries are files only? Am I misunderstanding here, and there is an option to include the source folder? I sure would prefer to have a simple solution AND the source folder name in the archive.

CodePudding user response:

Overloads of the method are:

static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName)
static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory)
static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Text.Encoding entryNameEncoding)

So the command would be:

[IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $zipPath, [System.IO.Compression.CompressionLevel]::Optimal, $true)

You could pick different CompressionLevel, but you can't skip it when also using includeBaseDirectory flag.

  • Related