[英]How to move files into a folder and then zip the folder in powershell
[英]How to move a single txt file to the zip in powershell 3
我正在嘗試將一個文本文件復制/移動到zip。 我不想解壓縮它,復制文件並將其壓縮回來。 我們有什么辦法可以直接將文本文件復制或移動到PowerShell中的zip文件中。 當我在powershell中這樣做的時候,它正在那之后,當我試圖查看拉鏈時,它說的是無效的路徑。
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
錯誤:壓縮文件夾無效
> = PowerShell 5.0
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
要將單個文件添加到現有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()
要從頭創建單個文件zip文件:
與上面相同,僅替換: [System.IO.Compression.ZipArchiveMode]::Update
使用: [System.IO.Compression.ZipArchiveMode]::Create
相關文檔:
Compress-Archive
: https : //technet.microsoft.com/en-us/library/dn841358.aspx CreateEntryFromFile
: https : CreateEntryFromFile
( v = CreateEntryFromFile
您可以使用Compress-Archive
進行此操作。 Copy-Item
不支持zip文件。
如果您沒有PowerShell v5,可以使用7Zip命令行或.Net
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.