ybiC has asked for the wisdom of the Perl Monks concerning the following question:

I want to replace a Bash (tar/gzip) script with Perl to archive files on my Debian workstation. I've installed Compress::Zlib, and used pod2html to peruse the included docs and examples.

Can someone point me to other, more explanatory examples of how to use Compress::Zlib ?

Replies are listed 'Best First'.
Re: examples using Compress::Zlib ?
by btrott (Parson) on Jun 12, 2000 at 08:21 UTC
    Apache::GzipChain (on CPAN) uses Compress::Zlib. You could look at its source.

    Did you see the examples in the Compress::Zlib docs? They have some examples of using gzopen, gzwrite, gzread, etc.

Re: examples using Compress::Zlib ?
by lhoward (Vicar) on Jun 12, 2000 at 16:11 UTC
    Here's a fragment showing the use of Compress::Zlib to write a compressed file. I've stripped off all the code that doesn't have anything to do w/ Zlib. This extract is from a script I wrote to compress/split web server logs in real-time.
    use Compress::Zlib; my $outlog=gzopen($fn,"a"); .... $outlog->gzwrite($line."\n"); .... $outlog->gzclose();
RE: examples using Compress::Zlib ?
by t0mas (Priest) on Jun 12, 2000 at 10:10 UTC
    I guess it isn't very explanatory, but I'll recycle an old example to save the rain forests...
    Take a look here for a working example.

    /brother t0mas
Re: examples using Compress::Zlib ?
by grinder (Bishop) on Sep 10, 2001 at 17:03 UTC

    I was going to post my own SoPW on my troubles at getting Compress::Zlib to work, and Super Search pointed me here. Based on the code samples here, I was able to get my test case (a line-oriented zip/unzip) to work correctly.

    Specifically, my problem was that I was creating a $gz handle using deflateInit(). So here's my code for future readers in the same predicament:

    #! /usr/bin/perl -w use strict; use Compress::Zlib; my $file = shift || 'testdata.gz'; my $gz = gzopen( $file, 'wb' ) or die "Cannot open $file for gzwrite +: $gzerrno\n"; my $line; while( defined($line = <DATA>) ) { $gz->gzwrite( $line ) or die "Could not write gzipped data to $file: $gzerrno\n"; } $gz->gzclose(); $gz = gzopen( $file, 'rb' ) or die "Cannot open $file for gzread: $gze +rrno\n"; while( $gz->gzreadline($line) > 0 ) { print $line; } die "Error reading from $file: [$gzerrno]\n" unless Z_STREAM_END == $g +zerrno; $gz->gzclose(); __DATA__ foo bar Judge my vow, sphinx of black quartz __END__

    I think it's pretty self-explanatory...

    This does leave me with one question, however, and that is, how can I specify the that the writer uses different compression strategies, à la Z_BEST_SPEED and Z_BEST_COMPRESSION?

    update: in reply to idnopheq's suggestion, despite have 512Mb RAM, I'd rather not have this hanging around in RAM, I actually want to dump it straight out onto tape. I initially used a deflateInit approach and wrote the output to a file myself, but I was unable to successfully read it back using gzopen on the resulting file.

    --
    g r i n d e r
      If you go with the in-memory compression/decompression odel, you have more flexibility. From the perldoc for the deflateInit call:

      -Level Defines the compression level. Valid values are 1 through 9, Z_BEST_SPEED, Z_BEST_COMPRESSION, and Z_DEFAULT_COMPRESSION. The default is -Level =>Z_DEFAULT_COMPRESSION.

      So then:

      deflateInit( -Bufsize => 300, -Level => Z_BEST_SPEED ) ;

      Then IIRC write the buffer to disk.

      HTH
      --
      idnopheq
      Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Re: examples using Compress::Zlib ? (thanks)
by ybiC (Prior) on Jun 12, 2000 at 19:18 UTC
    Many thanks to btrott, t0mas and lhoward.
    Your suggestions help a bunch.
        cheers,
        ybiC

    ##################################################

    mostly stylistic (and hopefully constructive) suggestions re "Simple WebServer Scanner"

    #!/usr/bin/perl -w # swesc.pl use strict; use warnings; use Socket; my $remote = shift; my $port = 80; Usage() unless($remote); if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } my $iaddr = inet_aton($remote) or die "Error with inet_aton: +$!"; my $paddr = sockaddr_in($port, $iaddr) or die "Error with sockaddr_in +: $!"; my $proto = getprotobyname('tcp') or die "Error with getprotobyn +ame: $!"; my $submit = "HEAD / HTTP/1.0\r\n\r\n"; socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "Error with socket: +$!"; connect(SOCK, $paddr) or die "cannot connect to $remote: $!"; send(SOCK,$submit,0); while(<SOCK>) { if($_ =~ /Server: (.*)/) { print "\n$_"; } } close(SOCK); ###################################################################### +#### sub Usage { print "\nUsage : $0 <remote host>\n"; exit(); } ###################################################################### +#### =head1 Mostly stylistic suggestions, hopefully constructive: X -w on shebang line or use warnings; X use strict; unless ($remote... instead of if ($ARGV[0]... Don't need die "no port specified"... line Rename "affichage" to "Usage" Mixed-case subroutine names Use "or" instead of "||" in die statements Use $! in all die messages Consistant die messages my $remote = shift; =head1 good monk Asmo's original code: #!/usr/bin/perl use Socket; use Strict; use warnings; my $remote = $ARGV[0]; my $port=80; if ($ARGV[0] eq "") { affichage(); } sub affichage { print "\n\nUsage : $0 <remote host>\n"; exit(); } $submit = "HEAD / HTTP/1.0\r\n\r\n"; if($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "no port specified" unless $port; my $iaddr = inet_aton($remote) || die "$remote"; my $paddr = sockaddr_in($port, $iaddr) || die "Caca !!"; my $proto = getprotobyname('tcp') || die "protocol !!"; socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "cannot open socket + : $!"; connect(SOCK, $paddr) || die "cannot connect to $remote: $!"; send(SOCK,$submit,0); while(<SOCK>) { if($_ =~ /Server: (.*)/) { print $_; } } close(SOCK); =cut