【问题标题】:Compress-Archive error: PermissionDenied压缩存档错误:PermissionDenied
【发布时间】:2018-07-18 23:47:23
【问题描述】:

我正在尝试使用 Powershell v5.1 压缩文件夹,但某些文件已被另一个进程使用,PS 无法强制或忽略它们。

Get-ChildItem "C:\folder" | Compress-Archive -DestinationPath "C:\file.zip"

也使用-Force-ErrorAction Ignore,Continue,SilentlyContinue 进行了测试,但每次我收到这样的错误:

ZipArchiveHelper:该进程无法访问文件“C:\folder\filexyz”,因为它正被另一个进程使用。 在 C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:69 6 个字符:30 + ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\folder\filexyz:String) [Write-Error], IOException + FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper 新对象:使用“1”参数调用“.ctor”的异常:“流不可读。” 在 C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:80 7 个字符:38 + ... $srcStream = 新对象 System.IO.BinaryReader $currentFileStream + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

【问题讨论】:

  • 没有错误操作Ignore/Continue(或Ignore)。你试过SilentlyContinue吗?
  • 是的,我几乎都测试过了
  • Get-ChildItem -Path C:\folder -ErrorAction SilentlyContinue | Compress-Archive -Destination D:\file.zip
  • 另外,您的访问被拒绝,而不是文件在使用中。 CompressArchiveUnauthorizedAccessError 这意味着您无权访问 D:\
  • 我修复了那个例子...我在任何地方都有完全访问权限-管理员

标签: powershell


【解决方案1】:

例外情况:Exception calling ".ctor" with "1" argument(s): "Stream was not readable." 在使用 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

【讨论】:

    【解决方案2】:

    由于其他进程使用的文件仍然可以读取,我认为问题在于权限不足。

    尝试以管理员身份启动 PowerShell(搜索 PowerShell -> 右键单击​​ -> 以管理员身份运行)。

    【讨论】:

    • 我是管理员并修复了该示例,默认情况下我以管理员身份运行...
    【解决方案3】:

    您可以检查 $Error 对象。如果它是在 compress 调用之后填充的,则发生错误,您可以采取适当的措施。

    $error.clear()
    Get-ChildItem "C:\folder" | Compress-Archive -DestinationPath "C:\file.zip"
    if ($error[0] -ne $null) {
        # Take appropriate action here
    }
    

    有关更多信息,请参阅此主题。 https://community.spiceworks.com/topic/2026265-checking-the-success-or-failure-of-compress-archive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 2011-01-20
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多