Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

IO::Compress::Gzip blank output

by shug94 (Sexton)
on Sep 06, 2009 at 05:46 UTC ( [id://793747]=perlquestion: print w/replies, xml ) Need Help??

shug94 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks,

I am trying to compress a text file into gzip format using Perl.

I thought that the best way of doing so would be to use IO::Compress::Gzip. However I seem to be getting no useable output, can anyone tell me what I am doing wrong?

Here is my code:
use IO::Compress::Gzip; #... my $localFilePath = 'C:/adi_vsfz/CTIP/transferFiles/20091015_Timetab +le.txt'; my $content; #Open a file handle to the file. if(open(FILE_TO_TRANSFER, $localFilePath)) { my @transferFileContents = <FILE_TO_TRANSFER>; foreach(@transferFileContents) { $content .= $_; } close(FILE_TO_TRANSFER); my $newContent; IO::Compress::Gzip::gzip($content, $newContent); print("<BR><BR>\n".$content."<BR><BR>\n"); print($newContent."<BR><BR>".length($newContent)."\n"); if($newContent == undef) { print('$newContent is undef'."<BR>\n"); } }
The output I get is:
Timetable Info 0 $newContent is undef


Any idea what I am doing wrong?
I hope you can help,

Cheers,

Shug

Replies are listed 'Best First'.
Re: IO::Compress::Gzip blank output
by Gangabass (Vicar) on Sep 06, 2009 at 06:39 UTC

    First always start your programs with this two lines:

    use warnings; use strict;

    And second always check results of open...

      I am using use strict, but I will add use warnings and see what that gives me. I am also checking the result of open, I just copied a snippet of my code to make it clearer.
Re: IO::Compress::Gzip blank output
by Sewi (Friar) on Sep 06, 2009 at 12:11 UTC
    You may want to check the return value from IO::Compress::Gzip::gzip and maybe $! and $@.
    Personally, I prefer Compress::Zlib which is also doing Gzip, but this shouldn't make any differenct for your problem.
Re: IO::Compress::Gzip blank output
by graff (Chancellor) on Sep 07, 2009 at 02:46 UTC
    You could also try a much simpler method, involving PerlIO::gzip:
    #!/usr/bin/perl use strict; use warnings; use PerlIO::gzip; my $Usage = "Usage: $0 filename-to-compress\n"; die $Usage unless ( @ARGV == 1 and -f $ARGV[0] ); open( IN, "<", $ARGV[0] ) or die "$ARGV[0]: $!\n"; open( GZIP, ">:gzip", "$ARGV[0].gz" ) or die "$ARGV[0].gz: $!\n"; $/ = undef; $_ = <IN>; print GZIP;
    If you want to use this on an input file that is too big to fit in memory all at once, just use the normal I/O idiom with a while loop -- the result will be the same:
    # $/ = undef; # leave $/ at it's default value... while (<IN>) { print GZIP; }

    UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).

Re: IO::Compress::Gzip blank output
by Burak (Chaplain) on Sep 07, 2009 at 00:27 UTC
    The docs clearly state that parameters can not be bare scalars (unless they are file paths, which is not the case for you). Try this:
    use IO::Compress::Gzip qw(gzip $GzipError) # other code... IO::Compress::Gzip::gzip(\$content, \$newContent) or die "gzip failed: + $GzipError\n"
    Also, you're doing the file slurp in a wrong and slow way:
    my @transferFileContents = <FILE_TO_TRANSFER>; foreach(@transferFileContents) { $content .= $_; }
    It is possible to write this as:
    $content = do { local $/; my $rv = <FILE_TO_TRANSFER>; $rv };
    which means:
    $content = do { # minimize scope local $/; # make input separator undef 'till the end of this scope my $rv = <FILE_TO_TRANSFER>; # get all contents $rv # return it };
      Thankyou very much!

      I will try using references instead. I have only been using Perl for three weeks, and I am getting the hang, but some things are still not clear.

      Thanks for helping out.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://793747]
Approved by Gangabass
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-04-19 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found