How to create archive with PowerShell?

The PowerShell 5.0 has features to support ZIP archives – Expand-Archive and Compress-Archive. They are very handy but what about performance?

In my previous post I measured performance of extracting ZIP archive, in this post I will focus on a ZIP archive creation. Again I will compare two ways of creation a ZIP archive – provided by PowerShell and provided by .NET Framework.

The first method is using native PowerShell command Compress-Archive; the second method is using CreateFromDirectory method from System.IO.Compression.Filesystem. The PowerShell code to compress a folder is shown below:

$measureCreate = Measure-Command { 
        Add-Type -assembly System.IO.Compression.Filesystem
        [io.compression.zipfile]::CreateFromDirectory("$folderToArchive", "$archivePath\Create$i.zip", [System.IO.Compression.CompressionLevel]::Optimal, $true )
    }

$measureCompress = Measure-Command {
        $ProgressPreference=’SilentlyContinue’
        Compress-Archive $folderToArchive "$archivePath\Compress$i.zip" -CompressionLevel Optimal
    }

First I compared a size of archives created by these two methods, and size is quite equal so I guess that compression level was the same in both cases.

Shows an archive size comparison for two methods.

The results are the following for around 313MB folder with 25000 of files.

Folder used as a source for tests
  • First column – ZIP archive and source folder are on the same disk
  • Second column – ZIP archive and source folder are on different disks
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:16
Compress-Archive     0:52
---
CreateFromDirectory 0:16
Compress-Archive     0:52
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:16
Compress-Archive     0:53
---
CreateFromDirectory 0:16
Compress-Archive     0:53
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:17
Compress-Archive     0:52
---
CreateFromDirectory 0:17
Compress-Archive     0:51
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:17
Compress-Archive     0:53
---
CreateFromDirectory 0:16
Compress-Archive     0:52
---
CreateFromDirectory 0:17
Compress-Archive     0:53
---
CreateFromDirectory 0:16
Compress-Archive     0:51
---
CreateFromDirectory 0:17
Compress-Archive     0:53
---
CreateFromDirectory 0:17
Compress-Archive     0:51
---
CreateFromDirectory 0:17
Compress-Archive     0:53
---
CreateFromDirectory 0:17
Compress-Archive     0:54
---
CreateFromDirectory 0:17
Compress-Archive     0:52

Conclusions

  • CreateFromDirectory is much faster then Compress-Archive
  • There is no difference when source folder for compression and output archive file are on the same disk drive. As we remember from previous post this is important for expanding archive to use two different disks for source and destination.

How can I reproduce your test?

Here is script used by me for performance tests.

$archivePath = "E:\Test"
$folderToArchive = "E:\Temp\FolderToCompress"
# prepare output folder
mkdir $archivePath Force
Clear-Host
for( $i = 0; $i -lt 10; $i++ )
{
$measureExtract = Measure-Command {
Add-Type assembly System.IO.Compression.Filesystem
[io.compression.zipfile]::CreateFromDirectory("$folderToArchive", "$archivePath\Create$i.zip", [System.IO.Compression.CompressionLevel]::Optimal, $true )
}
$measureExpand = Measure-Command {
$ProgressPreference=’SilentlyContinue’
Compress-Archive $folderToArchive "$archivePath\Compress$i.zip" CompressionLevel Optimal
}
Write-Host ""
Write-Host "CreateFromDirectory $($measureExtract.Minutes):$($measureExtract.Seconds)"
Write-Host "Compress-Archive $($measureExpand.Minutes):$($measureExpand.Seconds)"
}
# clean output folder
rmdir $archivePath Force Recurse
PowerShell code

1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: