programmatically extract tar.gz in a single step (on windows with 7zip)

偶尔善良 提交于 2019-12-02 20:10:43
7z e example.tar.gz  && 7z x example.tar

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

user2856

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"'

Use the win32 port of tar.

tar -xvfz filename.tar.gz
senthil

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!