Forum Discussion

Sixdog's avatar
Sixdog
Copper Contributor
Aug 08, 2023

Macos compress can't zip a folder.but can zip a file

 

 

$compress = @{
    Path = "./20230801"
    CompressionLevel = "Fastest"
    DestinationPath = "./Draft.zip"
  }
  Compress-Archive @compress

 

 

  • Sixdog 

     

    This is a normal "file not found" error, meaning it cannot see the directory you specified.

     

    Be careful when using file paths in .NET as relative paths are not always supported and even when they are, do not always yield expected results.

     

    Try something like this instead (to use the full path to the directory):

     

    [System.IO.Compression.ZipFile]::CreateFromDirectory(
        (Get-Item -Path ".\NewFolder").FullName
        , (Join-Path -Path ((Get-Item -Path .).FullName) -ChildPath "forum.zip")
        , [System.IO.Compression.CompressionLevel]::Fastest
        , $true
        );

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Sixdog 

     

    Compress-Archive has a couple of deviations from the underlying .NET class, one of which (in relation to missing hidden files) is mentioned in the documentation:

     

     

    If you use the underlying .NET class, you get what you expect:

     

    [System.IO.Compression.ZipFile]::CreateFromDirectory("D:\Data\Temp\Forum\NewFolder", "D:\Data\Temp\Forum\forum.zip", [System.IO.Compression.CompressionLevel]::Fastest, $true);

     

    Cheers,

    Lain

    • Sixdog's avatar
      Sixdog
      Copper Contributor
      thank u help very much, but i tried it,it doesn't work,this is the exception
      PS /Users/sixdog/Documents/PowerShell> [System.IO.Compression.ZipFile]::CreateFromDirectory("./20230701/", "./dddd.zip", [System.IO.Compression.CompressionLevel]::Fastest, $true);
      MethodInvocationException: Exception calling "CreateFromDirectory" with "4" argument(s): "Unable to find the specified file."
      PS /Users/sixdog/Documents/PowerShell>
      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        Sixdog 

         

        This is a normal "file not found" error, meaning it cannot see the directory you specified.

         

        Be careful when using file paths in .NET as relative paths are not always supported and even when they are, do not always yield expected results.

         

        Try something like this instead (to use the full path to the directory):

         

        [System.IO.Compression.ZipFile]::CreateFromDirectory(
            (Get-Item -Path ".\NewFolder").FullName
            , (Join-Path -Path ((Get-Item -Path .).FullName) -ChildPath "forum.zip")
            , [System.IO.Compression.CompressionLevel]::Fastest
            , $true
            );

         

        Cheers,

        Lain

Resources