I've had very good results in terms of performance letting Perl handle the compression via the Compress::Zlib library (it does the crunching in C). Basically, the speed penalty of handling it in perl is offset by avoiding the cost of spawning children (although YMMV). If you use the cpan shell, you no doubt already have the module already installed.

#! /usr/bin/perl -w use strict; use Compress::Zlib; my $file = shift or die "no file on command line.\n"; my( $d, $status ) = deflateInit( {-Level => Z_BEST_COMPRESSION } ); die "deflator construction failed: $status\n" unless $status == Z_OK; my $deflated; open IN, $file or die "Cannot open $file for input: $!\n"; while( <IN> ) { ($deflated, $status) = $d->deflate( $_ ); die "deflator deflate failed: $status\n" unless $status == Z_O +K; print $deflated; } ($deflated, $status) = $d->flush(); die "deflator final flush failed: $status\n" unless $status == Z_OK; print $deflated; close IN;

Note that this script does not produce a zipfile directory, so you can't use gunzip/unzip on it directly; it is just the raw stream. You would decompress the file using an analogue inflate script (examples of how to do this are included in the pod).

This code is sub-optimal in that it reads the code line-by-line, instead of in blocks of 4096 bytes. This was a proof-of-concept demo I hacked up a while ago. I must say in passing though that the Compress::Zlib interface is truly awful.

If space is at a premium on the server, and you have the CPU cycles to spare, you should really be looking at bzip2 instead.


--
g r i n d e r

In reply to Re: gziping files on server by grinder
in thread gziping files on server by filmo

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.