#PSTip Compress files and folders with System.IO.Compression.FileSystem class

Using the CreateFromDirectory and ExtractToDirectory methods, it is possible to compress and extract files. In this tip I will show different constructors that can be used to either compress or extract files using this class. The following example will compress the files stored in the c:\testing folder:

1
2
Add-Type -Assembly 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory('c:\testing', 'c:\testing.zip','Optimal',$false)

When you want to extract files, use the ExtractToDirectory method:

1
[System.IO.Compression.ZipFile]::ExtractToDirectory('c:\testing.zip', 'c:\newtest')

In PowerShell 5.0 the Compress-Archive and Expand-Archive cmdlets are available to simplify working with compressed archives.

For more information about the ZipFile class and the Compress-Archive and Expand-Archive cmdlets please refer to the following articles:

ZipFile Class

Compress-Archive

Expand-Archive

Share on: