LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to zip a variable with PERL without reading or writing a file? (http://www.linuxquestions.org/questions/programming-9/how-to-zip-a-variable-with-perl-without-reading-or-writing-a-file-815273/)

maxmeier12 06-20-2010 09:11 AM

How to zip a variable with PERL without reading or writing a file?
 
There is the Archive::Zip I think I can use with Perl 5.10 but I don't know how.
I don't want to read or write any files, just zip something in memory, with best compression, like

$text = "this is a test";
$zippedtext = &Zip($text);

sub Zip {
my $zip = Archive::Zip->new();
# help needed here
return ...
}

I guess it's only a few lines.

Thanks in advance.

Sergei Steshenko 06-20-2010 12:09 PM

Quote:

Originally Posted by maxmeier12 (Post 4009332)
There is the Archive::Zip I think I can use with Perl 5.10 but I don't know how.
I don't want to read or write any files, just zip something in memory, with best compression, like

$text = "this is a test";
$zippedtext = &Zip($text);

sub Zip {
my $zip = Archive::Zip->new();
# help needed here
return ...
}

I guess it's only a few lines.

Thanks in advance.

http://search.cpan.org/~leont/File-M...ib/File/Map.pm

maxmeier12 06-20-2010 12:27 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4009440)

Well, this links to memory mapped files, but all I want to do is just call the Compress method from the zip library. That's pretty easy in C so I thought it might be possible also with Perl.

The point is I don't need all that extra information which is included in a typical zip file, such as file size and name etc.

If I do something like

my $zip = Archive::Zip->new();
my $member = $zip->addString($text, $filename);
$member->desiredCompressionMethod(COMPRESSION_DEFLATED);

how can I access the zipped member and is it possible to get rid of the $filename stuff?

maxmeier12 06-20-2010 03:07 PM

Found what I was looking for in a different module:

use Compress::Raw::Zlib;

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\n" unless $status == Z_OK;

$status = $d->flush($result) ;
die "deflation failed\n" unless $status == Z_OK;

$result;
}

gubacsek 06-04-2013 04:50 AM

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...


All times are GMT -5. The time now is 03:15 AM.