Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically extract tar.gz in a single step (on Windows with 7-Zip)

Problem: I would like to be able to extract tar.gz files in a single step. This makes my question almost identical to this one: Stack Overflow question for tar-gz.

My question is almost the same, but not the same, because I would like to do this on windows using 7-Zip command-line (or something similar) inside a bat file or Ruby/Perl/Python script.

Question: This seemingly simple task is proving to be more involved than the first appearance would make it out to be. Does anyone have a script that does this already?

like image 559
dreftymac Avatar asked Aug 31 '09 23:08

dreftymac


People also ask

How do I extract a single file from a tar gz file?

For that, you need to make use of the “tar” command with the “-xvf” option and the name of a “tar” file while we are currently located at the “Downloads” folder. The option “x” is used for extraction, “-v” is used to display in ascending order, and “-f” is used to perform the extraction forcefully.


2 Answers

Old question, but I was struggling with it today so here's my 2c. The 7zip commandline tool "7z.exe" (I have v9.22 installed) can write to stdout and read from stdin so you can do without the intermediate tar file by using a pipe:

7z x "somename.tar.gz" -so | 7z x -aoa -si -ttar -o"somename" 

Where:

x     = Extract with full paths command -so   = write to stdout switch -si   = read from stdin switch -aoa  = Overwrite all existing files without prompt. -ttar = Treat the stdin byte stream as a TAR file -o    = output directory 

See the help file (7-zip.chm) in the install directory for more info on the command line commands and switches.

As noted by @zespri powershell will buffer the input to the second 7z process so can consume a lot of memory if your tar file is large. i.e:

& 7z x "somename.tar.gz" -so  | & 7z x -aoa -si -ttar -o"somename" 

A workaround from this SO answer if you want to do this from powershell is to pass the commands to cmd.exe:

& cmd.exe '/C 7z x "somename.tar.gz" -so | 7z x -aoa -si -ttar -o"somename"' 
like image 121
user2856 Avatar answered Sep 19 '22 12:09

user2856


7z e example.tar.gz  && 7z x example.tar 

Use && to combine two commands in one step. Use the 7-Zip portable (you will need 7z.exe and 7z.dll only).

like image 35
svandragt Avatar answered Sep 23 '22 12:09

svandragt