Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Help with archiving in perl.
April 26, 2006, 1:26 am
rate this thread
I'm quite new to perl and am trying to create a zip file out of chunks
of binary data.
here's a snippet of my code:
my $x = deflateInit()
or die "Cannot create a deflation stream\n" ;
my ($output, $status);
open (FH, "< somebigfile.bin");
my $gz = gzopen(\*STDOUT, "wb")
or die "Cannot open stdout: $gzerrno\n" ;
or die "error writing: $gzerrno\n" ;
when I tried using my program like this:
test.pl > test.zip
test.zip is created but I can't seem to open it with gunzip.
I'm not too sure if Archive::Zip allows you to create a zip member and
then feed chunks of data read from a file/stream to be compressed and
then create a compressed output stream on the fly. This example above
was done with Compress::Zlib.
I've tried nearly all the Archive::Zip functions, but somehow am
clueless on how to do things... as I'm very new to perl. Any clues
will be much appreciated.
Re: Help with archiving in perl.
You don't need to call deflateInit if you are just using gzopen.
I tried your script on a Linux box and it created a valid gzip file for me.
You aren't runnig on windows by any chance? There are times when writing to
STDOUT like your are can be troublesome on windows.
Archive::Zip manipulates zip files and gzopen creates gzip files. These
aren't the same thing. You will have to write a lot of code if you want to
use Compress::Zlib on its own to manipulate a zip file.
AFAIK Archive::Zip doesn't allow streaming output, so you would either have
to create a string or a temporary file that has all the uncompressed data in
it, then call either addString or addFile to carry out the compression in
For example use something like this if using a string to accumulate the
my $zip = new Archive::Zip;
my $data = "some data";
$data.= "something else";
Alternatively, if you don't mind running beta code, you can use
IO::Compress::Zip (it's part of the IO-Compress-Zlib distribution). This
module does allow streaming output. Below is an example
use IO::Compress::Zip qw($ZipError);
my $zip = new IO::Compress::Zip "my.zip",
Name => "member_name"
or die "cannot create zip file 'my.zip': $ZipError\n";
print $zip "abcde";
print $zip "fghi";