WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

How to use powershell to quickly create an archive of specific files and directories and maintain strucuture

I'm using Compress-Archive as follows to create a zip file that contains 3 files and 2 directories (and all of their sub-directories)

Compress-Archive -CompressionLevel Fastest -Force -DestinationPath ./My.zip -Path .\foo.ini, 
                    .\bar.exe, 
                    .\README.TXT,
                    .\dir1, 
                    .\dir2

Unfortunately, it is extremely slow. I'd like to use 7-Zip (which is faster) to create a zip file. I've been trying to use the Powershell add-on Compress-7Zip to do the compression instead. Also unfortunately, I can't figure out how to use Compress-7Zip to take just the specified files. I thought if I could populate a variable with all of the files I could pipe into Compress-7Zip.

$stuff = ???        .\foo.ini, 
                    .\bar.exe, 
                    .\README.TXT,
                    .\dir1, 
                    .\dir2

#$stuff now contains 3 txt files in the root and the complete contents
#dir\ and \dir2 with their paths

$stuff | Compress-7Zip -Format Zip -ArchiveFileName .\my.zip

How can I use Get-Child-Item (or something else) to get the directory structure into $stuff?

Or if you have another suggestion for creating this zip using a better compression method than what Compress-Archive, I'm all ears.

Upvotes: 0

Views: 704

Answers (1)

Adam
Adam

Reputation: 4178

A couple of things...

It looks like (in the last snippet with Compress-7Zip) you're trying to (splat)[https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6]:

enter image description here

That might not help you here, and I apologize, I can't find the documentation for Compress-Archive to confirm. It sort of looks like it's taking an array of strings. So, if you stuff all those files in a string array...

$files_to_compress = @(
  '.\foo.ini'
  '.\bar.exe' 
  '.\README.TXT'
  '.\dir1'
  '.\dir2'
)

You can probably do the following...

Compress-Archive -CompressionLevel Fastest `
-Force -DestinationPath ./My.zip `
-Path $files_to_compress 

Hopefully that example makes sense.

Upvotes: 0

Related Questions