[英]How to move a single txt file to the zip in powershell 3
I am trying to copy/move one text file to the zip. 我正在尝试将一个文本文件复制/移动到zip。 I don't want to unzip it, copy the file and zip it back. 我不想解压缩它,复制文件并将其压缩回来。 Is there any way we can directly copy or move text file to the zip in powershell. 我们有什么办法可以直接将文本文件复制或移动到PowerShell中的zip文件中。 When i am doing it in powershell, it's doing after that when i try to look inside the zip it's saying invalid path. 当我在powershell中这样做的时候,它正在那之后,当我试图查看拉链时,它说的是无效的路径。
Powershell commands: Powershell命令:
$A = "20160914.4"
New-Item "C:\test\VersionLabel.txt" -ItemType file
$A | Set-Content "C:\test\VersionLabel.txt"
Copy-Item "C:\test\VersionLabel.txt" "C:\test\Abc.zip" -Force
Error: The compressed folder is invalid 错误:压缩文件夹无效
>= PowerShell 5.0 > = PowerShell 5.0
Per @SonnyPuijk's answer above, use Compress-Archive
. Per @ SonnyPuijk上面的答案,使用Compress-Archive
。
clear-host
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
Compress-Archive -Path $fileToZip -Update -DestinationPath $zipFN
< PowerShell 5.0 <PowerShell 5.0
To add a single file to an existing zip: 要将单个文件添加到现有zip:
clear-host
Add-Type -assembly 'System.IO.Compression'
Add-Type -assembly 'System.IO.Compression.FileSystem'
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))
$ZipFile.Dispose()
To create a single file zip file from scratch: 要从头创建单个文件zip文件:
Same as above, only replace: [System.IO.Compression.ZipArchiveMode]::Update
与上面相同,仅替换: [System.IO.Compression.ZipArchiveMode]::Update
With: [System.IO.Compression.ZipArchiveMode]::Create
使用: [System.IO.Compression.ZipArchiveMode]::Create
Related Documentation: 相关文档:
Compress-Archive
: https://technet.microsoft.com/en-us/library/dn841358.aspx Compress-Archive
: https : //technet.microsoft.com/en-us/library/dn841358.aspx CreateEntryFromFile
: https://msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx CreateEntryFromFile
: https : CreateEntryFromFile
( v = CreateEntryFromFile
You can use Compress-Archive
for this. 您可以使用Compress-Archive
进行此操作。 Copy-Item
doesn't support zip files. Copy-Item
不支持zip文件。
If you don't have PowerShell v5 you can use either 7Zip command line or .Net 如果您没有PowerShell v5,可以使用7Zip命令行或.Net
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.