【问题标题】:Replacing files in a *.zip file without unzipping (extracting) in Powershell替换 *.zip 文件中的文件而不在 Powershell 中解压缩(提取)
【发布时间】:2016-09-10 11:54:06
【问题描述】:
大家好!
目前,我正在尝试在 Powershell 中创建一个脚本,该脚本将替换/删除 *.zip 文件中的文件而不提取它。
我知道可以通过使用 7Zip 和 WinRAR 等应用程序或在文件夹资源管理器中打开一个 zip 存档来手动执行此操作,但我正在寻找一种自动执行此操作的方法。
我是 powershell 和一般编程的新手,所以我无法提出任何初步代码。我试图在网上搜索答案,但找不到任何适合我的答案。
所以问题是:
假设我们有一个名为照片 (Photos.zip) 的 zip 存档。存档包含 5 张图像。如何替换/删除 Photos.zip 中的图像而不在 Powershell 中提取它?
任何代码/想法/有用资源的链接都将受到高度赞赏。
【问题讨论】:
标签:
file
powershell
replace
ziparchive
【解决方案1】:
我使用了以下解决方案。
首先,我使用以下代码将旧文件从存档移动到临时文件夹:
#Path to the temporary folder
$pathToTemporaryFolder = "C:\...\Temporary"
#Path to a file that will be moved from the archive to the temporary folder
$pathToFileToBeMoved = "C:\...\Photos.zip\My Photos\My Image.png"
(New-Object -COM Shell.Application).NameSpace("$pathToTemporaryFolder").MoveHere("$pathToFileToBeMoved")
然后,我将一些新文件复制到存档中:
#Path to the folder with a new file
$pathToFolderWithNewFile = "C:\...\New Images\My New Image.jpeg"
#Path to a folder in the archive where the new file will be copied to
$pathToFolderInArchive = "C:\...\Photos.zip\My Photos"
(New-Object -COM Shell.Application).NameSpace("$pathToFolderInArchive").CopyHere("$pathToFolderWithNewFile")
最后我删除了临时文件夹
Remove-Item -Path "C:\...\Temporary" -Recurse
这就是我从存档中删除文件并将新文件复制到其中而不解压缩/提取的方式。