Skip to content

Instantly share code, notes, and snippets.

@xpando
Last active June 27, 2019 11:01
Show Gist options
  • Save xpando/8a896d903ceb7cc31192 to your computer and use it in GitHub Desktop.
Save xpando/8a896d903ceb7cc31192 to your computer and use it in GitHub Desktop.
Powershell function to unzip files using 7zip. Accounts for tar.ga and tgz files. Uses piping to process gzipped tar files in a single pass.
function unzip($path,$to) {
$7z = "$env:TEMP\7z"
if (!(test-path $7z) -or !(test-path "$7z\7za.exe")) {
if (!(test-path $7z)) { md $7z | out-null }
push-location $7z
try {
write-host "Downloading 7zip" -foregroundcolor cyan
$wc = new-object system.net.webClient
$wc.headers.add('user-agent', [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox)
$wc.downloadFile("http://softlayer-dal.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7za920.zip","$7z\7z.zip")
write-host "done." foregroundcolor green
add-type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::extracttodirectory("$7z\7z.zip","$7z")
del .\7z.zip
}
finally { pop-location }
}
if ($path.endswith('.tar.gz') -or $path.endswith('.tgz')) {
# This is some crazy s**t right here
$x = "cmd"
$y = "/C `"^`"$7z\7za.exe^`" x ^`"$path^`" -so | ^`"$7z\7za.exe^`" x -y -si -ttar -o^`"$to^`""
& $x $y
} else {
& "$7z\7za.exe" x $path -y -o"$to"
}
}
@lawrencegripper
Copy link

This is awesomely useful, Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment