Friday, August 27, 2010

Z to gz On The Fly

If you have a lot of compress files (*.Z) and you want to convert them to gzip format to space disk space, here is the function that you can use. The function, Z2gz, make use of standard input and standard output to dynamically uncompress and gzip on the fly to avoiding any intermediate file to be created. Also, it will set the permission, ownership and timestamp to be the same as the original file.

Z2gz()
{
for Z in *.Z
do
    gz="${Z%.Z}.gz"
    uncompress < $Z | gzip > $gz
    touch -r $Z $gz
    getfacl $Z | setfacl -f - $gz
    rm -f $Z
done
}

Z2gz in action. In this exercise, we saved 11,970,771 bytes

$ ls -l *.Z
-rw-------   1 chihung   chihung     5278987 Feb  2  2009 apache.tar.Z
-rw-r-----   1 chihung   chihung     1479993 Aug 27 09:29 freetype-2.3.1-sol10-sparc-local.Z
-rw-r-----   1 chihung   chihung     25318755 Feb  2  2009 ganglia.tar.Z
-rw-r-----   1 chihung   chihung      797471 Aug 27 09:29 libgcc-3.4.6-sol10-sparc-local.Z
-rw-r--r--   1 chihung   chihung     4039745 Feb 19  2009 mediawiki-1.6.12.tar.Z

$ Z2gz

$ ls -l *.gz
-rw-------   1 chihung   chihung     3641373 Feb  2  2009 apache.tar.gz
-rw-r-----   1 chihung   chihung     1019829 Aug 27 09:29 freetype-2.3.1-sol10-sparc-local.gz
-rw-r-----   1 chihung   chihung     16940517 Feb  2  2009 ganglia.tar.gz
-rw-r-----   1 chihung   chihung      527891 Aug 27 09:29 libgcc-3.4.6-sol10-sparc-local.gz
-rw-r--r--   1 chihung   chihung     2814570 Feb 19  2009 mediawiki-1.6.12.tar.gz

Labels: ,

3 Comments:

Blogger Ah Choo said...

But what is the benefit of the zip format over the gzip?

12:23 PM  
Blogger chihungchan said...

compress is a very old compression utility. It's compression ratio and time taken to compress are always not as good as the newer utilities (gzip, bzip2, zip,...).

This link does a thorough comparison. It is a trade off between compression ratio and time.

Here is another comparison

5:42 PM  
Blogger chihungchan said...

You may consider parallel gzip that can take advantage of multi-cores. We use this utility to compress crash dump which is basically very big (10GB is not uncommon). By default, it spawn off 8 threads to gzip.

5:48 PM  

Post a Comment

<< Home