NAME

Gzip::Faster - simple and fast gzip and gunzip

SYNOPSIS

use Gzip::Faster;
my $gzipped = gzip ($input);
my $roundtrip = gunzip ($gzipped);
if ($roundtrip ne $input) { die; }
gzip_to_file ($input, 'file.gz');
$roundtrip = gunzip_file ('file.gz');
if ($roundtrip ne $input) { die; }

DESCRIPTION

This module compresses to and decompresses from the gzip format.

The module offers two basic functions, gzip and gunzip, which convert scalars to and from gzip format, and three convenience functions: gzip_file reads a file then compresses it; gunzip_file reads a file then uncompresses it; and gzip_to_file compresses a scalar and writes it to a file.

FUNCTIONS

gzip

my $zipped = gzip ($stuff);

This compresses $stuff into the gzip format. The return value is the compressed version of $stuff.

gunzip

my $stuff = gunzip ($zipped);

This uncompresses $zipped and returns the result of the uncompression. It returns the undefined value if $zipped is the undefined value or an empty string. Otherwise, it throws a fatal error if $zipped is not in the gzip format.

gzip_file

my $zipped = gzip_file ('file');

This reads the contents of file into memory and then runs "gzip" on the file's contents. The return value and the possible errors are the same as "gzip", plus this may also throw an error if open fails.

gunzip_file

my $stuff = gunzip_file ('file.gz');

This reads the contents of file.gz into memory and then runs "gunzip" on the file's contents. The return value and the possible errors are the same as "gunzip", plus this may also throw an error if open fails.

gzip_to_file

gzip_to_file ($plain, 'file.gz');

This compresses $plain in memory using "gzip" and writes the compressed content to 'file.gz'. There is no return value. The errors are the same as "gzip", plus this may also throw an error if open fails. As of this version, it does not write any gzip header information to file.gz.

PERFORMANCE

This section compares the performance of Gzip::Faster with IO::Compress::Gzip/IO::Uncompress::Gunzip and Compress::Raw::Zlib. According to my results, Gzip::Faster is about five times faster to load, seven times faster to compress, and twenty-five times faster to uncompress than the IO::Uncompress modules. Round trips are about ten times faster with Gzip::Faster.

Compared to the Compress::Raw::Zlib modules, load times are about one and a half times faster, round trips are about three times faster, compression is about two and a half times faster, and decompression is about six times faster.

The versions used in this test are as follows:

$IO::Compress::Gzip::VERSION = 2.068
$IO::Uncompress::Gunzip::VERSION = 2.068
$Compress::Raw::Zlib::VERSION = 2.069
$Gzip::Faster::VERSION = 0.12

Here is a comparison of load times:

            Rate Load IOUG Load IOCG  Load CRZ   Load GF
Load IOUG 25.4/s        --       -4%      -66%      -77%
Load IOCG 26.6/s        4%        --      -65%      -76%
Load CRZ  75.3/s      196%      183%        --      -33%
Load GF    113/s      344%      325%       50%        --

Here is a comparison of a round-trip:

                       Rate IO::Compress::Gzip Compress::Raw::Zlib  Gzip::Faster
IO::Compress::Gzip   1298/s                 --                -66%          -90%
Compress::Raw::Zlib  3853/s               197%                  --          -70%
Gzip::Faster        12673/s               876%                229%            --

Here is a comparison of gzip (compression) only:

                                Rate IO::Compress::Gzip Compress::Raw::Zlib::Deflate Gzip::Faster
IO::Compress::Gzip            2553/s                 --                         -60%         -86%
Compress::Raw::Zlib::Deflate  6445/s               152%                           --         -64%
Gzip::Faster                 17927/s               602%                         178%           --

Here is a comparison of gunzip (decompression) only:

                                Rate IO::Uncompress::Gunzip Compress::Raw::Zlib::Inflate Gzip::Faster
IO::Uncompress::Gunzip        2805/s                     --                         -74%         -96%
Compress::Raw::Zlib::Inflate 10997/s                   292%                           --         -84%
Gzip::Faster                 68817/s                  2354%                         526%           --

The test file is in "examples/benchmark.pl" in the distribution.

BUGS

The module source code includes disabled functionality to round-trip Perl flags. I applied this to preserving Perl's "utf8" flag. However, the mechanism I used trips a browser bug in the Firefox web browser where it produces a content encoding error message. Thus this functionality is disabled. Please refer to the file gzip-faster-perl.c in the distribution, the relevant parts are commented out with a macro COPY_PERL.

The module doesn't check whether the input of "gzip" is already gzipped, and it doesn't check whether the compression was effective. That is, it doesn't check whether the output of "gzip" is actually smaller than the input.

INSTALLATION

Installation follows the standard Perl methods. If you do not know what the standard Perl module install methods are, detailed instructions can be found in the file README in the distribution. The following are some extra notes for people who get stuck.

Gzip::Faster requires the compression library zlib (also called libz) to be installed. The following message printed during perl Makefile.PL:

Warning (mostly harmless): No library found for -lz

or the following message at run-time:

undefined symbol: inflate

indicate that Gzip::Faster was unable to link to libz.

Ubuntu Linux

On Ubuntu Linux, you may need to install zlib1g-dev using the following command:

sudo apt-get install zlib1g-dev

ACKNOWLEDGEMENTS

zgrim reported an important bug related to zlib.

Aristotle Pagaltzis contributed the benchmarking code for Compress::Raw::Zlib.

AUTHOR, COPYRIGHT AND LICENCE

Ben Bullock <bkb@cpan.org>. Copyright (C) 2015 Ben Bullock. This software may be used, modified, distributed under the same licence as Perl itself.