[英]Replacing files in a *.zip file without unzipping (extracting) in Powershell
嗨,大家好!
目前,我正在嘗試在Powershell中創建一個腳本,它將替換/刪除* .zip文件中的文件而不提取它 。
我知道可以通過使用7Zip和WinRAR等應用程序手動完成,也可以只在文件夾資源管理器中打開zip存檔,但我正在尋找一種自動執行此操作的方法。
我是PowerShell和一般編程的新手,所以我不能提出任何初步的代碼。 我試圖在網上搜索答案,但沒有找到任何對我有用的東西。
所以問題是:
假設我們有一個名為Photos(Photos.zip)的zip存檔。 存檔包含5張圖像。 如何替換/刪除Photos.zip中的圖像而不在Powershell中提取它?
任何有用資源的代碼/想法/鏈接都將受到高度贊賞。
我使用了以下解決方案。
首先,我使用以下代碼將舊文件從存檔移動到臨時文件夾:
#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
這是我從存檔中刪除文件並將新文件復制到其中而不解壓縮/解壓縮的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.