LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2010, 09:11 AM   #1
maxmeier12
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Rep: Reputation: 0
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.
 
Old 06-20-2010, 12:09 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by maxmeier12 View Post
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
 
0 members found this post helpful.
Old 06-20-2010, 12:27 PM   #3
maxmeier12
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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?
 
Old 06-20-2010, 03:07 PM   #4
maxmeier12
LQ Newbie
 
Registered: Jun 2010
Posts: 4

Original Poster
Rep: Reputation: 0
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;
}
 
Old 06-04-2013, 04:50 AM   #5
gubacsek
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
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...
 
  


Reply

Tags
perl, zip



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and writing a file with bash XeroXer Linux - Newbie 7 06-02-2008 08:08 AM
struct reading and writing to file ilnli Programming 8 08-20-2007 09:54 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration