Nov 16 2023 05:07 PM
I'm running Powershell version 5.1.17763.4974 on a server with Windows Server 2019 Datacenter v10.
Been using PS for years but trying Compress-Archive for the first time.
Tried building a script which didn't work. When executing the script, it immediately goes right back to the PS prompt and nothing happens.
Trying to execute a simple command directly with something like this:
Compress-Archive -Path 'D:\abc\*.*' -DestinationPath 'D:\xyz\abc.zip'
Same thing happens. Goes immediately back to the PS prompt.
I'm running PS as administrator.
What am I missing?
Nov 16 2023 05:57 PM
Hi, John.
Have a look at examples four and five, as you've specified the format (i.e. *.*) that explicitly only adds files from d:\abc. Subfolders and files from those subfolders will not be added.
If there's files in d:\abc then something would appear to be wrong. If there's no files but there are subfolders then what you're observing is expected behaviour, and you should change *.* simply to *, as per example four.
Also note that Compress-Archive has limitations - when compared to the underlying .NET class upon which it's based, which I spoke to in another thread:
Cheers,
Lain
Nov 17 2023 01:00 PM
Thank you @LainRobertson
I had read through that article and completely missed the obvious answer:
This: Compress-Archive -Path 'D:\abc\' -DestinationPath 'D:\xyz\abc.zip'
Instead of this: Compress-Archive -Path 'D:\abc\*.*' -DestinationPath 'D:\xyz\abc.zip'
Is working perfectly.
In fact, the folder D:\abc is indeed empty, but there are multiple folders on the next level.
If I may, I have two more questions.
I believe I read somewhere that using this compression method is limited to 2gb file sizes?
Is there a way to remove files from a compressed file leaving everything else in tact?
Thanks again for your quick answer and that excellent article!
Nov 17 2023 03:46 PM
Hey, John.
Yes, you certainly can manipulate an archive, however, you have to do so from .NET. Here's a very basic example, which you can easily extend as it's a pretty simple set of classes to work with.
# Add the required .NET library reference.
Add-Type -AssemblyName "System.IO.Compression.FileSystem";
# Open the Zip file.
$Archive = [System.IO.Compression.ZipFile]::Open("<full path to your Zip file>", [System.IO.Compression.ZipArchiveMode]::Update);
# Delete the first entry.
$Archive.Entries[0].Delete();
# Finish up properly by disposing the $Archive object.
$Archive.Dispose();
In a real-world context, you'd most likely enumerate the Entries collection and make smarter decisions based on file size, type, etc.
Cheers,
Lain
Nov 17 2023 04:26 PM
Thanks again for all you help @LainRobertson
Hope you have a wonderful weekend!