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.

Is there a way to print the decompressed size of a .bz2 file without actually decompressing the entire thing?

share|improve this question
    
So there is no metadata about the original file in the bzip output? >:( –  endolith Oct 11 '09 at 22:30
    
not that i've seen reference to. :/ –  quack quixote Oct 11 '09 at 22:35
add comment

3 Answers

up vote 10 down vote accepted

As noted by others, bzip2 doesn't provide much information. But this technique works -- you will have to decompress the file, but you won't have to write the decompressed data to disk, which may be a "good enough" solution for you:

$ ls -l foo.bz2
-rw-r--r-- 1 ~quack ~quack 2364418 Jul  4 11:15 foo.bz2

$ bzcat foo.bz2 | wc -c         # bzcat decompresses to stdout, wc -c counts bytes
2928640                         # number of bytes of decompressed data

You can pipe that output into something else to give you a human-readable form:

$ ls -lh foo.bz2
-rw-r--r-- 1 quack quack 2.3M Jul  4 11:15 foo.bz2

$ bzcat foo.bz2 | wc -c | perl -lne 'printf("%.2fM\n", $_/1024/1024)'
2.79M
share|improve this answer
2  
Well, that only took five minutes of 100% CPU to calculate. –  endolith Oct 11 '09 at 22:39
    
only? AND it would fill up a disk? i've got a compressed tarball of an old linux install that's only 407meg yet took my poor ancient server 30-45 minutes to extract. that included writing to disk, tho, i'll have to run that script to time it. get back to ya in half an hour... :) –  quack quixote Oct 11 '09 at 23:12
    
I picked the smallest file for the first test, of course. 140 MB compressed --> 3 GB uncompressed. The larger files are 5 GB compressed... –  endolith Oct 12 '09 at 4:50
    
heh .. lemme know how big the 5GBs turn out to be... and how long it takes to figure it out via this XD –  quack quixote Oct 12 '09 at 5:25
add comment

Given the fact that bzip2 can compress a stream, it means that it doesn't write in the output file a header with the original size. Of course, it could write something at the end of the output, but I doubt it.

share|improve this answer
    
gzip can compress streams too. And yes, it appends the filesize info at the footer –  syockit May 10 '11 at 18:59
    
@syockit: you're right about gzip according to Wikipedia, but I think it will work only if you use it like this: gzip file and not like this: gzip < file > file.gz. –  Cristian Ciupitu Jun 22 '11 at 12:04
2  
@CristianCiupitu Actually, gzip will store the file size in both cases. In the second case, gzip can't store the filename. You'll get the exact same output from each command if you add -n to avoid storing the name. –  Michael Mior Aug 14 '13 at 19:20
add comment

The program bzip2 unfortunately doesn't have a list parameter.

If a programmer, I suppose you would need to use libbzip2 to write a small program for doing it.

share|improve this answer
add comment

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.