40

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dreftymac
  • 31,404
  • 26
  • 119
  • 182

5 Answers5

86

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"'
Community
  • 1
  • 1
user2856
  • 1,145
  • 1
  • 10
  • 24
  • 11
    a word of advise: if you running it on windows, and your archive is large do not run this under powershell - it will suck the whole thing into memory instead of streaming. open cmd prompt and run it from there. – Andrew Savinykh Nov 29 '14 at 11:29
  • This is awesome. (Note that when I tried it (v15.12), it created the tar file in the working directory, but deleted it when it was done) – Dave Apr 27 '16 at 19:16
  • @Dave That doesn't happen for me in 15.11. Were you running it directly in a cmd prompt or in powershell with the cmd workaround? – user2856 Apr 27 '16 at 23:39
34
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
svandragt
  • 1,672
  • 20
  • 38
  • 14
    This isn't exactly one step. It's still doing the gzip step first, leaving a potentially very large tar file around. – bug Feb 04 '13 at 17:36
  • see https://stackoverflow.com/a/12321558/5612605 (answere from @senthil ) to have a single handy solution ; ) – Cutton Eye Jun 14 '18 at 12:47
13

Use the win32 port of tar.

tar -xvfz filename.tar.gz
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • lately you must run bsdtar instead of tar but still a great idea and simpler than using 7zip – pcnate Oct 03 '19 at 15:53
8

Since you asked for 7-zip or something similar, I am providing an alternative tool that was written for your exact use case.

The tartool utility is a free and open source tool built using the .NET SharpZipLib library.

Example commmand to extract a .tar.gz / .tgz - file,

C:\>TarTool.exe D:\sample.tar.gz ./

Disclaimer : I am the author of this utility.

Cutton Eye
  • 3,207
  • 3
  • 20
  • 39
senthil
  • 339
  • 3
  • 8
0

As you can see 7-Zip is not very good at this. People have been asking for tarball atomic operation since 2009. As an alternative, you can use the Arc program. Example command:

arc unarchive test.tar.gz
Zombo
  • 1
  • 62
  • 391
  • 407