Help with archiving in perl.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi all:

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:

use Compress::Zlib;

sub testZipStream()
        my $file="";
        my $x = deflateInit()
       or die "Cannot create a deflation stream\n" ;

    my ($output, $status);

        open (FH, "< somebigfile.bin");

        binmode STDOUT;
        my $gz = gzopen(\*STDOUT, "wb")
          or die "Cannot open stdout: $gzerrno\n" ;
    while (read(FH,$out,4096))
                or die "error writing: $gzerrno\n" ;
        $gz->gzclose ;
    close FH;



when I tried using my program like this: > 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.

Much Thanks,


Re: Help with archiving in perl.

Quoted text here. Click to load it

You don't need to call deflateInit if you are just using gzopen.

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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

For example use something like this if using a string to accumulate the
uncompressed data

  use Archive::Zip;

  my $zip = new Archive::Zip;

  my $data = "some data";
  $data.= "something else";

  $zip->addString($data, "member_name");

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 "",
                                                   Name => "member_name"
      or die "cannot create zip file '': $ZipError\n";

  print $zip "abcde";
  print $zip "fghi";
  print $some_binary_data;

  close $zip;


Site Timeline