powershell 将文件添加到 zip [英] powershell add file to zip

查看:45
本文介绍了powershell 将文件添加到 zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 powershell 将文件添加到 zip 文件

I'm trying to add a file to a zip file using powershell

我可以创建 zip 文件,但不知道如何将我的文件添加到其中

I can create the zip file but can't work out how to add my file to it

我正在使用

$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 文件,但不会添加新文件

This create the zip file but doesn't add the new file

我尝试了我在stackoverflow上找到的以下代码

I tried the following code I found on stackoverflow which works

$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 创建的文件,而我想添加一个现有文件

but that add a file created by powershell whereas I want to add an existing file

任何帮助将不胜感激

推荐答案

CopyHere 函数只需要一个字符串作为文件的路径.例如:

The CopyHere function just takes a string that is the path to your file. For example:

$folder.copyhere( "c:\PathToYourFile\YourFile" )

提示:

Powershell Pack Module 有一些有用的工具,其中之一是 zip 实用程序,将您的代码减少到 1 行:

The Powershell Pack Module has some useful tools, one of which is a zip utility, which will reduce your code to 1 line:

Copy-ToZip "c:\PathToYourFile\YourFile" -ZipFile a.zip

这篇关于powershell 将文件添加到 zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆