package IO::Uncompress::AnyUncompress ;
use strict;
use warnings;
use bytes;
use IO::Compress::Base::Common 2.021 qw(createSelfTiedObject);
use IO::Uncompress::Base 2.021 ;
require Exporter ;
our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
$VERSION = '2.021';
$AnyUncompressError = '';
@ISA = qw( Exporter IO::Uncompress::Base );
@EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
%EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
Exporter::export_ok_tags('all');
# TODO - allow the user to pick a set of the three formats to allow
# or just assume want to auto-detect any of the three formats.
BEGIN
{
eval ' use IO::Uncompress::Adapter::Inflate 2.021 ;';
eval ' use IO::Uncompress::Adapter::Bunzip2 2.021 ;';
eval ' use IO::Uncompress::Adapter::LZO 2.021 ;';
eval ' use IO::Uncompress::Adapter::Lzf 2.021 ;';
eval ' use IO::Uncompress::Adapter::UnLzma 2.020 ;';
eval ' use IO::Uncompress::Adapter::UnXz 2.020 ;';
eval ' use IO::Uncompress::Bunzip2 2.021 ;';
eval ' use IO::Uncompress::UnLzop 2.021 ;';
eval ' use IO::Uncompress::Gunzip 2.021 ;';
eval ' use IO::Uncompress::Inflate 2.021 ;';
eval ' use IO::Uncompress::RawInflate 2.021 ;';
eval ' use IO::Uncompress::Unzip 2.021 ;';
eval ' use IO::Uncompress::UnLzf 2.021 ;';
eval ' use IO::Uncompress::UnLzma 2.018 ;';
eval ' use IO::Uncompress::UnXz 2.018 ;';
}
sub new
{
my $class = shift ;
my $obj = createSelfTiedObject($class, \$AnyUncompressError);
$obj->_create(undef, 0, @_);
}
sub anyuncompress
{
my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
return $obj->_inf(@_) ;
}
sub getExtraParams
{
use IO::Compress::Base::Common 2.021 qw(:Parse);
return ( 'RawInflate' => [1, 1, Parse_boolean, 0] ) ;
}
sub ckParams
{
my $self = shift ;
my $got = shift ;
# any always needs both crc32 and adler32
$got->value('CRC32' => 1);
$got->value('ADLER32' => 1);
return 1;
}
sub mkUncomp
{
my $self = shift ;
my $got = shift ;
my $magic ;
# try zlib first
if (defined $IO::Uncompress::RawInflate::VERSION )
{
my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;
*$self->{Uncomp} = $obj;
my @possible = qw( Inflate Gunzip Unzip );
unshift @possible, 'RawInflate'
if $got->value('RawInflate');
$magic = $self->ckMagic( @possible );
if ($magic) {
*$self->{Info} = $self->readHeader($magic)
or return undef ;
return 1;
}
}
# if (defined $IO::Uncompress::UnLzma::VERSION )
# {
# my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::UnLzma::mkUncompObject();
#
# return $self->saveErrorString(undef, $errstr, $errno)
# if ! defined $obj;
#
# *$self->{Uncomp} = $obj;
#
# my @possible = qw( UnLzma );
# #unshift @possible, 'RawInflate'
# # if $got->value('RawInflate');
#
# if ( *$self->{Info} = $self->ckMagic( @possible ))
# {
# return 1;
# }
# }
if (defined $IO::Uncompress::UnXz::VERSION and
$magic = $self->ckMagic('UnXz')) {
*$self->{Info} = $self->readHeader($magic)
or return undef ;
my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::UnXz::mkUncompObject();
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;
*$self->{Uncomp} = $obj;
return 1;
}
if (defined $IO::Uncompress::Bunzip2::VERSION and
$magic = $self->ckMagic('Bunzip2')) {
*$self->{Info} = $self->readHeader($magic)
or return undef ;
my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;
*$self->{Uncomp} = $obj;
return 1;
}
if (defined $IO::Uncompress::UnLzop::VERSION and
$magic = $self->ckMagic('UnLzop')) {
*$self->{Info} = $self->readHeader($magic)
or return undef ;
my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;
*$self->{Uncomp} = $obj;
return 1;
}
if (defined $IO::Uncompress::UnLzf::VERSION and
$magic = $self->ckMagic('UnLzf')) {
*$self->{Info} = $self->readHeader($magic)
or return undef ;
my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Lzf::mkUncompObject();
return $self->saveErrorString(undef, $errstr, $errno)
if ! defined $obj;
*$self->{Uncomp} = $obj;
return 1;
}
return 0 ;
}
sub ckMagic
{
my $self = shift;
my @names = @_ ;
my $keep = ref $self ;
for my $class ( map { "IO::Uncompress::$_" } @names)
{
bless $self => $class;
my $magic = $self->ckMagic();
if ($magic)
{
#bless $self => $class;
return $magic ;
}
$self->pushBack(*$self->{HeaderPending}) ;
*$self->{HeaderPending} = '' ;
}
bless $self => $keep;
return undef;
}
1 ;
__END__
=head1 NAME
IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop file/buffer
=head1 SYNOPSIS
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
my $status = anyuncompress $input => $output [,OPTS]
or die "anyuncompress failed: $AnyUncompressError\n";
my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
or die "anyuncompress failed: $AnyUncompressError\n";
$status = $z->read($buffer)
$status = $z->read($buffer, $length)
$status = $z->read($buffer, $length, $offset)
$line = $z->getline()
$char = $z->getc()
$char = $z->ungetc()
$char = $z->opened()
$data = $z->trailingData()
$status = $z->nextStream()
$data = $z->getHeaderInfo()
$z->tell()
$z->seek($position, $whence)
$z->binmode()
$z->fileno()
$z->eof()
$z->close()
$AnyUncompressError ;
# IO::File mode
<$z>
read($z, $buffer);
read($z, $buffer, $length);
read($z, $buffer, $length, $offset);
tell($z)
seek($z, $position, $whence)
binmode($z)
fileno($z)
eof($z)
close($z)
=head1 DESCRIPTION
This module provides a Perl interface that allows the reading of
files/buffers that have been compressed with a variety of compression
libraries.
The formats supported are:
=over 5
=item RFC 1950
=item RFC 1951 (optionally)
=item gzip (RFC 1952)
=item zip
=item bzip2
=item lzop
=item lzf
=back
The module will auto-detect which, if any, of the supported
compression formats is being used.
=head1 Functional Interface
A top-level function, C, is provided to carry out
"one-shot" uncompression between buffers and/or files. For finer
control over the uncompression process, see the L"OO Interface">
section.
use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
anyuncompress $input => $output [,OPTS]
or die "anyuncompress failed: $AnyUncompressError\n";
The functional interface needs Perl5.005 or better.
=head2 anyuncompress $input => $output [, OPTS]
C expects at least two parameters, C<$input> and C<$output>.
=head3 The C<$input> parameter
The parameter, C<$input>, is used to define the source of
the compressed data.
It can take one of the following forms:
=over 5
=item A filename
If the C<$input> parameter is a simple scalar, it is assumed to be a
filename. This file will be opened for reading and the input data
will be read from it.
=item A filehandle
If the C<$input> parameter is a filehandle, the input data will be
read from it.
The string '-' can be used as an alias for standard input.
=item A scalar reference
If C<$input> is a scalar reference, the input data will be read
from C<$$input>.
=item An array reference
If C<$input> is an array reference, each element in the array must be a
filename.
The input data will be read from each file in turn.
The complete array will be walked to ensure that it only
contains valid filenames before any data is uncompressed.
=item An Input FileGlob string
If C<$input> is a string that is delimited by the characters "<" and ">"
C will assume that it is an I. The
input is the list of files that match the fileglob.
If the fileglob does not match any files ...
See L for more details.
=back
If the C<$input> parameter is any other type, C will be returned.
=head3 The C<$output> parameter
The parameter C<$output> is used to control the destination of the
uncompressed data. This parameter can take one of these forms.
=over 5
=item A filename
If the C<$output> parameter is a simple scalar, it is assumed to be a
filename. This file will be opened for writing and the uncompressed
data will be written to it.
=item A filehandle
If the C<$output> parameter is a filehandle, the uncompressed data
will be written to it.
The string '-' can be used as an alias for standard output.
=item A scalar reference
If C<$output> is a scalar reference, the uncompressed data will be
stored in C<$$output>.
=item An Array Reference
If C<$output> is an array reference, the uncompressed data will be
pushed onto the array.
=item An Output FileGlob
If C<$output> is a string that is delimited by the characters "<" and ">"
C will assume that it is an I