3

I'm using the old ncompress to create a backup file. And what I see is amazing:

[root@centos6 home]# ll -l -b mytest*
-rw-r--r--. 1 root root 1073741824 Mar  8 13:41 mytest.iso
-rw-r--r--. 1 root root      88099 Mar  9 10:26 mytest.iso.bak.Z

Here is the command I used:

compress -c mytest.iso>mytest.iso.bak.Z

The size of mytest.iso (which actually is centos6.8-livecd.iso) changed from 1073741824 (1.0G) to 88099 (87K).

But when I use:

tar -cvf mytest.iso.tar.gz mytest.iso

the tar.gz file doesn't change a lot in size:

-rw-r--r--. 1 root root 1073745920 Mar  9 10:43 mytest.tar.gz

Is this normal? The iso file works just fine if I uncompress it.

1
  • It's not surprising that your tar example doesn't make the file much smaller (in fact it makes it larger), because tar -cvf won't do any compression. (The tar utility is not a compression program, though some versions - eg GNU tar - support compression with options such as -z or -j.) – psmears Apr 28 '17 at 15:49
3

Compression algorithms have varying compression ratios depending on the properties of the data they are compressing. For instance:

$ dd if=/dev/zero of=test.img bs=1m count=1024
$ compress -c test.img > test.img.Z
$ gzip -c test.img > test.img.gz
$ wc -c test.img test.img.gz test.img.Z
 1073741824 test.img
    4685486 test.img.gz
      84781 test.img.Z
 1078512091 total

Having a file made up of mostly repeated zeroes is probably a best-case situation for this algorithm. Since you are getting similar compression ratios, and since your file is of such a round size (1GB), it is likely that the image is much larger than necessary and merely filled with repeated data.


Of course, gzip, compress, bzip2, and others will all have differing compression ratios on a given file. This is why many large open-source projects offer several downloads compressed by different algorithms — so that users can download the smallest file for which they have a decompression utility.

3
  • I know file size could shrink a lot when there is repeated 0 or 1.But unlike compress, gzip gives an even much bigger file... – FrontENG Mar 9 '17 at 3:14
  • Actually, you didn't compress your tar file. I have a feeling gzip would get you a fairly small output file as well if you had used it. Your tar command actually increased the file size (by placing tar headers around the single file). You actually want gzip -c mytest.iso > mytest.iso.gz or tar -cvzf mytest.iso.tar.gz mytest.iso (Note the z) – Fox Mar 9 '17 at 3:21
  • I get a result of 1021K similiar to the compress result of ncompress by adding -z.Thank you! – FrontENG Mar 9 '17 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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