I know this is a very old thread, but I just had the same question, to compress the contents of a variable in perl.
I tried the method above, and it seems i can compress the data successfully, BUT i can't decompress it.
Inflate is not working for me on the variable i just compressed.
Googling this doesn't bring up anything else then the normal manual, which is not helpful for me. Doing it that way doesn't work.
Code:
#!/usr/bin/perl
use Compress::Raw::Zlib;
my $version = Compress::Raw::Zlib::zlib_version();
print "zlib version: $version\n";
$uncompressed_text = "this is my uncompressed text, which is tooooo loooooong...";
sub zip {
my ($d, $status) = new Compress::Raw::Zlib::Deflate( -Level => Z_BEST_COMPRESSION );
my $result;
my $status = $d->deflate(shift, $result);
die "deflation failed: $status\n" unless $status == Z_OK;
$status = $d->flush($result);
die "deflation failed: $status\n" unless $status == Z_OK;
return $result;
}
sub unzip {
my ($d, $status) = new Compress::Raw::Zlib::Inflate() or die "Cannot create a inflation stream\n";
my $result;
my $status = $d->inflate(shift, $result);
die "do inflation failed: $status\n" unless $status == Z_OK;
$status = $d->flush($result);
die "flush inflation failed: $status\n" unless $status == Z_OK;
return $result;
}
my $compressed_text = zip($uncompressed_text);
print "compressed: \n$compressed_text \n";
my $redecompressed_text = unzip($compressed_text);
print "decompressed: \n$redecompressed_text\n";
the output is:
Code:
zlib version: 1.2.3
compressed:
+��,V��J�Ҽ��܂�������Ԋ�����
�tI(䀩��t===�Jw
do inflation failed: data error
did anyone ever managed to successfully compress and de-compress a variable ?
Is there a working example somewhere ?
any help would be appreciated...