2017-10-10 38 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