Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use GNU tar. It can auto detect compression type when compressing/decompressing files. But I need to decompress an archive from stdin and the compress type is unknown. I noticed that tar can give me correct suggesting like:

tar: Archive is compressed. Use -z option

But I want tar to use that compression option automatically without asking me to input that argument. How can I do that? Why not tar just decompress since it already knows the compression type?

Thank you!

share|improve this question
    
It weren't hard to develop in, but it is not done until now. –  Peter Horvath Dec 17 '13 at 22:30
2  
The reason it cannot do this is that tar does not know which type of data it has on stdin until after it reads it, and by then it is too late to call the uncompress program. It supports gzip, bzip2, and others. Working around this problem is not easy (it would have to buffer the data), so it just tells you to try again. –  Kevin Panko Dec 17 '13 at 22:46
    
@KevinPanko Why where it too late? No, it is not late. He reads f.e. the first 4K in a buffer, tests its compression type (if there is one), then calls the program. This buffering were 10-20 lines of additional C code. –  Peter Horvath Dec 17 '13 at 22:55
2  
At that point in time when it knows what data it has, it can no longer use a simple fork()/exec() method to pipe the data through an uncompress utility. The utility would read from the stdin pipe and the first 4K would now be missing. There is no way to put the data back into the pipe after reading it. –  Kevin Panko Dec 17 '13 at 22:59
3  
@KevinPanko: It could be done by forking two processes, though, basically doing the equivalent of cat buffer - | gunzip. (Alternatively, non-blocking I/O could be used to avoid the need for the extra process.) –  Ilmari Karonen Dec 17 '13 at 23:57

2 Answers 2

This is not the answer you want to hear, but this is not supported by GNU tar according to its manual:

The only case when you have to specify a decompression option while reading the archive is when reading from a pipe or from a tape drive that does not support random access. However, in this case GNU tar will indicate which option you should use. For example:

$ cat archive.tar.gz | tar tf -
tar: Archive is compressed.  Use -z option
tar: Error is not recoverable: exiting now

If you see such diagnostics, just add the suggested option to the invocation of GNU tar:

$ cat archive.tar.gz | tar tzf -

-- 8.1.1 Creating and Reading Compressed Archives

share|improve this answer
2  
Note this works in the tar command on OSX but not on Linux using GNU tar. So this could be fixed. –  Sean Perry Dec 17 '13 at 22:53
up vote 2 down vote accepted

Finally, I realized the reason I cannot let tar detect and decompress the archive from stdin is that I use GNU tar. BSD tar can do it automatically without problem. So I decide to use bsdtar instead of tar in my script now.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.