http://www.perlmonks.org?node_id=457159

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