问题描述
我很难让 compress-archive
做我想做的事...
I'm having a challenging time getting compress-archive
to do what I want...
我有一个根项目文件夹,我想压缩子目录中的一些文件并保留相对路径.例如:/├── _scripts├── ├─_module1||└── filex.js|└─_module2|├── file1.js|└── file2.txt
I have a root project folder and I want to zip up some of the files in sub-directories and preserve the relative paths. For example:/├── _scripts├── ├─_module1| | └── filex.js| └─_module2| ├── file1.js| └── file2.txt
因此,我想从我的根目录创建一个包含 module2/*
的 zip 文件,并且我想保留文件夹结构.我希望我的 zip 包含:脚本/module2/file1.js脚本/module2/file2.txt
So from my root directory, i'd like to create a zip file that includes module2/*
, and i want to keep the folder structure. I'd like my zip to contain:scripts/module2/file1.jsscripts/module2/file2.txt
但是当我从根文件夹运行它时:Compress-Archive -Path "scripts\module2\*" -DestinationPath tmp.zip
But when I run this from the root folder:Compress-Archive -Path "scripts\module2\*" -DestinationPath tmp.zip
zip 文件的内容仅包含:/file1.js/file2.txt
The contents of the zip file only contain:/file1.js/file2.txt
推荐答案
Compress-Archive
(从 Windows PowerShell v5.1 开始)似乎不支持您想要的:
It appears that Compress-Archive
(as of Windows PowerShell v5.1) doesn't support what you want:
定位一个文件夹会递归地将该文件夹的子树添加到存档中,但只能通过目标文件夹的名称(成为存档内的子文件夹),而不是它的路径.
Targeting a folder recursively adds that folder's subtree to the archive, but only by the target folder's name (which becomes a child folder inside the archive), not its path.
具体来说,
Compress-Archive -Path scripts\module2 -DestinationPath tmp.zip
将(递归地)将 scripts\module2
的内容存储在 tmp.zip
中,但不包含存档内部路径 .\scripts\module2
,只需 .\module2
- 目标文件夹的名称(最后输入路径组件).
will (recursively) store the contents of scripts\module2
in tmp.zip
, but not with archive-internal path .\scripts\module2
, just with .\module2
- the target folder's name (the last input path component).
这意味着您必须通过文件夹 scripts
而不是获得所需的存档内部路径,但这总是包括 的整个子树>scripts
,鉴于 Compress-Archive
不提供包含/排除机制.
The implication is that you'd have to pass folder scripts
instead to get the desired archive-internal path, but that would invariably include the entire subtree of scripts
, given that Compress-Archive
offers no inclusion/exclusion mechanism.
一个 - 繁琐 - 选项是在 $env:TEMP
文件夹中重新创建所需的层次结构,将目标文件夹复制到那里,运行 Compress-Archive
重新创建层次结构的根,然后清理:
One - cumbersome - option is to recreate the desired hierarchy in, say, the $env:TEMP
folder, copy the target folder there, run Compress-Archive
against the root of the recreated hierarchy, and then clean up:
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]
类 直接;您可以使用Add-Type -Assembly System.IO.Compression.FileSystem
(在 PowerShell Core 中不需要)将其加载到您的会话中.
by using the .NET v4.5+
[System.IO.Compression.ZipFile]
class directly; you can load it into your session withAdd-Type -Assembly System.IO.Compression.FileSystem
(not necessary in PowerShell Core).
通过使用外部程序,例如 7-Zip、
by using external programs such as 7-Zip,
这篇关于压缩归档并保留相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!