|
Next
Previous
Contents
10. Using bzip2 to recompress other compression formatsThe following perl program takes files compressed in other formats (.tar.gz, .tgz. .tar.Z, and .Z for this iteration) and repacks them for better compression. The perl source has all kinds of neat documentation on what it does and how it does what it does. This latest version takes files as input on the command line. Without command line arguments, it tries to repack every file in the current working directory.
#!/usr/bin/perl -w
#######################################################
# #
# This program takes compressed and gzipped programs #
# in the current directory and turns them into bzip2 #
# format. It handles the .tgz extension in a #
# reasonable way, producing a .tar.bz2 file. #
# #
#######################################################
$counter = 0;
$saved_bytes = 0;
$totals_file = '/tmp/machine_bzip2_total';
$machine_bzip2_total = 0;
@raw = (defined @ARGV)?@ARGV:<*>;
foreach(@raw) {
next if /^bzip/;
next unless /\.(tgz|gz|Z)$/;
push @files, $_;
}
$total = scalar(@files);
foreach (@files) {
if (/tgz$/) {
($new=$_) =~ s/tgz$/tar.bz2/;
} else {
($new=$_) =~ s/\.g?z$/.bz2/i;
}
$orig_size = (stat $_)[7];
++$counter;
print "Repacking $_ ($counter/$total)...\n";
if ((system "gzip -cd $_ |bzip2 >$new") == 0) {
$new_size = (stat $new)[7];
$factor = int(100*$new_size/$orig_size+.5);
$saved_bytes += $orig_size-$new_size;
print "$new is about $factor% of the size of $_. :",($factor<100)?')':'(',"\n";
unlink $_;
} else {
print "Arrgghh! Something happened to $_: $!\n";
}
}
print "You've "
, ($saved_bytes>=0)?"saved ":"lost "
, abs($saved_bytes)
, " bytes of storage space :"
, ($saved_bytes>=0)?")":"("
, "\n"
;
unless (-e '/tmp/machine_bzip2_total') {
system ('echo "0" >/tmp/machine_bzip2_total');
system ('chmod', '0666', '/tmp/machine_bzip2_total');
}
chomp($machine_bzip2_total = `cat $totals_file`);
open TOTAL, ">$totals_file"
or die "Can't open system-wide total: $!";
$machine_bzip2_total += $saved_bytes;
print TOTAL $machine_bzip2_total;
close TOTAL;
print "That's a machine-wide total of ",`cat $totals_file`," bytes saved.\n";
Next Previous Contents Linux HOWTO full list |
|
This document, LDP HOWTO-INDEX, is copyrighted (c) 1995 - 2002 by Tim Bynum, Guylhem Aznar, Joshua Drake and Greg Ferguson. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is available at http://www.gnu.org/copyleft/fdl.html. If you have questions, please contact the LDP.
Web Design Copyright © 1999-2003. Chrisranjana Software Solutions Pvt Ltd. syndicate rss feed |