2017-10-10 39 views
-1

我試圖使用PowerShell下面的命令來解壓縮解壓縮 -無法使用PowerShell

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('E:\test.zip', 'E:\'); }" 

我得到下面登錄

'E:\test1.txt' 
already exists." 
At line:1 char:53 
+ & { Add-Type -A 'System.IO.Compression.FileSystem'; 
[IO.Compression.ZipFile]::Ex ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : IOException 

=============================== 

我沒有看到正在解壓縮存檔。

更新1

E:\ test1.txt的已經存在的目的地。如何更改命令以覆蓋文件。

更新2

提供的PowerShell版本不支持Expand-Archive

+1

沒有E:\ test1.txt的存在? – ArcSet

+0

是的,E:\ test1.txt存在。我想在解壓縮過程中覆蓋此文件。 – Apurv

+0

這就是你的問題....我如何用解壓文件覆蓋文件....我想你應該編輯你的問題 – ArcSet

回答

2

你不能用這種方法覆蓋文件。你需要閱讀the documentation for ZipFileExtensions.ExtractToDirectory(source, destinationDirectoryName)

這個方法創建 destinationDirectoryName指定的目錄。 如果目標目錄已存在,則此方法不覆蓋它;它會拋出一個IOException 異常。該方法還會創建反映zip歸檔文件中的 層次結構的子目錄。如果在提取過程中發生錯誤,則檔案保持部分提取。每個提取的文件具有 與由 指定的目錄相同的相對路徑destinationDirectoryName作爲其源條目必須存檔的根目錄。

如果要使用ZipFileExtensions.ExtractToDirectory()進行覆蓋,則需要將文件提取到臨時文件夾,然後將它們複製/移動到所需的位置。

嘗試類似:

do { 
    $TempFolder = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $([System.IO.Path]::GetRandomFileName()); 
} while ((Test-Path -Path $TempFolder)); 

mkdir $TempFolder | Out-Null; 

[IO.Compression.ZipFile]::ExtractToDirectory('E:\test.zip',$TempFolder); 

Get-ChildItem -Path $TempFolder -Recurse | Move-Item -Destination 'E:\' -Force; 

rmdir $TempFolder; 

請注意,此代碼是未經測試。

+0

感謝您的想法。我編寫了簡單的命令來解壓縮臨時文件夾,複製到目標文件夾和刪除臨時文件夾。 – Apurv

0

下面就是解決我的問題

md E:\temp 
move E:\test.zip  E:\temp 
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('E:\temp\test.zip', 'E:\temp\'); }" 
del E:\temp\test.zip 
move /Y E:\temp\* E:\ 
rd E:\temp