压缩归档错误:权限被拒绝。

3
我正在尝试使用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 char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\folder\filexyz:String) [Write-Error], IOException
    + FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper

New-Object:使用“1”个参数调用“.ctor”时发生异常:“流不可读取。”
位于 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

没有错误操作Ignore/Continue(或Ignore)。您尝试过SilentlyContinue吗? - Ansgar Wiechers
是的,我测试了几乎所有它们。 - LamiX
Get-ChildItem -Path C:\folder -ErrorAction SilentlyContinue | Compress-Archive -Destination D:\file.zip - Maximilian Burszley
此外,您收到的是访问被拒绝,而不是文件正在使用中。CompressArchiveUnauthorizedAccessError 这意味着您无法访问 D:\ - Maximilian Burszley
我修复了那个例子... 我有管理员权限,可以访问所有地方。 - LamiX
显示剩余4条评论
3个回答

3

当使用多个文件进行Compress-Archive操作时,如果其中一个或多个文件被打开,则会出现以下异常:Exception calling ".ctor" with "1" argument(s): "Stream was not readable."

在将文件传递给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

1

0

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

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


我是管理员,已经修复了那个例子,并默认以管理员身份运行所有内容... - LamiX

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