1 package IO::Uncompress::AnyUncompress ;
7 use IO::Compress::Base::Common 2.004 qw(createSelfTiedObject);
9 use IO::Uncompress::Base 2.004 ;
14 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
17 $AnyUncompressError = '';
19 @ISA = qw( Exporter IO::Uncompress::Base );
20 @EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
21 %EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
22 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
23 Exporter::export_ok_tags('all');
25 # TODO - allow the user to pick a set of the three formats to allow
26 # or just assume want to auto-detect any of the three formats.
30 eval ' use IO::Uncompress::Adapter::Inflate 2.004 ;';
31 eval ' use IO::Uncompress::Adapter::Bunzip2 2.004 ;';
32 eval ' use IO::Uncompress::Adapter::LZO 2.004 ;';
33 eval ' use IO::Uncompress::Adapter::Lzf 2.004 ;';
35 eval ' use IO::Uncompress::Bunzip2 2.004 ;';
36 eval ' use IO::Uncompress::UnLzop 2.004 ;';
37 eval ' use IO::Uncompress::Gunzip 2.004 ;';
38 eval ' use IO::Uncompress::Inflate 2.004 ;';
39 eval ' use IO::Uncompress::RawInflate 2.004 ;';
40 eval ' use IO::Uncompress::Unzip 2.004 ;';
41 eval ' use IO::Uncompress::UnLzf 2.004 ;';
47 my $obj = createSelfTiedObject($class, \$AnyUncompressError);
48 $obj->_create(undef, 0, @_);
53 my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
54 return $obj->_inf(@_) ;
59 use IO::Compress::Base::Common 2.004 qw(:Parse);
60 return ( 'RawInflate' => [1, 1, Parse_boolean, 0] ) ;
68 # any always needs both crc32 and adler32
69 $got->value('CRC32' => 1);
70 $got->value('ADLER32' => 1);
84 if (defined $IO::Uncompress::RawInflate::VERSION )
86 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
88 return $self->saveErrorString(undef, $errstr, $errno)
91 *$self->{Uncomp} = $obj;
93 my @possible = qw( Inflate Gunzip Unzip );
94 unshift @possible, 'RawInflate'
95 if $got->value('RawInflate');
97 $magic = $self->ckMagic( @possible );
100 *$self->{Info} = $self->readHeader($magic)
107 if (defined $IO::Uncompress::Bunzip2::VERSION and
108 $magic = $self->ckMagic('Bunzip2')) {
109 *$self->{Info} = $self->readHeader($magic)
112 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
114 return $self->saveErrorString(undef, $errstr, $errno)
117 *$self->{Uncomp} = $obj;
122 if (defined $IO::Uncompress::UnLzop::VERSION and
123 $magic = $self->ckMagic('UnLzop')) {
125 *$self->{Info} = $self->readHeader($magic)
128 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
130 return $self->saveErrorString(undef, $errstr, $errno)
133 *$self->{Uncomp} = $obj;
138 if (defined $IO::Uncompress::UnLzf::VERSION and
139 $magic = $self->ckMagic('UnLzf')) {
141 *$self->{Info} = $self->readHeader($magic)
144 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Lzf::mkUncompObject();
146 return $self->saveErrorString(undef, $errstr, $errno)
149 *$self->{Uncomp} = $obj;
164 my $keep = ref $self ;
165 for my $class ( map { "IO::Uncompress::$_" } @names)
167 bless $self => $class;
168 my $magic = $self->ckMagic();
172 #bless $self => $class;
176 $self->pushBack(*$self->{HeaderPending}) ;
177 *$self->{HeaderPending} = '' ;
180 bless $self => $keep;
192 IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop file/buffer
197 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
199 my $status = anyuncompress $input => $output [,OPTS]
200 or die "anyuncompress failed: $AnyUncompressError\n";
202 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
203 or die "anyuncompress failed: $AnyUncompressError\n";
205 $status = $z->read($buffer)
206 $status = $z->read($buffer, $length)
207 $status = $z->read($buffer, $length, $offset)
208 $line = $z->getline()
213 $data = $z->trailingData()
214 $status = $z->nextStream()
215 $data = $z->getHeaderInfo()
217 $z->seek($position, $whence)
223 $AnyUncompressError ;
229 read($z, $buffer, $length);
230 read($z, $buffer, $length, $offset);
232 seek($z, $position, $whence)
242 This module provides a Perl interface that allows the reading of
243 files/buffers that have been compressed with a variety of compression
246 The formats supported are:
252 =item RFC 1951 (optionally)
254 =item gzip (RFC 1952)
266 The module will auto-detect which, if any, of the supported
267 compression formats is being used.
272 =head1 Functional Interface
274 A top-level function, C<anyuncompress>, is provided to carry out
275 "one-shot" uncompression between buffers and/or files. For finer
276 control over the uncompression process, see the L</"OO Interface">
279 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
281 anyuncompress $input => $output [,OPTS]
282 or die "anyuncompress failed: $AnyUncompressError\n";
286 The functional interface needs Perl5.005 or better.
289 =head2 anyuncompress $input => $output [, OPTS]
292 C<anyuncompress> expects at least two parameters, C<$input> and C<$output>.
294 =head3 The C<$input> parameter
296 The parameter, C<$input>, is used to define the source of
299 It can take one of the following forms:
305 If the C<$input> parameter is a simple scalar, it is assumed to be a
306 filename. This file will be opened for reading and the input data
307 will be read from it.
311 If the C<$input> parameter is a filehandle, the input data will be
313 The string '-' can be used as an alias for standard input.
315 =item A scalar reference
317 If C<$input> is a scalar reference, the input data will be read
320 =item An array reference
322 If C<$input> is an array reference, each element in the array must be a
325 The input data will be read from each file in turn.
327 The complete array will be walked to ensure that it only
328 contains valid filenames before any data is uncompressed.
332 =item An Input FileGlob string
334 If C<$input> is a string that is delimited by the characters "<" and ">"
335 C<anyuncompress> will assume that it is an I<input fileglob string>. The
336 input is the list of files that match the fileglob.
338 If the fileglob does not match any files ...
340 See L<File::GlobMapper|File::GlobMapper> for more details.
345 If the C<$input> parameter is any other type, C<undef> will be returned.
349 =head3 The C<$output> parameter
351 The parameter C<$output> is used to control the destination of the
352 uncompressed data. This parameter can take one of these forms.
358 If the C<$output> parameter is a simple scalar, it is assumed to be a
359 filename. This file will be opened for writing and the uncompressed
360 data will be written to it.
364 If the C<$output> parameter is a filehandle, the uncompressed data
365 will be written to it.
366 The string '-' can be used as an alias for standard output.
369 =item A scalar reference
371 If C<$output> is a scalar reference, the uncompressed data will be
372 stored in C<$$output>.
376 =item An Array Reference
378 If C<$output> is an array reference, the uncompressed data will be
379 pushed onto the array.
381 =item An Output FileGlob
383 If C<$output> is a string that is delimited by the characters "<" and ">"
384 C<anyuncompress> will assume that it is an I<output fileglob string>. The
385 output is the list of files that match the fileglob.
387 When C<$output> is an fileglob string, C<$input> must also be a fileglob
388 string. Anything else is an error.
392 If the C<$output> parameter is any other type, C<undef> will be returned.
399 When C<$input> maps to multiple compressed files/buffers and C<$output> is
400 a single file/buffer, after uncompression C<$output> will contain a
401 concatenation of all the uncompressed data from each of the input
408 =head2 Optional Parameters
410 Unless specified below, the optional parameters for C<anyuncompress>,
411 C<OPTS>, are the same as those used with the OO interface defined in the
412 L</"Constructor Options"> section below.
416 =item C<< AutoClose => 0|1 >>
418 This option applies to any input or output data streams to
419 C<anyuncompress> that are filehandles.
421 If C<AutoClose> is specified, and the value is true, it will result in all
422 input and/or output filehandles being closed once C<anyuncompress> has
425 This parameter defaults to 0.
428 =item C<< BinModeOut => 0|1 >>
430 When writing to a file or filehandle, set C<binmode> before writing to the
439 =item C<< Append => 0|1 >>
443 =item C<< MultiStream => 0|1 >>
446 If the input file/buffer contains multiple compressed data streams, this
447 option will uncompress the whole lot as a single data stream.
455 =item C<< TrailingData => $scalar >>
457 Returns the data, if any, that is present immediately after the compressed
458 data stream once uncompression is complete.
460 This option can be used when there is useful information immediately
461 following the compressed data stream, and you don't know the length of the
462 compressed data stream.
464 If the input is a buffer, C<trailingData> will return everything from the
465 end of the compressed data stream to the end of the buffer.
467 If the input is a filehandle, C<trailingData> will return the data that is
468 left in the filehandle input buffer once the end of the compressed data
469 stream has been reached. You can then use the filehandle to read the rest
472 Don't bother using C<trailingData> if the input is a filename.
476 If you know the length of the compressed data stream before you start
477 uncompressing, you can avoid having to use C<trailingData> by setting the
478 C<InputLength> option.
489 To read the contents of the file C<file1.txt.Compressed> and write the
490 compressed data to the file C<file1.txt>.
494 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
496 my $input = "file1.txt.Compressed";
497 my $output = "file1.txt";
498 anyuncompress $input => $output
499 or die "anyuncompress failed: $AnyUncompressError\n";
502 To read from an existing Perl filehandle, C<$input>, and write the
503 uncompressed data to a buffer, C<$buffer>.
507 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
510 my $input = new IO::File "<file1.txt.Compressed"
511 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
513 anyuncompress $input => \$buffer
514 or die "anyuncompress failed: $AnyUncompressError\n";
516 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
520 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
522 anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
523 or die "anyuncompress failed: $AnyUncompressError\n";
525 and if you want to compress each file one at a time, this will do the trick
529 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
531 for my $input ( glob "/my/home/*.txt.Compressed" )
534 $output =~ s/.Compressed// ;
535 anyuncompress $input => $output
536 or die "Error compressing '$input': $AnyUncompressError\n";
543 The format of the constructor for IO::Uncompress::AnyUncompress is shown below
546 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
547 or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
549 Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
550 The variable C<$AnyUncompressError> will contain an error message on failure.
552 If you are running Perl 5.005 or better the object, C<$z>, returned from
553 IO::Uncompress::AnyUncompress can be used exactly like an L<IO::File|IO::File> filehandle.
554 This means that all normal input file operations can be carried out with
555 C<$z>. For example, to read a line from a compressed file/buffer you can
556 use either of these forms
558 $line = $z->getline();
561 The mandatory parameter C<$input> is used to determine the source of the
562 compressed data. This parameter can take one of three forms.
568 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
569 file will be opened for reading and the compressed data will be read from it.
573 If the C<$input> parameter is a filehandle, the compressed data will be
575 The string '-' can be used as an alias for standard input.
578 =item A scalar reference
580 If C<$input> is a scalar reference, the compressed data will be read from
585 =head2 Constructor Options
588 The option names defined below are case insensitive and can be optionally
589 prefixed by a '-'. So all of the following are valid
596 OPTS is a combination of the following options:
600 =item C<< AutoClose => 0|1 >>
602 This option is only valid when the C<$input> parameter is a filehandle. If
603 specified, and the value is true, it will result in the file being closed once
604 either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
607 This parameter defaults to 0.
609 =item C<< MultiStream => 0|1 >>
613 Allows multiple concatenated compressed streams to be treated as a single
614 compressed stream. Decompression will stop once either the end of the
615 file/buffer is reached, an error is encountered (premature eof, corrupt
616 compressed data) or the end of a stream is not immediately followed by the
617 start of another stream.
619 This parameter defaults to 0.
622 =item C<< Prime => $string >>
624 This option will uncompress the contents of C<$string> before processing the
627 This option can be useful when the compressed data is embedded in another
628 file/data structure and it is not possible to work out where the compressed
629 data begins without having to read the first few bytes. If this is the
630 case, the uncompression can be I<primed> with these bytes using this
633 =item C<< Transparent => 0|1 >>
635 If this option is set and the input file/buffer is not compressed data,
636 the module will allow reading of it anyway.
638 In addition, if the input file/buffer does contain compressed data and
639 there is non-compressed data immediately following it, setting this option
640 will make this module treat the whole file/bufffer as a single data stream.
642 This option defaults to 1.
644 =item C<< BlockSize => $num >>
646 When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
647 blocks of C<$num> bytes.
649 This option defaults to 4096.
651 =item C<< InputLength => $size >>
653 When present this option will limit the number of compressed bytes read
654 from the input file/buffer to C<$size>. This option can be used in the
655 situation where there is useful data directly after the compressed data
656 stream and you know beforehand the exact length of the compressed data
659 This option is mostly used when reading from a filehandle, in which case
660 the file pointer will be left pointing to the first byte directly after the
661 compressed data stream.
665 This option defaults to off.
667 =item C<< Append => 0|1 >>
669 This option controls what the C<read> method does with uncompressed data.
671 If set to 1, all uncompressed data will be appended to the output parameter
672 of the C<read> method.
674 If set to 0, the contents of the output parameter of the C<read> method
675 will be overwritten by the uncompressed data.
679 =item C<< Strict => 0|1 >>
683 This option controls whether the extra checks defined below are used when
684 carrying out the decompression. When Strict is on, the extra tests are
685 carried out, when Strict is off they are not.
687 The default for this option is off.
699 =item C<< RawInflate => 0|1 >>
701 When auto-detecting the compressed format, try to test for raw-deflate (RFC
702 1951) content using the C<IO::Uncompress::RawInflate> module.
704 The reason this is not default behaviour is because RFC 1951 content can
705 only be detected by attempting to uncompress it. This process is error
706 prone and can result is false positives.
727 $status = $z->read($buffer)
729 Reads a block of compressed data (the size the the compressed block is
730 determined by the C<Buffer> option in the constructor), uncompresses it and
731 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
732 set in the constructor, the uncompressed data will be appended to the
733 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
735 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
736 or a negative number on error.
742 $status = $z->read($buffer, $length)
743 $status = $z->read($buffer, $length, $offset)
745 $status = read($z, $buffer, $length)
746 $status = read($z, $buffer, $length, $offset)
748 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
750 The main difference between this form of the C<read> method and the
751 previous one, is that this one will attempt to return I<exactly> C<$length>
752 bytes. The only circumstances that this function will not is if end-of-file
753 or an IO error is encountered.
755 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
756 or a negative number on error.
763 $line = $z->getline()
768 This method fully supports the use of of the variable C<$/> (or
769 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
770 determine what constitutes an end of line. Paragraph mode, record mode and
771 file slurp mode are all supported.
780 Read a single character.
786 $char = $z->ungetc($string)
795 $hdr = $z->getHeaderInfo();
796 @hdrs = $z->getHeaderInfo();
798 This method returns either a hash reference (in scalar context) or a list
799 or hash references (in array context) that contains information about each
800 of the header fields in the compressed data stream(s).
812 Returns the uncompressed file offset.
823 Returns true if the end of the compressed input stream has been reached.
829 $z->seek($position, $whence);
830 seek($z, $position, $whence);
835 Provides a sub-set of the C<seek> functionality, with the restriction
836 that it is only legal to seek forward in the input file/buffer.
837 It is a fatal error to attempt to seek backward.
841 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
842 SEEK_CUR or SEEK_END.
844 Returns 1 on success, 0 on failure.
853 This is a noop provided for completeness.
859 Returns true if the object currently refers to a opened file/buffer.
863 my $prev = $z->autoflush()
864 my $prev = $z->autoflush(EXPR)
866 If the C<$z> object is associated with a file or a filehandle, this method
867 returns the current autoflush setting for the underlying filehandle. If
868 C<EXPR> is present, and is non-zero, it will enable flushing after every
869 write/print operation.
871 If C<$z> is associated with a buffer, this method has no effect and always
874 B<Note> that the special variable C<$|> B<cannot> be used to set or
875 retrieve the autoflush setting.
877 =head2 input_line_number
879 $z->input_line_number()
880 $z->input_line_number(EXPR)
884 Returns the current uncompressed line number. If C<EXPR> is present it has
885 the effect of setting the line number. Note that setting the line number
886 does not change the current position within the file/buffer being read.
888 The contents of C<$/> are used to to determine what constitutes a line
898 If the C<$z> object is associated with a file or a filehandle, this method
899 will return the underlying file descriptor.
901 If the C<$z> object is is associated with a buffer, this method will
911 Closes the output file/buffer.
915 For most versions of Perl this method will be automatically invoked if
916 the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
917 variable with the reference to the object going out of scope). The
918 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
919 these cases, the C<close> method will be called automatically, but
920 not until global destruction of all live objects when the program is
923 Therefore, if you want your scripts to be able to run on all versions
924 of Perl, you should call C<close> explicitly and not rely on automatic
927 Returns true on success, otherwise 0.
929 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
930 object was created, and the object is associated with a file, the
931 underlying file will also be closed.
940 my $status = $z->nextStream();
942 Skips to the next compressed data stream in the input file/buffer. If a new
943 compressed data stream is found, the eof marker will be cleared and C<$.>
946 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
947 error was encountered.
953 my $data = $z->trailingData();
955 Returns the data, if any, that is present immediately after the compressed
956 data stream once uncompression is complete. It only makes sense to call
957 this method once the end of the compressed data stream has been
960 This option can be used when there is useful information immediately
961 following the compressed data stream, and you don't know the length of the
962 compressed data stream.
964 If the input is a buffer, C<trailingData> will return everything from the
965 end of the compressed data stream to the end of the buffer.
967 If the input is a filehandle, C<trailingData> will return the data that is
968 left in the filehandle input buffer once the end of the compressed data
969 stream has been reached. You can then use the filehandle to read the rest
972 Don't bother using C<trailingData> if the input is a filename.
976 If you know the length of the compressed data stream before you start
977 uncompressing, you can avoid having to use C<trailingData> by setting the
978 C<InputLength> option in the constructor.
982 No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
988 Imports C<anyuncompress> and C<$AnyUncompressError>.
991 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
1002 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>
1004 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1006 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1007 L<Archive::Tar|Archive::Tar>,
1008 L<IO::Zlib|IO::Zlib>
1016 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1020 =head1 MODIFICATION HISTORY
1022 See the Changes file.
1024 =head1 COPYRIGHT AND LICENSE
1026 Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
1028 This program is free software; you can redistribute it and/or
1029 modify it under the same terms as Perl itself.