繁体   English   中英

powershell 添加文件到 zip

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM