Home > OS >  Getting error in [System.IO.Compression.ZipFile]::CreateFromDirectory when using CompressionLevel of
Getting error in [System.IO.Compression.ZipFile]::CreateFromDirectory when using CompressionLevel of

Time:07-20

I want to compress a directory using PowerShell script. The following script works when I set the compression level to be Optimal or Fastest, but it is failing with SmallestSize.

$compressionLevel = [System.IO.Compression.CompressionLevel]::SmallestSize
[System.IO.Compression.ZipFile]::CreateFromDirectory("testfolder","D:\Dev.zip", $compressionLevel, $false)

Error:

Cannot find an overload for "CreateFromDirectory" and the argument count: "4".
At line:1 char:1
  [System.IO.Compression.ZipFile]::CreateFromDirectory("testfolder","D: ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [], MethodException
      FullyQualifiedErrorId : MethodCountCouldNotFindBest

The same script is working if I set compression level as Optimal/Fastest - $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal

CodePudding user response:

SmallestSize is not available and will evaluate as null if your PowerShell version is 5.1 or lower.

If you'd like to check what options you have available, you can run the following command:

[System.IO.Compression.CompressionLevel] | Get-Member -Static -MemberType Property

  • Related