Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Compressing Data On The Fly

by awohld (Hermit)
on May 15, 2005 at 04:31 UTC ( #457159=perlquestion: print w/replies, xml ) Need Help??

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

I'm making a script that will take huge log files (some times 200MB+) and store them in a directory with a time encoded name and compressed. I got the program to store and rename the files but I'm sort of confused on how to compress them. I'm encoding the file names with epoch time but right now the are uncompressed. I want them to be compressed when I store them.

Here is my code so far:

#!/usr/bin/perl -w use strict; use CGI; use DBI; use CGI::Carp qw(fatalsToBrowser); my $upload_dir = "/home/local/upload"; my $query = new CGI; my $filename = $query->param("filename"); my $to = $query->param("to"); my $from = $query->param("from"); my $expire = $query->param("expire"); my $comments = $query->param("comments"); my $uldate = time; my $expdate; if ($expire !~m/^(1|2|3|4|5|6|7|8|9|10|11|12|13|14)$/) { print "Content-type: text/html\n\nDon't tamper with me!"; die; } if ($expire eq 1) {$expdate = $uldate + 86400;} if ($expire eq 2) {$expdate = $uldate + 172800;} if ($expire eq 3) {$expdate = $uldate + 259200;} $filename =~ s/.*[\/\\](.*)/$1/; ##Start database connections############################### my $database = "databox"; my $db_server = "localhost"; my $user = "user"; my $password = "pass"; ##Connect to database, insert statement, & disconnect ##### my $sth; my $dbh = DBI->connect("DBI:mysql:$database:$db_server",$user,$passwor +d); my $statement = "INSERT INTO databox (filename,data_to,data_from,comme +nts,uldate,expdate) VALUES (?,?,?,?,?,?)"; $sth = $dbh->prepare($statement) or die "Couldn't prepare the query +: ".$DBI::errstr; my $rv = $sth->execute($filename,$to,$from,$comments,$uldate,$expdate) + or die "Couldn't execute query: ".$DBI::errstr; $sth->finish; $dbh->disconnect; ########################################################### my $upload_filehandle = $query->upload("filename"); open UPLOADFILE, ">$upload_dir/$uldate"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;
I need some direction on how I should compress the file. I looked at Compress::Zlib and think I should use this module.

I'm not too sure from the documentation on Compress::Zlib on how to apply this to my code. How should I integrate Compress::Zlib into my code? I need to apply Compress::Zlib to compress the log file before it's saved on the server, and do it on the fly.

I'm guessing I should change:
my $upload_filehandle = $query->upload("filename"); open UPLOADFILE, ">$upload_dir/$uldate"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; }
To
my $upload_filehandle = $query->upload("filename"); open UPLOADFILE, ">$upload_dir/$uldate"; binmode UPLOADFILE; my $x = deflateInit() or die "Cannot create a deflation stream\n" ; my ($output, $status) ; while ( <$upload_filehandle> ) { ($output, $status) = $x->deflate($upload_filehandle) ; $status == Z_OK or die "deflation failed\n" ; print UPLOADFILE; }
I'm not really sure about what to do.

READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: Compressing Data On The Fly
by Zaxo (Archbishop) on May 15, 2005 at 05:24 UTC

    gaal++ has given you my favorite solution to this. Here's detail of how to use it:

    use PerlIO::gzip; open UPLOADFILE, '>:gzip', "$upload_dir/$uldate" or die $!; # . . .
    If you wish, you can add the gzip layer with binmode, instead.

    After Compline,
    Zaxo

      And in reading,

      open my $fh, '<:gzip(autopop)', $file or die $!; This transparently falls back on ungzipped where the file is not compressed. There's a warning in the docs about using it, but in many cases it's an excellent tool.

Re: Compressing Data On The Fly
by gaal (Parson) on May 15, 2005 at 05:11 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-07-05 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?