Need compress-zlib example for textfile into .gz

M

Marc Bauer

hi

i searched the net and read the docu of Compress-Zlib. But i do not
understand the Compress-Zlib examples... :-(.

please give me a simple example, to compress a txt file on my disk to .gz -
nothing more.


Regards
Marc
 
P

Paul Lalli

Marc said:
i searched the net and read the docu of Compress-Zlib. But i do not
understand the Compress-Zlib examples... :-(.

It would be most helpful if you could explain *what* you don't
understand about them. That way, we can help you to learn.
please give me a simple example, to compress a txt file on my disk to .gz -
nothing more.

Since I'd never used this module before either, I consider this a good
learning exercise for myself. The below seems to work, gzipping any
file provided on the command line (but not removing the original, as
the actual gzip program does):

#!/usr/bin/perl
use strict;
use warnings;
use Compress::Zlib;

@ARGV > 0 or die "Usage: $0 <file1> [<file2> [<file3> ... ] ]\n";

for my $file (@ARGV) {
open my $fh, '<', $file or
warn "Could not open '$file': $!\n" and next;
my $gz = gzopen("$file.gz", "w") or die "Cannot open $file.gz: $!";

while (<$fh>) {
$gz->gzwrite($_);
}
$gz->gzclose();
}
 
M

Marc Bauer

hi

i tryed this and it produces corrupt files.


use Compress::Zlib;

my $tmpdir='./tmp';
my $outputdir='.';

my $inputfile="$tmpdir/test.txt";
my $outputfile="$outputdir/test.txt.gz";

binmode FILE;
open (FILE, '<', $inputfile) or die "Could not open $inputfile: $!\n";

my $gz = gzopen($outputfile, "w") or die "Cannot open $outputfile:
$gzerrno\n";

while (<FILE>) {
$gz->gzwrite($_) or die "error writing: $gzerrno\n" ;
}
$gz->gzclose();

close (FILE);



Regards
Marc
 
P

Paul Lalli

Marc said:
hi

i tryed this and it produces corrupt files.

What you posted worked perfectly for me. It created test.txt.gz in the
current directory. I then gunzipped that file and saw my original
file.

By what means are you determining that the files it produces are
"corrupt"?

Paul Lalli
 
T

Tad McClellan

i tryed this


You should always enable warnings when developing Perl code!

and it produces corrupt files.


And perl will tell you why, but only if you ask it to.

use Compress::Zlib;

my $tmpdir='./tmp';
my $outputdir='.';

my $inputfile="$tmpdir/test.txt";
my $outputfile="$outputdir/test.txt.gz";

binmode FILE;


binmode() on unopened filehandle FILE at ...

open (FILE, '<', $inputfile) or die "Could not open $inputfile: $!\n";


binmode() the filehandle after you actually _have_ a filehandle,
ie. _after_ a successful open().
 
M

Marc Bauer

What you posted worked perfectly for me. It created test.txt.gz in the
current directory. I then gunzipped that file and saw my original
file.

By what means are you determining that the files it produces are
"corrupt"?

if i'm unzipping the file it tells me error in archive...

this textfile created one the fly... just in the same script. but i have
closed the test.txt 5 lines befor. so it shouldn't be possible that this
file is in access...

i'm using ActivePerl 5.8.8 on Windows... i will try the small script i
posted with a different text file and see if this will change something...
but this is no help...


Marc
 
M

Marc Bauer

hi
You should always enable warnings when developing Perl code!

yes... i know and i have, but my PC at work haven't warned me...
And perl will tell you why, but only if you ask it to.

And how? i have CRC errors...
binmode() the filehandle after you actually _have_ a filehandle,
ie. _after_ a successful open().

i change this, but the CRC stay there...


Regards
Marc
 
P

Paul Lalli

Marc said:
if i'm unzipping the file it tells me error in archive...

Are you 'unzipping', or *g*unzipping?
this textfile created one the fly... just in the same script. but i have
closed the test.txt 5 lines befor. so it shouldn't be possible that this
file is in access...

So in other words, the code you posted that you claimed didn't work
*isn't* the actual code you're using. I'm growing less and less
interested in helping you. Have you read the posting guidelines for
this group yet?
i'm using ActivePerl 5.8.8 on Windows... i will try the small script i
posted with a different text file and see if this will change something...
but this is no help...

You've been given a *lot* of help, despite your own reluctance to make
it easy to help you. You should be a lot more grateful than you are
right now.

Paul Lalli
 
M

Marc Bauer

my textfile is 12MB in size... once i saw the problem after the 10.000 line
in the textfile... do i need to buffer something here? how is this done?

Marc
 
M

Marc Bauer

hi
Are you 'unzipping', or *g*unzipping?

i compress a text file to .gz - therefor gzip-ping.
So in other words, the code you posted that you claimed didn't work
*isn't* the actual code you're using. I'm growing less and less
interested in helping you. Have you read the posting guidelines for
this group yet?

The posted code is the code i'm trying / using. i have extracted this part
and tested this part alone now and it is not working, for sure. everytime a
CRC error!


Regards
Marc
 
J

Josef Moellers

Marc said:
hi

i tryed this and it produces corrupt files.


use Compress::Zlib;

my $tmpdir='./tmp';
my $outputdir='.';

my $inputfile="$tmpdir/test.txt";
my $outputfile="$outputdir/test.txt.gz";

binmode FILE;
open (FILE, '<', $inputfile) or die "Could not open $inputfile: $!\n";

Hmmm, dunno ... I always use it the other way round:

open(my $fh, ...);
binmode $fh;

Remember:

use strict;
use warnings;

would have told you

binmode() on unopened filehandle FILE at - line ...
 
M

Marc Bauer

hi

so, for all interessed in this. here is the solution for the problem...

[gzip-file.pl]
# use module
use strict;
use warnings;
use Compress::Zlib;

# minimum specify "./" for current directory
my $tmpdir='./tmp';
my $outputdir='.';


my $inputfile="$tmpdir/test.txt";
my $outputfile="$outputdir/test.txt.gz";

open (INFILE, "<$inputfile") or die "Could not open $inputfile: $!\n";
binmode INFILE;

open (OUTFILE, ">$outputfile") or die "Could not open $outputfile: $!\n";
binmode OUTFILE;

my $gz = gzopen(\*OUTFILE, "w") or die "Cannot open $outputfile:
$gzerrno\n";

while (<INFILE>) {
$gz->gzwrite($_) or die "error writing: $gzerrno\n";
}

$gz->gzclose() == 0 or die "error closing $gzerrno\n";
close (INFILE);
close (OUTFILE);



Regards
Marc
 
D

Dr.Ruud

Marc Bauer schreef:
# minimum specify "./" for current directory

ITYM '.'

open (INFILE, "<$inputfile") or die "Could not open $inputfile: $!\n";

I would make that:

open my $in_fh, '<', $in_name or die "open $in_name, stopped $!" ;

or if you don't like line numbers:

open my $in_fh, '<', $in_name or die "open $in_name: $!\n" ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top