7-Zip for Powershell
At work we deal with different big databases, and by big I mean between 3.5 and 8 GB. Those databases are zipped with 7-Zip and stored on a server. Depending on the scenario we unzip one of these databases to a local folder and attach it to the SQL Server. To simplify these steps we have a couple of Powershell scripts, among other things invoking the command line version of 7-Zip.
However, extracting an archive of several gigabytes over the network might take some time, and we’d like to see the progress in the Powershell console. Powershell provides a standard way to report progress by calling Cmdlet.WriteProgress
, which then will be displayed by the current host (e.g. command line) appropriately.
Therefore with some support of a co-worker I’ve written two Cmdlets for extracting and compressing archives. The syntax is simple as this:
Expand-7Zip
[-ArchiveFileName] <string>
[-TargetPath] <string>
[<CommonParameters>]
Compress-7Zip
[-ArchiveFileName] <string>
[-Path] <string>
[[-Filter] <string>]
[-Format <OutArchiveFormat> {SevenZip | Zip | GZip | BZip2 | Tar | XZ}]
[-CompressionLevel <CompressionLevel> {None | Fast | Low | Normal | High | Ultra}]
[-CompressionMethod <CompressionMethod> {Copy | Deflate | Deflate64 | BZip2 | Lzma | Lzma2 | Ppmd | Default}]
[<CommonParameters>]
It works with both x86 and x64 and uses SevenZipSharp as a wrapper around 7zip’s API.
You can download the cmdlets with the required libraries here or grap the sources at Github.
Comments
rZr
Nice, thanks!
Dave
Hi Thomas,
Thanks for great work. I have downloaded the package and started using. It’s really great.
Can you please help me how can I modify your package ?
Actually I want to exclude few folders and file extensions, but filter parameter just include those files/folder which are filtered.
May be your work required that, but is there any existing setting to exclude folders and files?
Thanks,
Dave
Thomas Freudenberg
Hi Dave,
I didn’t need the exclusion of files and folders, so I didn’t implement it. Feel free to modify my code, you can find it on Github. How about using the pipeline featire of PowerShell? I would accept a pull request.
Best regards
Thomas
Mathieu
Nice.
Import-Module -Name “7Zip”
Works when all the libraries from 7Zip4PowerShell.zip are placed in the modules directory, and create a module manifest data file (e.g. c:\Windows\System32\WindowsPowerShell\v1.0\Modules\7Zip\7Zip.psd1):
Harry
Nice! is there a way to handle unzipping password protected files?
Zack Price
Before extracting the zip contents, I needed to right click -> properties -> unblock.
Otherwise, the module wouldn’t install because it was blocked.
Aaron GrandeCorazón Clark
Did he ever implement this feature? I need it for a script at work and I am willing to put in the effort if he hasn’t already.
Thomas Freudenberg
Hi Aaron, no, I never got a PR for that. Feel free to contribute :)
Thomas Freudenberg
Hi Mathieu, I published 7Zip4Powershell as a module in the PowerShell Gallery: https://www.powershellgallery.com/packages/7Zip4Powershell
sotropino
Great job.
Even greater if it had two additional cmdlets to mimic the useful “l” and “t” switches available in 7z.exe:
Any .NET-savvy challenged to help?
Chappy167
Absolutely awesome Thomas, worked a treat.
pmedina 61920
Great job. thanks.
How can I exclude a file with the -Filter parameter?
Leave a Comment
Your email address will not be published. Required fields are marked *