Click here to get back home

Help with archiving in perl.

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with archiving in perl. dcatdemon 04-25-2006
Get Chitika Premium
Posted by dcatdemon on April 25, 2006, 9:26 pm
Please log in for more thread options


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.


Posted by Paul Marquess on April 26, 2006, 8:50 am
Please log in for more thread options


> 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



Similar ThreadsPosted
ANNOUNCE: Initial release of WSF/Perl (Perl bindings for a WS-* framework) October 4, 2007, 1:37 am
PLJava - Perl embeded into Java (calling Perl from Java) - 1sr release - call for tests and review, please. July 13, 2004, 4:06 am
Perl MakeMaker - how to force Perl linking with the static C library (libcrt.lib) instead of dynamic C library (msvcrt.lib) April 17, 2007, 5:22 pm
MFC with Perl July 19, 2005, 9:38 pm
Net::SSH::Perl October 23, 2005, 11:16 pm
perl with ASP November 9, 2005, 10:19 am
Perl December 30, 2005, 9:28 pm
perl DBI help March 1, 2006, 10:25 am
Help with Net::SSH::Perl August 25, 2006, 2:26 pm
perl -> VB .NET September 2, 2006, 11:17 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap