LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack 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,123

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
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;
}
 
  


Reply

Tags
perl, zip


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 03:34 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration