使用Compress-Archive压缩文件夹时,它会跳过所有隐藏文件。
文档页面告诉我们,此命令使用Microsoft .NET Framework API System.IO.Compression.ZipArchive在后台运行。
有没有办法强制它归档隐藏文件?我找不到任何记录这个问题的地方。我试过-Force
,但没用。
我的当前解决方法是使用Set-FileAttribute
去掉隐藏属性后再压缩。
使用Compress-Archive压缩文件夹时,它会跳过所有隐藏文件。
文档页面告诉我们,此命令使用Microsoft .NET Framework API System.IO.Compression.ZipArchive在后台运行。
有没有办法强制它归档隐藏文件?我找不到任何记录这个问题的地方。我试过-Force
,但没用。
我的当前解决方法是使用Set-FileAttribute
去掉隐藏属性后再压缩。
这似乎是Compress-Archive
命令中的一个错误/疏忽。由于该命令不提供“包括隐藏文件”的参数,但接受通过-Path
或-LiteralPath
参数指定的源文件集合,我期望要么...
Compress-Archive -Path (
Get-ChildItem -Path '...' -Force `
| Select-Object -ExpandProperty 'FullName' `
) -DestinationPath '...'
...or this...
Get-ChildItem -Path '...' -Force | Compress-Archive -DestinationPath '...'
...作为将隐藏文件传递给cmdlet的一种方法;关键是为Get-ChildItem
指定-Force
参数。 然而,这两个调用都会引发以下错误...
Get-Item : Could not find item ....
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:814 char:63
+ ... Entry.LastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (...:String) [Get-Item], IOException
+ FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Exception setting "LastWriteTime": "Cannot convert null to type "System.DateTimeOffset"."
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:814 char:25
+ ... $currentArchiveEntry.LastWriteTime = (Get-Item -LiteralPa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
针对输入列表中的第一个隐藏文件进行压缩。(请注意,如果不使用 Select-Object -ExpandProperty 'FullName'
调用第一个代码段,则会抛出 Compress-Archive : The path '...' either does not exist or is not a valid file system path.
的异常。)
在我的系统上,Microsoft.PowerShell.Archive.psm1 的 812-814 行是...
# Updating the File Creation time so that the same timestamp would be retained after expanding the compressed file.
# At this point we are sure that Get-ChildItem would succeed.
$currentArchiveEntry.LastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWriteTime
-Force
给Get-ChildItem
以获取隐藏文件对象的路径并将其传递给Compress-Archive
,但在内部,该命令正在再次使用Get-Item
获取这些文件对象……但它没有传递-Force
,这当然会失败(尽管前一行的注释声称可以成功)。因此,我认为没有办法让Compress-Archive
处理隐藏文件,除非你或Microsoft编辑该脚本。我用\[System.IO.Compression.ZipFile]::CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName)
替换了 Compress-Archive
, 发现在 macOS 上生成的 ZIP 文件中包含以 .
开始的目录(这是在 macOS 上隐藏目录/文件的一种方式)。这是使用 PowerShell 7.2 实现的。
-Force
参数来执行 Get-ChildItem
以便找到文件,就可以压缩隐藏文件。在我的测试中,归档中未保留隐藏属性。请参见此答案,其中提供了一个在归档中存储路径的示例。 - Rich Moss我刚遇到了同样的问题,以下是我解决它的方法。
# WARNING: This only works on a windows based powershell core terminal instance.
# In a linux based powershell core instance the .dot files are not included in
# the final archive when using the -Filter example
# This will include .files like .gitignore
Get-ChildItem -Path . -Filter *.* | Compress-Archive -DestinationPath dist.zip
# This will include .dot files like .gitignore and any that are hidden like .git
Get-ChildItem -Path . -Force | Compress-Archive -DestinationPath dist.zip
嗯...那真的很烦人啊!我开始使用PowerShell中的7-Zip,但这当然意味着我又在使用另一个工具。这也意味着使用Compress-Archive几乎是多余的。
这是我的脚本的一部分,声明了7Zip变量并压缩文件:
$7ZipPath = "$env:ProgramFiles\7-Zip\7z.exe"
If (-Not (Test-Path -Path $7ZipPath -PathType Leaf)) {
throw "7 Zip File '$7ZipPath' Not Found"
PAUSE
}
Set-Alias 7Zip $7ZipPath
$ItemsToZip = @{
Path= "$TempDirectory\*"
CompressionLevel = "Fastest"
DestinationPath = $MyZipFile
Compress-Archive @ItemsToZip
7zip u $MyZipFile -ux2y2z2 "C:\Path\HiddenFile.ext"
Get-ChildItem -Path '...' -Force | Compress-Archive -DestinationPath '...'
会是解决方案,但它抛出了一个Get-Item:找不到项目...
的错误,并给出了其中一个隐藏文件的路径。 - Lance U. MatthewsCompress-Archive $test.fullname -Force -DestinationPath test.zip
命令并添加force参数来解决此问题,但是仍然出现相同的错误。 - Owain Esau