Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-08-19 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?