niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Need compress-zlib example for textfile into .gz
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
Author Message
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 1:48 pm    Post subject: Need compress-zlib example for textfile into .gz Reply with quote

hi

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

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


Regards
Marc
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Thu Jul 20, 2006 2:19 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Marc Bauer wrote:
Quote:
i searched the net and read the docu of Compress-Zlib. But i do not
understand the Compress-Zlib examples... Sad.

It would be most helpful if you could explain *what* you don't
understand about them. That way, we can help you to learn.

Quote:
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();
}
Back to top
Todd W
*nix forums beginner


Joined: 10 Feb 2005
Posts: 37

PostPosted: Thu Jul 20, 2006 2:25 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

"Marc Bauer" <marc.bau@gmx.net> wrote in message
news:44bf89b4$0$31181$bb690d87@news.main-rheiner.de...
Quote:
hi

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

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


Do the programs in the examples directory that come with the distro not
suffice:

http://search.cpan.org/src/PMQS/Compress-Zlib-1.42/examples/

?

Todd W.
Back to top
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 3:48 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

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
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Thu Jul 20, 2006 4:48 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Marc Bauer wrote:
Quote:
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
Back to top
Tad McClellan
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1647

PostPosted: Thu Jul 20, 2006 6:24 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Marc Bauer <marc.bau@gmx.net> wrote:


Quote:
i tryed this


You should always enable warnings when developing Perl code!


Quote:
and it produces corrupt files.


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


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


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


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


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Back to top
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 6:40 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Quote:
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
Back to top
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 7:07 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

hi

Quote:
You should always enable warnings when developing Perl code!

yes... i know and i have, but my PC at work haven't warned me...

Quote:
and it produces corrupt files.
And perl will tell you why, but only if you ask it to.

And how? i have CRC errors...

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

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


Regards
Marc
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Thu Jul 20, 2006 7:09 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Marc Bauer wrote:
Quote:
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...

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

Quote:
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?

Quote:
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
Back to top
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 7:30 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

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
Back to top
Marc Bauer
*nix forums beginner


Joined: 20 Jul 2006
Posts: 6

PostPosted: Thu Jul 20, 2006 7:40 pm    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

hi

Quote:
Are you 'unzipping', or *g*unzipping?

i compress a text file to .gz - therefor gzip-ping.

Quote:
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
Back to top
Josef Moellers
*nix forums Guru


Joined: 28 Feb 2005
Posts: 426

PostPosted: Fri Jul 21, 2006 6:32 am    Post subject: Re: Need compress-zlib example for textfile into .gz Reply with quote

Marc Bauer wrote:
Quote:
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 ...

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 3:19 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Problem to arrange a compress on the fly to an export etantonio@libero.it Server 3 Tue Jul 18, 2006 10:23 am
No new posts Problem to arrange a compress on the fly to an export etantonio@libero.it Tools 1 Tue Jul 18, 2006 10:21 am
No new posts Problem to arrange a compress on the fly to an export etantonio@libero.it Oracle 3 Tue Jul 18, 2006 10:19 am
No new posts Can shareplex compress the information when it replicates... wangbinlxx@gmail.com Server 1 Wed Jul 12, 2006 9:52 am
No new posts COMPRESS .. & VALUES COMPRESSION change request: RFC. Konstantin Andreev IBM DB2 7 Thu Jul 06, 2006 11:52 am

Credit Report | WoW Gold | Myspace Proxy | Literatura fantastica | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2350s ][ Queries: 16 (0.0966s) ][ GZIP on - Debug on ]