Następna strona Poprzednia strona Spis treści

10. Użycie bzip2-a do rekompresji plików skompresowanych w innych formatach

Ten program w perl-u pobiera plik skompresowany w innych formatach (.tar.gz, .tgz. .tar.Z, and .Z ) i przepakowuje je dla lepszej kompresji. Kod żródłowy w perl-u ma wiele porządnej dokumentacji dotyczącej tego co robi i jak to robi co robi.

#!/usr/bin/perl -w

#######################################################
#                                                     #
# Ten program pobiera pliki skompresowane programami  #
# compress i gzip w aktualnym katalogu i zamienia je  #
# do formatu bzip2. Obsługuje rozdszerzenie .tgz w    #
# prawidłowy sposób, tworząc plik .tar.bz2.           #
#                                                     #
#######################################################
$counter = 0;
$saved_bytes = 0;
$totals_file = '/tmp/machine_bzip2_total';
$machine_bzip2_total = 0;

while(<*[Zz]>) {
    next if /^bzip2-0.1pl2.tar.gz$/;
    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",
      " $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";


Następna strona Poprzednia strona Spis treści