我正在尝试使用 Powershell v5.1 压缩文件夹,但某些文件已被另一个进程使用,PS 无法强制或忽略它们。
Get-ChildItem "C:\folder" | Compress-Archive -DestinationPath "C:\file.zip"
还用 -Force
和 -ErrorAction Ignore,Continue,SilentlyContinue
进行了测试,但每次我都会遇到这样的错误:
ZipArchiveHelper : The process cannot access the file 'C:\folder\filexyz' be cause it is being used by another process. At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:69 6 char:30 + ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\folder\filexyz:String) [Write-Error], IOException + FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable." At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:80 7 char:38 + ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
最佳答案
异常:使用“1”参数调用“.ctor”的异常:“流不可读。”
当使用多个文件与 Compress-Archive 并且一个或多个打开时发生.
您可以在将文件传送到 Compress-Archive 之前检查文件是否未锁定。
$items = Get-ChildItem -Path $path
[System.Collections.ArrayList]$itemsToCompress = @()
[System.Collections.ArrayList]$itemsToNotCompress = @()
foreach ($item in $items){
Try {
# Checking File Mode and Access
$FileStream = [System.IO.File]::Open($item.FullName,'Open','Read')
if ($null -ne $FileStream){
$FileStream.Close()
$FileStream.Dispose()
$itemsToCompress += $item
}
}
Catch {
$itemsToNotCompress += $item
}
}
$itemsToCompress | Compress-Archive -DestinationPath $archivefile -ErrorAction SilentlyContinue
关于powershell - 压缩存档错误 : PermissionDenied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684948/