Prerequisites
Steps to reproduce
Sometimes, Compress-Archive creates a weird directory-like entry in the produced archive. The details I was able to find:
- only in 2nd-level nested directory
- if said directory contains no files (i.e. is empty or only nested directories)
Compress-Archive will create a ZIP entry called <yourDirectoryName>/ not marked as directory according to the output of zipinfo
So, exact steps to reproduce:
$ md playground/pt/fspt/d
$ echo xxx >> playground/pt/fspt/d/file.txt
$ Compress-Archive playground/* -DestinationPath 'file.zip' -Force
Now, examine the resulting archive using zipinfo (I wasn't able to quickly find a zipinfo distribution for Windows, so installed sudo apt install unzip on my Ubuntu into WSL):
$ zipinfo file.zip
Archive: file.zip
Zip file size: 233 bytes, number of entries: 2
-rw---- 2.0 fat 0 b- stor 22-Dec-07 20:56 pt/fspt/
-rw---- 2.0 fat 5 b- defN 22-Dec-07 20:56 pt/fspt/d/file.txt
2 files, 5 bytes uncompressed, 7 bytes compressed: -40.0%
The weird path is the first entry:
-rw---- 2.0 fat 0 b- stor 22-Dec-07 20:56 pt/fspt/
Notably, the weird entry goes away if I create a file in directory pt/fspt:
$ echo 123 >> playground/pt/fspt/111.txt
$ Compress-Archive playground/* -DestinationPath 'file.zip' -force && wsl -d Ubuntu zipinfo file.zip
Archive: file.zip
Zip file size: 254 bytes, number of entries: 2
-rw---- 2.0 fat 5 b- defN 22-Dec-07 21:15 pt/fspt/111.txt
-rw---- 2.0 fat 5 b- defN 22-Dec-07 20:56 pt/fspt/d/file.txt
2 files, 10 bytes uncompressed, 14 bytes compressed: -40.0%
I've seen two kinds of other archives:
- The ones that contain no directory entries at all.
- The ones that contain directory entries with
drwxr-xr-x flags instead of -rw----.
At the same time, [System.IO.Compression.ZipFile]::CreateFromDirectory works well:
$ [System.IO.Compression.ZipFile]::CreateFromDirectory($(Resolve-Path playground), "$(Resolve-Path '.')/file2.zip")
$ zipinfo file2.zip
Archive: file2.zip
Zip file size: 141 bytes, number of entries: 1
-rw---- 2.0 fat 5 b- defN 22-Dec-07 20:56 pt/fspt/d/file.txt
1 file, 5 bytes uncompressed, 7 bytes compressed: -40.0%
So, System.IO.Compression.ZipFile]::CreateFromDirectory doesn't have this problem.
For empty directory, it will still create an entry with -rw---- flags, though, so it is not without problems.
Expected behavior
I'd say that Compress-Archive should work the same way as System.IO.Compression.ZipFile]::CreateFromDirectory.
Actual behavior
Compress-Archive behaves differently from System.IO.Compression.ZipFile]::CreateFromDirectory, and in a very strange way that is.
Environment data
The issue reproduces in both Windows PowerShell 5.1 and PowerShell 7.3.
# pwsh 7.3
Name Value
---- -----
PSVersion 7.3.0
PSEdition Core
GitCommitId 7.3.0
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
# powershell 5
Name Value
---- -----
PSVersion 5.1.22000.832
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22000.832
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Prerequisites
Steps to reproduce
Sometimes,
Compress-Archivecreates a weird directory-like entry in the produced archive. The details I was able to find:Compress-Archivewill create a ZIP entry called<yourDirectoryName>/not marked as directory according to the output ofzipinfoSo, exact steps to reproduce:
Now, examine the resulting archive using
zipinfo(I wasn't able to quickly find azipinfodistribution for Windows, so installedsudo apt install unzipon my Ubuntu into WSL):The weird path is the first entry:
Notably, the weird entry goes away if I create a file in directory
pt/fspt:I've seen two kinds of other archives:
drwxr-xr-xflags instead of-rw----.At the same time,
[System.IO.Compression.ZipFile]::CreateFromDirectoryworks well:$ [System.IO.Compression.ZipFile]::CreateFromDirectory($(Resolve-Path playground), "$(Resolve-Path '.')/file2.zip")So,
System.IO.Compression.ZipFile]::CreateFromDirectorydoesn't have this problem.For empty directory, it will still create an entry with
-rw----flags, though, so it is not without problems.Expected behavior
I'd say that
Compress-Archiveshould work the same way asSystem.IO.Compression.ZipFile]::CreateFromDirectory.Actual behavior
Compress-Archivebehaves differently fromSystem.IO.Compression.ZipFile]::CreateFromDirectory, and in a very strange way that is.Environment data
The issue reproduces in both Windows PowerShell 5.1 and PowerShell 7.3.