无法将 Windows 中搜索到的文件以其绝对路径捆绑到 ZIP 中

问题描述 投票:0回答:1

我想在目录

$itemsToInclude
下搜索提供给
C:\bea
的文件或文件夹 一旦找到,它应该将找到的项目[文件或文件夹]添加到 $zipFileName,同时保留文件夹结构。

因此,如果在

C:\bea\dev\log\rt.jar
C:\bea\QA\log\rt.jar
中找到 rt.jar,我的
C:\bea\package-dev.zip
应该包含两者,如下所示:

- `C:\bea\dev\log\rt.jar`
- `C:\bea\QA\log\rt.jar`

下面是我的尝试,失败并出现错误

Access to the path 'C:\bea\package-dev.zip' is denied."
但我的 powershell 可以很好地写入
C:\bea\
文件夹。我不希望看到任何“访问被拒绝”错误。

  $zipFileName = "C:\bea\package-dev.zip"
  cd "C:\bea"
  $itemsToInclude = "font.properties,rt.jar"
  Write-Host "itemsToInclude is- $itemsToInclude"
  $workspace = "C:\bea"
  if ($itemsToInclude -eq '*') {
  # Include all files, including files from subdirectories
  Write-Host "Include all files, including files from subdirectories"
  Get-ChildItem -Path $workspace -Recurse -File |
    Compress-Archive -DestinationPath $zipFileName  -Update -Force
  } else {
  # Include specific files as per the comma-separated list
  Write-Host "Include specific files as per the comma-separated list"
  $pattern = $itemsToInclude.Split(',').ForEach({ [regex]::Escape($_) }) -join '|'
  $filesToInclude = Get-ChildItem $workspace -Recurse -File |
  Where-Object FullName -Match $pattern
  
  $filesToInclude | ForEach-Object {
    $relativePath = $_.FullName.Substring($workspace.Length)
    $destinationPath = Join-Path $zipFileName $relativePath
    $destinationFolder = Split-Path $destinationPath -Parent
    if (-not (Test-Path $destinationFolder)) {
      New-Item -ItemType Directory -Path $destinationFolder | Out-Null
      }
      Copy-Item $_.FullName -Destination $destinationPath -Force
    }
    Compress-Archive -Path $zipFileName -DestinationPath $zipFileName -Update
  }

输出:

itemsToInclude is- font.properties,rt.jar
Include specific files as per the comma-separated list
New-Object : Exception calling ".ctor" with "2" argument(s): "Access to the path 'C:\bea\package-dev.zip' is denied."
At C:\windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:729 
char:30
+ ... ileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
 

注意:我可以使用以下代码成功创建 ZIP,而无需同一目录中文件的绝对路径:

  $zipFileName = "C:\bea\package-dev.zip"
  cd "C:\bea"
  $itemsToInclude = "font.properties,rt.jar"
  Write-Host "itemsToInclude is- $itemsToInclude"
  $workspace = "C:\bea"
  if ($itemsToInclude -eq '*') {
  # Include all files, including files from subdirectories
    Write-Host "Include all files, including files from subdirectories"
    Get-ChildItem -Path $workspace -Recurse -File |
      Compress-Archive -DestinationPath $zipFileName  -Update -Force
     } else {
     # Include specific files as per the comma-separated list
     Write-Host "Include specific files as per the comma-separated list" 
       $pattern = $itemsToInclude.Split(',').ForEach({ [regex]::Escape($_) }) -join '|'
       Get-ChildItem $workspace -Recurse -File |
       Where-Object FullName -Match $pattern |
       Compress-Archive -DestinationPath $zipFileName -Update
       }

你能推荐一下吗?

windows powershell zip access-denied file-search
1个回答
0
投票

很难确定代码失败的原因,可能是

Compress-Archive
未正确关闭 zip 流,或者您在尝试运行代码时打开了 zip。

如果您能够安装模块,那么使用

PSCompression
可以简化将 zip 条目添加到现有 zip 存档的任务。该模块可以通过 PowerShell Gallery 安装:

Install-Module PSCompression -Scope CurrentUser

然后可以使用与文档的

示例 4
中类似的逻辑来替换代码的else和平:

Write-Host 'Include specific files as per the comma-separated list'
$pattern = $itemsToInclude.Split(',').ForEach({ [regex]::Escape($_) }) -join '|'
$filesToInclude = Get-ChildItem $workspace -Recurse -File |
    Where-Object FullName -Match $pattern

$filesToInclude | ForEach-Object {
    $newZipEntrySplat = @{
        EntryPath   = $_.FullName.Substring($workspace.Length)
        SourcePath  = $_.FullName
        Destination = $zipFileName
    }

    New-ZipEntry @newZipEntrySplat
}
© www.soinside.com 2019 - 2024. All rights reserved.