
=head1 NAME

IO::Compress::FAQ -- Frequently Asked Questions about IO::Compress

=head1 DESCRIPTION

Common questions answered.

=head1 GENERAL 

=head2 Compatibility with Unix compress/uncompress.

Although C<Compress::Zlib> has a pair of functions called C<compress> and
C<uncompress>, they are I<not> related to the Unix programs of the same
name. The C<Compress::Zlib> module is not compatible with Unix
C<compress>.

If you have the C<uncompress> program available, you can use this to read
compressed files

    open F, "uncompress -c $filename |";
    while (<F>)
    {
        ...

Alternatively, if you have the C<gunzip> program available, you can use
this to read compressed files

    open F, "gunzip -c $filename |";
    while (<F>)
    {
        ...

and this to write compress files, if you have the C<compress> program
available

    open F, "| compress -c $filename ";
    print F "data";
    ...
    close F ;

=head2 Accessing .tar.Z files

The C<Archive::Tar> module can optionally use C<Compress::Zlib> (via the
C<IO::Zlib> module) to access tar files that have been compressed with
C<gzip>. Unfortunately tar files compressed with the Unix C<compress>
utility cannot be read by C<Compress::Zlib> and so cannot be directly
accessed by C<Archive::Tar>.

If the C<uncompress> or C<gunzip> programs are available, you can use one
of these workarounds to read C<.tar.Z> files from C<Archive::Tar>

Firstly with C<uncompress>

    use strict;
    use warnings;
    use Archive::Tar;

    open F, "uncompress -c $filename |";
    my $tar = Archive::Tar->new(*F);
    ...

and this with C<gunzip>

    use strict;
    use warnings;
    use Archive::Tar;

    open F, "gunzip -c $filename |";
    my $tar = Archive::Tar->new(*F);
    ...

Similarly, if the C<compress> program is available, you can use this to
write a C<.tar.Z> file

    use strict;
    use warnings;
    use Archive::Tar;
    use IO::File;

    my $fh = new IO::File "| compress -c >$filename";
    my $tar = Archive::Tar->new();
    ...
    $tar->write($fh);
    $fh->close ;

=head2 How do I recompress using a different compression?

This is easier that you might expect if you realise that all the
C<IO::Compress::*> objects are derived from C<IO::File> and that all the
C<IO::Uncompress::*> modules can read from an C<IO::File> filehandle.

So, for example, say you have a file compressed with gzip that you want to
recompress with bzip2. Here is all that is needed to carry out the
recompression.

    use IO::Uncompress::Gunzip ':all';
    use IO::Compress::Bzip2 ':all';

    my $gzipFile = "somefile.gz";
    my $bzipFile = "somefile.bz2";

    my $gunzip = new IO::Uncompress::Gunzip $gzipFile
        or die "Cannot gunzip $gzipFile: $GunzipError\n" ;

    bzip2 $gunzip => $bzipFile 
        or die "Cannot bzip2 to $bzipFile: $Bzip2Error\n" ;

Note, there is a limitation of this technique. Some compression file
formats store extra information along with the compressed data payload. For
example, gzip can optionally store the original filename and Zip stores a
lot of information about the original file. If the original compressed file
contains any of this extra information, it will not be transferred to the
new compressed file using the technique above.

=head1 ZIP

=head2 What Compression Types do IO::Compress::Zip & IO::Uncompress::Unzip support?

The following compression formats are supported by C<IO::Compress::Zip> and
C<IO::Uncompress::Unzip>

=over 5

=item * Store (method 0)

No compression at all.

=item * Deflate (method 8)

This is the default compression used when creating a zip file with
C<IO::Compress::Zip>.

=item * Bzip2 (method 12)

Only supported if the C<IO-Compress-Bzip2> module is installed.

=item * Lzma (method 14)

Only supported if the C<IO-Compress-Lzma> module is installed.

=back

=head2 Can I Read/Write Zip files larger the 4 Gig?

Yes, both the C<IO-Compress-Zip> and C<IO-Uncompress-Unzip>  modules
support the zip feature called I<Zip64>. That allows them to read/write
files/buffers larger than 4Gig. 

If you are creating a Zip file using the one-shot interface, and any of the
input files is greater than 4Gig, a zip64 complaint zip file will be
created. 

    zip "really-large-file" => "my.zip";

Similarly with the one-shot interface, if the input is a buffer larger than
4 Gig, a zip64 complaint zip file will be created. 

    zip \$really_large_buffer => "my.zip";

The one-shot interface allows you to force the creation of a zip64 zip file
by including the C<Zip64> option.

    zip $filehandle => "my.zip", Zip64 => 1;

If you want to create a zip64 zip file with the OO interface you must
specify the C<Zip64> option.

    my $zip = new IO::Compress::Zip "whatever", Zip64 => 1;
	
When uncompressing with C<IO-Uncompress-Unzip>, it will automatically
detect if the zip file is zip64.

If you intend to manipulate the Zip64 zip files created with
C<IO-Compress-Zip> using an external zip/unzip, make sure that it supports
Zip64.  

In particular, if you are using Info-Zip you need to have zip version 3.x
or better to update a Zip64 archive and unzip version 6.x to read a zip64
archive. 

=head2 Can I write more that 64K entries is a Zip files?

Yes. Zip64 allows this. See previous question.

=head2 Zip Resources

The primary reference for zip files is the "appnote" document available at
L<http://www.pkware.com/documents/casestudies/APPNOTE.TXT>

An alternatively is the Info-Zip appnote. This is available from
L<ftp://ftp.info-zip.org/pub/infozip/doc/>

=head1 GZIP

=head2 Gzip Resources

The primary reference for gzip files is RFC 1952
L<http://www.faqs.org/rfcs/rfc1952.html>

The primary site for gzip is F<http://www.gzip.org>.

=head2 Dealing with concatenated gzip files

If the gunzip program encounters a file containing multiple gzip files
concatenated together it will automatically uncompress them all.
The example below illustrates this behaviour

    $ echo abc | gzip -c >x.gz
    $ echo def | gzip -c >>x.gz
    $ gunzip -c x.gz 
    abc
    def

By default C<IO::Uncompress::Gunzip> will I<not> behave like the gunzip
program. It will only uncompress the first gzip data stream in the file, as
shown below

    $ perl -MIO::Uncompress::Gunzip=:all -e 'gunzip "x.gz" => \*STDOUT'
    abc

To force C<IO::Uncompress::Gunzip> to uncompress all the gzip data streams,
include the C<MultiStream> option, as shown below

    $ perl -MIO::Uncompress::Gunzip=:all -e 'gunzip "x.gz" => \*STDOUT, MultiStream => 1'
    abc
    def

=head2 Reading bgzip files with IO::Uncompress::Gunzip

A C<bgzip> file consists of a series of valid gzip-compliant data streams
concatenated together. To read a file created by C<bgzip> with
C<IO::Uncompress::Gunzip> use the C<MultiStream> option as shown in the
previous section.

See the section titled "The BGZF compression format" in
F<http://samtools.github.io/hts-specs/SAMv1.pdf> for a definition of
C<bgzip>.

=head1 ZLIB

=head2 Zlib Resources

The primary site for the I<zlib> compression library is
F<http://www.zlib.org>.

=head1 Bzip2

=head2 Bzip2 Resources

The primary site for bzip2 is F<http://www.bzip.org>.

=head2 Dealing with Concatenated bzip2 files

If the bunzip2 program encounters a file containing multiple bzip2 files
concatenated together it will automatically uncompress them all.
The example below illustrates this behaviour

    $ echo abc | bzip2 -c >x.bz2
    $ echo def | bzip2 -c >>x.bz2
    $ bunzip2 -c x.bz2
    abc
    def

By default C<IO::Uncompress::Bunzip2> will I<not> behave like the bunzip2
program. It will only uncompress the first bunzip2 data stream in the file, as
shown below

    $ perl -MIO::Uncompress::Bunzip2=:all -e 'bunzip2 "x.bz2" => \*STDOUT'
    abc

To force C<IO::Uncompress::Bunzip2> to uncompress all the bzip2 data streams,
include the C<MultiStream> option, as shown below

    $ perl -MIO::Uncompress::Bunzip2=:all -e 'bunzip2 "x.bz2" => \*STDOUT, MultiStream => 1'
    abc
    def

=head2 Interoperating with Pbzip2

Pbzip2 (L<http://compression.ca/pbzip2/>) is a parallel implementation of
bzip2. The output from pbzip2 consists of a series of concatenated bzip2
data streams.

By default C<IO::Uncompress::Bzip2> will only uncompress the first bzip2
data stream in a pbzip2 file. To uncompress the complete pbzip2 file you
must include the C<MultiStream> option, like this.

    bunzip2 $input => \$output, MultiStream => 1 
        or die "bunzip2 failed: $Bunzip2Error\n";

=head1 HTTP & NETWORK

=head2 Apache::GZip Revisited

Below is a mod_perl Apache compression module, called C<Apache::GZip>,
taken from
F<http://perl.apache.org/docs/tutorials/tips/mod_perl_tricks/mod_perl_tricks.html#On_the_Fly_Compression>

  