17

compress-archive我在做我想做的事时遇到了挑战……

我有一个根项目文件夹,我想压缩子目录中的一些文件并保留相对路径。例如: / ├── _scripts ├── ├─_module1 | | └── filex.js | └─_module2 | ├── file1.js | └── file2.txt

因此,我想从我的根目录创建一个包含 的 zip 文件module2/*,并且我想保留文件夹结构。我希望我的 zip 包含: scripts/module2/file1.js scripts/module2/file2.txt

但是当我从根文件夹运行它时: Compress-Archive -Path "scripts\module2\*" -DestinationPath tmp.zip

zip 文件的内容仅包含: /file1.js /file2.txt

4

4 回答 4

12

看来Compress-Archive(从 Windows PowerShell v5.1 开始)不支持您想要的:

以文件夹为目标递归地将该文件夹的子树添加到存档中,但仅通过目标文件夹的名称(它成为存档中的子文件夹),而不是其路径

具体来说,

Compress-Archive -Path scripts\module2 -DestinationPath tmp.zip

将(递归地)存储scripts\module2in的内容tmp.zip,但不使用 archive-internal path .\scripts\module2,仅使用.\module2- 目标文件夹的名称最后一个输入路径组件)。

这意味着您必须传递文件夹scripts来获取所需的存档内部路径,但这将始终包括 的整个子树scripts因为它Compress-Archive不提供包含/排除机制。


一个 - 麻烦 - 选项是在文件夹中重新创建所需的层次结构,$env:TEMP将目标文件夹复制到那里,Compress-Archive针对重新创建的层次结构的根运行,然后清理:

New-Item -Force -ItemType Directory $env:TEMP/scripts
Copy-Item -Recurse -Force scripts/module2 $env:TEMP/scripts
Compress-Archive -LiteralPath $env:TEMP/scripts -DestinationPath tmp.zip
Remove-Item $env:TEMP/Scripts -Recurse -Whatif

否则,您可能会找到解决方案:

  • 直接使用 .NET v4.5+[System.IO.Compression.ZipFile]您可以使用(在 PowerShell CoreAdd-Type -Assembly System.IO.Compression.FileSystem中不需要)将其加载到您的会话中。

  • 通过使用7-Zip等外部程序,

于 2018-07-18T05:36:59.370 回答
4

我想这样做而不必将完整结构复制到临时目录。

#build list of files to compress
$files = @(Get-ChildItem -Path .\procedimentos -Recurse | Where-Object -Property Name -EQ procedimentos.xlsx);
$files += @(Get-ChildItem -Path .\procedimentos -Recurse | Where-Object -Property Name -CLike procedimento_*_fs_*_d_*.xml);
$files += @(Get-ChildItem -Path .\procedimentos -Recurse | Where-Object -Property FullName -CLike *\documentos_*_fs_*_d_*);

# exclude directory entries and generate fullpath list
$filesFullPath = $files | Where-Object -Property Attributes -CContains Archive | ForEach-Object -Process {Write-Output -InputObject $_.FullName}

#create zip file
$zipFileName = 'procedimentos.zip'
$zip = [System.IO.Compression.ZipFile]::Open((Join-Path -Path $(Resolve-Path -Path ".") -ChildPath $zipFileName), [System.IO.Compression.ZipArchiveMode]::Create)

#write entries with relative paths as names
foreach ($fname in $filesFullPath) {
    $rname = $(Resolve-Path -Path $fname -Relative) -replace '\.\\',''
    echo $rname
    $zentry = $zip.CreateEntry($rname)
    $zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
    $zentryWriter.Write([System.IO.File]::ReadAllBytes($fname))
    $zentryWriter.Flush()
    $zentryWriter.Close()
}

# clean up
Get-Variable -exclude Runspace | Where-Object {$_.Value -is [System.IDisposable]} | Foreach-Object {$_.Value.Dispose(); Remove-Variable $_.Name};
于 2019-05-06T18:16:07.833 回答
2

这是一个有点旧的线程,但我认为这将帮助人们通过 PowerShell 5.1 创建 zip 文件,这是目前 Windows 10 安装的标准配置。脚本允许您保留原始子目录结构以及排除一些不必要的子树/文件。这是我用来存档我的 Visual Studio 解决方案的源代码的:

Write-Output "Zipping Visual Studio solution..."

# top level from where to start and location of the zip file
$path = "C:\TheSolution"
# top path that we want to keep in the source code zip file
$subdir = "source\TheSolution"
# location of the zip file
$ZipFile = "${path}\TheSolution.zip"

# change current directory
Set-Location "$path"

# collecting list of files that we want to archive excluding those that we don't want to preserve
$Files  = @(Get-ChildItem "${subdir}" -Recurse -File | Where-Object {$_.PSParentPath -inotmatch "x64|packages|.vs|Win32"})
$Files += @(Get-ChildItem "${subdir}\packages" -Recurse -File)
$Files += @(Get-ChildItem "${subdir}\.git" -Recurse -File)
$FullFilenames = $files | ForEach-Object -Process {Write-Output -InputObject $_.FullName}

# remove old zip file
if (Test-Path $ZipFile) { Remove-Item $ZipFile -ErrorAction Stop }

#create zip file
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open(($ZipFile), [System.IO.Compression.ZipArchiveMode]::Create)

# write entries with relative paths as names
foreach ($fname in $FullFilenames) {
    $rname = $(Resolve-Path -Path $fname -Relative) -replace '\.\\',''
    Write-Output $rname
    $zentry = $zip.CreateEntry($rname)
    $zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
    $zentryWriter.Write([System.IO.File]::ReadAllBytes($fname))
    $zentryWriter.Flush()
    $zentryWriter.Close()
}

# release zip file
$zip.Dispose()
于 2019-11-24T14:45:01.577 回答
1

mklement0 提到的繁琐技术对我有用。下面是我创建的脚本,用于支持与文件夹混合的各种文件的列表。

# Compress LFS based files into a zip
# To use
#  1. place this script in the root folder
#  2. modify the contents of $lfsAssetFiles to point to files relative to this root folder
#  3. modify $zipDestination to be where you want the resultant zip to be placed
# based off of https://stackoverflow.com/a/51394271

# this should match files being .gitignored
$lfsAssetFiles = 
"\Assets\Project\Plugins\x32",
"\Assets\Project\Plugins\x64\HugePlugin.dll"

# This is where the contents of the zip file will be structured, because placing them inside of a specific folder of the zip is difficult otherwise
$zipStruct = $PSScriptRoot + "\zipStruct"

# the actual zip file that will be created
$zipDestination = "C:\Dropbox\GitLfsZip\ProjectNameLfs.zip"

# remove files from previous runs of this script
If(Test-path $zipStruct) {Remove-item $zipStruct -Recurse}
If(Test-path $zipDestination) {Remove-item $zipDestination}

Foreach ($entry in $lfsAssetFiles)
{
  # form absolute path to source each file to be included in the zip
  $sourcePath = $PSScriptRoot + $entry;

  # get the parent directories of the path. If the entry itself is a directory, we still only need the parent as the directory will be created when it is copied over.
  $entryPath = Split-Path -Parent $entry

  # form what the path will look like in the destination
  $entryPath = $zipStruct + $entryPath

  # ensure the folders to the entry path exist
  $createdPath = New-Item -Force -ItemType Directory $entryPath

  # copy the file or directory
  Copy-Item -Recurse -Force $sourcePath $createdPath
}

# create a zip file https://blogs.technet.microsoft.com/heyscriptingguy/2015/page/59/
Add-Type -AssemblyName "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($zipStruct, $zipDestination)
# Compress-Archive doesn't work here because it includes the "zipStruct" folder: Compress-Archive -Path $zipStruct -DestinationPath $zipDestination
于 2018-09-04T23:36:53.573 回答