如何使用Compress-Archive压缩/归档隐藏文件?

15

使用Compress-Archive压缩文件夹时,它会跳过所有隐藏文件。

文档页面告诉我们,此命令使用Microsoft .NET Framework API System.IO.Compression.ZipArchive在后台运行。

有没有办法强制它归档隐藏文件?我找不到任何记录这个问题的地方。我试过-Force,但没用。

我的当前解决方法是使用Set-FileAttribute去掉隐藏属性后再压缩。


2
我以为Get-ChildItem -Path '...' -Force | Compress-Archive -DestinationPath '...'会是解决方案,但它抛出了一个Get-Item:找不到项目...的错误,并给出了其中一个隐藏文件的路径。 - Lance U. Matthews
尝试使用Compress-Archive $test.fullname -Force -DestinationPath test.zip命令并添加force参数来解决此问题,但是仍然出现相同的错误。 - Owain Esau
4个回答

7

这似乎是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

因此,即使我们传递-ForceGet-ChildItem以获取隐藏文件对象的路径并将其传递给Compress-Archive,但在内部,该命令正在再次使用Get-Item获取这些文件对象……但它没有传递-Force,这当然会失败(尽管前一行的注释声称可以成功)。因此,我认为没有办法让Compress-Archive处理隐藏文件,除非你或Microsoft编辑该脚本。

似乎需要为此提出问题:https://github.com/powershell/powershell/issues - henrycarteruk
@JamesC。我提出了这个问题。 - GuidedHacking
2
我在Microsoft.PowerShell.Archive GitHub 仓库中也为此问题打开了一个问题 - Lance U. Matthews

4

1
我已经在 Windows 上测试过使用 system.io.compression.zipfileextensions.createentryfromfile 进行压缩,只要你使用 -Force 参数来执行 Get-ChildItem 以便找到文件,就可以压缩隐藏文件。在我的测试中,归档中未保留隐藏属性。请参见此答案,其中提供了一个在归档中存储路径的示例。 - Rich Moss
我可以确认这在OSX / PS7上运行良好,这是一个绝妙的解决方法,直到“Compress-Archive”更新以处理隐藏文件。 - Hobadee

3

我刚遇到了同样的问题,以下是我解决它的方法。

# 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

1

嗯...那真的很烦人啊!我开始使用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"

希望这能帮助到某人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接