大家好!
当前,我正在尝试在Powershell中创建一个脚本,该脚本将替换/删除* .zip文件中的文件而不将其解压缩。
我知道可以通过使用7Zip和WinRAR之类的应用程序来手动进行操作,也可以只在Folder Explorer中打开一个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
这就是我从存档中删除文件并将新文件复制到其中而不进行解压缩/提取的方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句