How to Unzip a file and copy it to specific location with 7zip?
I believe the right answer should be something like that:
Get-ChildItem C:\zipplayground\*.zip | % {& "C:\Program Files\7-Zip\7z.exe" "x" $_.fullname "-oC:\unzipplayground"}
Alroc was almost right, but $_.fullname
between quotes doesn't work, and he was missing the -o
parameter for 7z. I'm using 7z.exe
instead of 7zg.exe
, works fine this way.
For reference, the command line help can be found here: http://sevenzip.sourceforge.jp/chm/cmdline/Basically, x
stands for 'eXtract' and -o
for 'Output directory'
Function to acquire path of the 7z.exe
function Get-7ZipExecutable{ $7zipExecutable = "C:\Program Files\7-Zip\7z.exe" return $7zipExecutable}
Function to zip folders where the destination is set to
function 7Zip-ZipDirectories{ param ( [CmdletBinding()] [Parameter(Mandatory=$true)] [System.IO.DirectoryInfo[]]$include, [Parameter(Mandatory=$true)] [System.IO.FileInfo]$destination ) $7zipExecutable = Get-7ZipExecutable # All folders in the destination path will be zipped in .7z format foreach ($directory in $include) { $arguments = "a","$($destination.FullName)","$($directory.FullName)" (& $7zipExecutable $arguments) $7ZipExitCode = $LASTEXITCODE if ($7ZipExitCode -ne 0) { $destination.Delete() throw "An error occurred while zipping [$directory]. 7Zip Exit Code was [$7ZipExitCode]." } } return $destination}
Function to unzip files
function 7Zip-Unzip{ param ( [CmdletBinding()] [Parameter(Mandatory=$true)] [System.IO.FileInfo]$archive, [Parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$destinationDirectory ) $7zipExecutable = Get-7ZipExecutable $archivePath = $archive.FullName $destinationDirectoryPath = $destinationDirectory.FullName (& $7zipExecutable x "$archivePath" -o"$destinationDirectoryPath" -aoa -r) $7zipExitCode = $LASTEXITCODE if ($7zipExitCode -ne 0) { throw "An error occurred while unzipping [$archivePath] to [$destinationDirectoryPath]. 7Zip Exit Code was [$7zipExitCode]." } return $destinationDirectory}
I don't have 7Zip to test,but I think it's failing because you aren't telling 7Zip what to operate on, and you're moving your ZIP file to the destination yourself. Try this:
Get-ChildItem C:\zipplayground\*.zip | % {invoke-expression "C:\Program Files (x86)\7-Zip\7zG.exe x $_.FullName c:\unzipplayground\";}