📄 anyuncompress.pm
字号:
package IO::Uncompress::AnyUncompress ;use strict;use warnings;use bytes;use IO::Compress::Base::Common 2.008 qw(createSelfTiedObject);use IO::Uncompress::Base 2.008 ;require Exporter ;our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);$VERSION = '2.008';$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.008 ;'; eval ' use IO::Uncompress::Adapter::Bunzip2 2.008 ;'; eval ' use IO::Uncompress::Adapter::LZO 2.008 ;'; eval ' use IO::Uncompress::Adapter::Lzf 2.008 ;'; eval ' use IO::Uncompress::Bunzip2 2.008 ;'; eval ' use IO::Uncompress::UnLzop 2.008 ;'; eval ' use IO::Uncompress::Gunzip 2.008 ;'; eval ' use IO::Uncompress::Inflate 2.008 ;'; eval ' use IO::Uncompress::RawInflate 2.008 ;'; eval ' use IO::Uncompress::Unzip 2.008 ;'; eval ' use IO::Uncompress::UnLzf 2.008 ;';}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.008 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 $class = 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::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 NAMEIO::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 DESCRIPTIONThis module provides a Perl interface that allows the reading offiles/buffers that have been compressed with a variety of compressionlibraries.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=backThe module will auto-detect which, if any, of the supportedcompression formats is being used.=head1 Functional InterfaceA top-level function, C<anyuncompress>, is provided to carry out"one-shot" uncompression between buffers and/or files. For finercontrol 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<anyuncompress> expects at least two parameters, C<$input> and C<$output>.=head3 The C<$input> parameterThe parameter, C<$input>, is used to define the source ofthe compressed data. It can take one of the following forms:=over 5=item A filenameIf the C<$input> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for reading and the input datawill be read from it.=item A filehandleIf the C<$input> parameter is a filehandle, the input data will beread 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 readfrom C<$$input>.=item An array reference If C<$input> is an array reference, each element in the array must be afilename.The input data will be read from each file in turn. The complete array will be walked to ensure that it onlycontains valid filenames before any data is uncompressed.=item An Input FileGlob stringIf C<$input> is a string that is delimited by the characters "<" and ">"C<anyuncompress> will assume that it is an I<input fileglob string>. Theinput is the list of files that match the fileglob.If the fileglob does not match any files ...See L<File::GlobMapper|File::GlobMapper> for more details.=backIf the C<$input> parameter is any other type, C<undef> will be returned.=head3 The C<$output> parameterThe parameter C<$output> is used to control the destination of theuncompressed data. This parameter can take one of these forms.=over 5=item A filenameIf the C<$output> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for writing and the uncompresseddata will be written to it.=item A filehandleIf the C<$output> parameter is a filehandle, the uncompressed datawill 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 bestored in C<$$output>.=item An Array ReferenceIf C<$output> is an array reference, the uncompressed data will bepushed onto the array.=item An Output FileGlobIf C<$output> is a string that is delimited by the characters "<" and ">"C<anyuncompress> will assume that it is an I<output fileglob string>. Theoutput is the list of files that match the fileglob.When C<$output> is an fileglob string, C<$input> must also be a fileglobstring. Anything else is an error.=backIf the C<$output> parameter is any other type, C<undef> will be returned.=head2 NotesWhen C<$input> maps to multiple compressed files/buffers and C<$output> isa single file/buffer, after uncompression C<$output> will contain aconcatenation of all the uncompressed data from each of the inputfiles/buffers.=head2 Optional ParametersUnless specified below, the optional parameters for C<anyuncompress>,C<OPTS>, are the same as those used with the OO interface defined in theL</"Constructor Options"> section below.=over 5=item C<< AutoClose => 0|1 >>This option applies to any input or output data streams to C<anyuncompress> that are filehandles.If C<AutoClose> is specified, and the value is true, it will result in allinput and/or output filehandles being closed once C<anyuncompress> hascompleted.This parameter defaults to 0.=item C<< BinModeOut => 0|1 >>When writing to a file or filehandle, set C<binmode> before writing to thefile.Defaults to 0.=item C<< Append => 0|1 >>TODO=item C<< MultiStream => 0|1 >>If the input file/buffer contains multiple compressed data streams, thisoption will uncompress the whole lot as a single data stream.Defaults to 0.=item C<< TrailingData => $scalar >>Returns the data, if any, that is present immediately after the compresseddata stream once uncompression is complete. This option can be used when there is useful information immediatelyfollowing the compressed data stream, and you don't know the length of thecompressed data stream.If the input is a buffer, C<trailingData> will return everything from theend of the compressed data stream to the end of the buffer.If the input is a filehandle, C<trailingData> will return the data that isleft in the filehandle input buffer once the end of the compressed datastream has been reached. You can then use the filehandle to read the restof the input file. Don't bother using C<trailingData> if the input is a filename.If you know the length of the compressed data stream before you startuncompressing, you can avoid having to use C<trailingData> by setting theC<InputLength> option.=back=head2 ExamplesTo read the contents of the file C<file1.txt.Compressed> and write thecompressed data to the file C<file1.txt>. use strict ; use warnings ; use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ; my $input = "file1.txt.Compressed"; my $output = "file1.txt"; anyuncompress $input => $output or die "anyuncompress failed: $AnyUncompressError\n";To read from an existing Perl filehandle, C<$input>, and write theuncompressed data to a buffer, C<$buffer>. use strict ; use warnings ; use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ; use IO::File ; my $input = new IO::File "<file1.txt.Compressed" or die "Cannot open 'file1.txt.Compressed': $!\n" ; my $buffer ; anyuncompress $input => \$buffer or die "anyuncompress failed: $AnyUncompressError\n";To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -