all.
I want to use gunzip to calculate a string’s crc32 code.
String: teststring
This command works well:
echo -n teststring | gzip -f > /tmp/filename && gunzip -lv /tmp/filename && rm -f /tmp/filename
But this command can’t work:
echo -n teststring | gzip -f | gunzip -lv
I searched all the gunzip manual, and find this “The –list option reports sizes as -1 and crc as ffffffff if the compressed file is on a non seekable media.”, I want to why the standard output is a non seekable media? How can make this command works well?
Update:
This does’t work:
cat /tmp/filename | gunzip -lv
This works well:
gunzip -lv < /tmp/filename
Can you tell me the difference between these two commands? Thank you!
When you pipe, the input to the receiving executable is simply not seekable. Hence the problem you are experiencing.
Instead you could extract the second-to-last four bytes, which is the crc:
or you could use pigz (a parallel version of gzip that happens to not try to seek for a listing):
In general, this is a rather inefficient way to calculate the crc, since gzip is doing a lot more work to compress than it is to calculate the crc. (I put in the
-1option above to minimize the compression work, but it’s still a lot more.) You should simply write a short program to calculate the crc. Your linux machine almost certainly has the zlib library available. Try to includezlib.hand use-lzto link. You can readzlib.hto see how to use thecrc32()function.The
cksumcommand suggested in a comment can be used to calculate a different crc. If all you want is any crc, then you can use that. If you want the crc used by gzip, zip, png, etc., then you need to use thecrc32()routine from zlib, or equivalent.