2016-09-14 76 views
2

我試圖將一個文本文件複製/移動到壓縮文件中。我不想將其解壓縮,將文件複製並壓縮回去。有什麼方法可以直接將文本文件複製或移動到PowerShell中的壓縮文件中。當我在PowerShell中做這件事的時候,它正在做這件事之後,當我嘗試查看zip文件時,它說無效路徑。如何將單個文本文件移動到PowerShell中的壓縮文件3

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

+0

[如何使用PowerShell創建zip存檔?](https://stackoverflow.com/a/12978117),您可以通過ZipArchive或ZipPackage或zip文件夾命名空間手動完成。相關問題有很多答案。 – wOxxOm

回答

1

您可以使用Compress-Archive這一點。 Copy-Item不支持zip文件。

如果你沒有PowerShell的V5則可以使用7Zip的命令行或.Net

+0

有沒有其他辦法可以做到這一點。我不想使用72ip – Meet101

+0

只有我知道的其他方法是使用dotnet庫。腳本編寫人員已經寫了一篇關於此的博客。 https://blogs.technet.microsoft.com/heyscriptingguy/2015/03/09/use-powershell-to-create-zip-archive-of-folder/ –

5

> = 5.0的PowerShell

每@ 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

到單個文件添加到現有拉鍊:

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

相關文檔:

+0

注意:要從目錄創建zip文件,請參閱http ://stackoverflow.com/a/20070550/361842 – JohnLBevan

相關問題