[英]powershell add file to zip
我正在嘗試使用 powershell 將文件添加到 zip 文件
我可以創建 zip 文件,但不知道如何向其中添加我的文件
我正在使用
$zipfilename = 'c:\cwRsync\backup.zip'
$file = 'c:\cwRsync\backup.log'
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
$zipfile = (New-Object -ComObject shell.application).NameSpace($zipfilename)
$zipfile.MoveHere($file.FullName)
這會創建 zip 文件但不會添加新文件
我嘗試了以下在 stackoverflow 上找到的有效代碼
$zipfilename = "a.zip"
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$app = new-object -com shell.application
$zip = ( get-item a.zip ).fullname
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )
但是添加了一個由 powershell 創建的文件,而我想添加一個現有文件
任何幫助將非常感激
要創建 zip 存檔:
powershell Compress-Archive %file% %file%.zip
要替換存檔(使用不同的文件):
powershell Compress-Archive -force %different_file% %file%.zip
將文件添加到(現有)存檔:
powershell Compress-Archive -update %2nd_file% %file%.zip
在 Win 10 上測試 CMD Powershell 5.1
CopyHere
函數僅采用字符串作為文件路徑。 例如:
$folder.copyhere( "c:\PathToYourFile\YourFile" )
小費:
Powershell Pack模塊具有一些有用的工具,其中一個是zip實用程序,它將把您的代碼減少到1行:
Copy-ToZip "c:\PathToYourFile\YourFile" -ZipFile a.zip
我從筆記中拉了這個。 從腳本專家的網站上刪除它,也許這會有所幫助...
$source = "D:\temp\test"
$destination = "d:\temp\csvdir_Backup.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.