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


In reply to Compressing Data On The Fly by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.