Help with archiving in perl.

Help with archiving in perl.

am 26.04.2006 03:26:28 von dcatdemon

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;
testZipStream();

sub testZipStream()
{
my $file="test.zip";
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))
{
$gz->gzwrite($out)
or die "error writing: $gzerrno\n" ;
}
$gz->gzclose ;
close FH;

}

---

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.

Much Thanks,

caleb.

Re: Help with archiving in perl.

am 26.04.2006 14:50:58 von Paul Marquess

"dcatdemon" wrote in message
news:1146014788.403104.197830@j33g2000cwa.googlegroups.com.. .
> 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;
> testZipStream();
>
> sub testZipStream()
> {
> my $file="test.zip";
> my $x = deflateInit()
> or die "Cannot create a deflation stream\n" ;

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

>
> 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))
> {
> $gz->gzwrite($out)
> or die "error writing: $gzerrno\n" ;
> }
> $gz->gzclose ;
> close FH;
>
> }
>
> ---
>
> 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 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.

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

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");
$zip->writeToFileNamed("my.zip");

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";
print $some_binary_data;

close $zip;

Paul