Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
go ahead... be a heretic
 
PerlMonks  

Re: examples using Compress::Zlib ?

by grinder (Bishop)
on Sep 10, 2001 at 13:03 UTC ( [id://111439]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to examples using Compress::Zlib ?

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

Replies are listed 'Best First'.
Re: Re: examples using Compress::Zlib ?
by idnopheq (Chaplain) on Sep 10, 2001 at 13:33 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://111439]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.