Forum Discussion
Macos compress can't zip a folder.but can zip a file
- Aug 09, 2023
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
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
- SixdogAug 09, 2023Copper Contributorthank 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>- LainRobertsonAug 09, 2023Silver Contributor
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
- SixdogAug 09, 2023Copper Contributorthank you very much,from your response i studied the powershell's knowledge.