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

hi monks,

I have a string of text and I want to save to disk in a compressed format.

I have had a look at Compress::Zlib and I have searched Perlmonks and the net for a simple example of writing a string to a file in ZIP or compressed format of some sort.

Everyone and every example appears to be based on reading ZIP files not writting them.. apart from the compress:zlib itself which is I cant make much sense of.

This is the kind of thing Im hoping I can do but Im obviously doing something wrong:

my $string = "This is a test string"; my $x = deflateInit() or die "Cannot do that\n"; open(OUT,">dmp.zip"); print OUT $x->deflate($string); close(OUT);

Do I really need to flush it?
Should I binmode the OUT file?
Does running it on a Windows box make any difference?
It appears as if Compress::Zlib is a pretty low level module and no doubt there are others on top that simplfy its use, but this then relies on the server I run it on having that module - something Im trying to avoid if possible.

Thanks for any advice you can give

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com/cow <-- Visit my Creative Object World

Replies are listed 'Best First'.
Re: Compress zlib and friends
by monkey_boy (Priest) on Jul 15, 2005 at 09:25 UTC
    I'm not familiar with that module, but i like to use PerlIO::gzip,
    It uses the gzip format, but can transparently be used for reading & writing files.
    e.g:
    use PerlIO::gzip; open( my $fh , ">:gzip", "file.txt.gz") || die $!; print $fh "hello!\n"; close($fh); open( my $fh2 , "<:gzip", "file.txt.gz") || die $!; print <$fh2>; close($fh2);

    I've never tried it on windows, but it doesnt mention any caveats in the docs.
    Of course, if this isnt installed on your server, it may be of no use at all!


    This is not a Signature...
      I'm not familiar with that module, but i like to use PerlIO::gzip
      Sounds like a great module to know about. It would be nice if eventually this made into a core layer...
Re: Compress zlib and friends
by blazar (Canon) on Jul 15, 2005 at 09:24 UTC
    I'm not really sure, but the overall impression is that Compress::Zlib is a relatively low level interface to (all the bells and whistles of) zlib, whereas you may want a higher level interface, e.g. Archive::Zip.