1 package IO::Uncompress::AnyUncompress ;
7 use IO::Compress::Base::Common qw(createSelfTiedObject);
9 use IO::Uncompress::Base ;
13 eval { require IO::Uncompress::Adapter::Inflate; import IO::Uncompress::Adapter::Inflate };
14 eval { require IO::Uncompress::Adapter::Bunzip2; import IO::Uncompress::Adapter::Bunzip2 };
15 eval { require IO::Uncompress::Adapter::LZO; import IO::Uncompress::Adapter::LZO };
17 eval { require IO::Uncompress::Bunzip2; import IO::Uncompress::Bunzip2 };
18 eval { require IO::Uncompress::UnLzop; import IO::Uncompress::UnLzop };
19 eval { require IO::Uncompress::Gunzip; import IO::Uncompress::Gunzip };
20 eval { require IO::Uncompress::Inflate; import IO::Uncompress::Inflate };
21 eval { require IO::Uncompress::RawInflate; import IO::Uncompress::RawInflate };
22 eval { require IO::Uncompress::Unzip; import IO::Uncompress::Unzip };
27 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $AnyUncompressError);
29 $VERSION = '2.000_13';
30 $AnyUncompressError = '';
32 @ISA = qw( Exporter IO::Uncompress::Base );
33 @EXPORT_OK = qw( $AnyUncompressError anyuncompress ) ;
34 %EXPORT_TAGS = %IO::Uncompress::Base::DEFLATE_CONSTANTS ;
35 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
36 Exporter::export_ok_tags('all');
38 # TODO - allow the user to pick a set of the three formats to allow
39 # or just assume want to auto-detect any of the three formats.
44 my $obj = createSelfTiedObject($class, \$AnyUncompressError);
45 $obj->_create(undef, 0, @_);
50 my $obj = createSelfTiedObject(undef, \$AnyUncompressError);
51 return $obj->_inf(@_) ;
64 # any always needs both crc32 and adler32
65 $got->value('CRC32' => 1);
66 $got->value('ADLER32' => 1);
80 if (defined $IO::Uncompress::RawInflate::VERSION )
82 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject();
84 return $self->saveErrorString(undef, $errstr, $errno)
87 *$self->{Uncomp} = $obj;
89 $magic = $self->ckMagic( qw( RawInflate Inflate Gunzip Unzip ) );
92 *$self->{Info} = $self->readHeader($magic)
99 #foreach my $type ( qw( Bunzip2 UnLzop ) ) {
100 if (defined $IO::Uncompress::Bunzip2::VERSION and
101 $magic = $self->ckMagic('Bunzip2')) {
102 *$self->{Info} = $self->readHeader($magic)
105 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject();
107 return $self->saveErrorString(undef, $errstr, $errno)
110 *$self->{Uncomp} = $obj;
114 elsif (defined $IO::Uncompress::UnLzop::VERSION and
115 $magic = $self->ckMagic('UnLzop')) {
117 *$self->{Info} = $self->readHeader($magic)
120 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::LZO::mkUncompObject();
122 return $self->saveErrorString(undef, $errstr, $errno)
125 *$self->{Uncomp} = $obj;
140 my $keep = ref $self ;
141 for my $class ( map { "IO::Uncompress::$_" } @names)
143 bless $self => $class;
144 my $magic = $self->ckMagic();
148 #bless $self => $class;
152 $self->pushBack(*$self->{HeaderPending}) ;
153 *$self->{HeaderPending} = '' ;
156 bless $self => $keep;
168 IO::Uncompress::AnyUncompress - Uncompress gzip, zip, bzip2 or lzop file/buffer
173 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
175 my $status = anyuncompress $input => $output [,OPTS]
176 or die "anyuncompress failed: $AnyUncompressError\n";
178 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
179 or die "anyuncompress failed: $AnyUncompressError\n";
181 $status = $z->read($buffer)
182 $status = $z->read($buffer, $length)
183 $status = $z->read($buffer, $length, $offset)
184 $line = $z->getline()
189 $data = $z->trailingData()
190 $status = $z->nextStream()
191 $data = $z->getHeaderInfo()
193 $z->seek($position, $whence)
199 $AnyUncompressError ;
205 read($z, $buffer, $length);
206 read($z, $buffer, $length, $offset);
208 seek($z, $position, $whence)
219 B<WARNING -- This is a Beta release>.
223 =item * DO NOT use in production code.
225 =item * The documentation is incomplete in places.
227 =item * Parts of the interface defined here are tentative.
229 =item * Please report any problems you find.
236 This module provides a Perl interface that allows the reading of
237 files/buffers that have been compressed with a variety of compression
240 The formats supported are:
248 =item gzip (RFC 1952)
258 The module will auto-detect which, if any, of the supported
259 compression formats is being used.
265 =head1 Functional Interface
267 A top-level function, C<anyuncompress>, is provided to carry out
268 "one-shot" uncompression between buffers and/or files. For finer
269 control over the uncompression process, see the L</"OO Interface">
272 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
274 anyuncompress $input => $output [,OPTS]
275 or die "anyuncompress failed: $AnyUncompressError\n";
279 The functional interface needs Perl5.005 or better.
282 =head2 anyuncompress $input => $output [, OPTS]
285 C<anyuncompress> expects at least two parameters, C<$input> and C<$output>.
287 =head3 The C<$input> parameter
289 The parameter, C<$input>, is used to define the source of
292 It can take one of the following forms:
298 If the C<$input> parameter is a simple scalar, it is assumed to be a
299 filename. This file will be opened for reading and the input data
300 will be read from it.
304 If the C<$input> parameter is a filehandle, the input data will be
306 The string '-' can be used as an alias for standard input.
308 =item A scalar reference
310 If C<$input> is a scalar reference, the input data will be read
313 =item An array reference
315 If C<$input> is an array reference, each element in the array must be a
318 The input data will be read from each file in turn.
320 The complete array will be walked to ensure that it only
321 contains valid filenames before any data is uncompressed.
325 =item An Input FileGlob string
327 If C<$input> is a string that is delimited by the characters "<" and ">"
328 C<anyuncompress> will assume that it is an I<input fileglob string>. The
329 input is the list of files that match the fileglob.
331 If the fileglob does not match any files ...
333 See L<File::GlobMapper|File::GlobMapper> for more details.
338 If the C<$input> parameter is any other type, C<undef> will be returned.
342 =head3 The C<$output> parameter
344 The parameter C<$output> is used to control the destination of the
345 uncompressed data. This parameter can take one of these forms.
351 If the C<$output> parameter is a simple scalar, it is assumed to be a
352 filename. This file will be opened for writing and the uncompressed
353 data will be written to it.
357 If the C<$output> parameter is a filehandle, the uncompressed data
358 will be written to it.
359 The string '-' can be used as an alias for standard output.
362 =item A scalar reference
364 If C<$output> is a scalar reference, the uncompressed data will be
365 stored in C<$$output>.
369 =item An Array Reference
371 If C<$output> is an array reference, the uncompressed data will be
372 pushed onto the array.
374 =item An Output FileGlob
376 If C<$output> is a string that is delimited by the characters "<" and ">"
377 C<anyuncompress> will assume that it is an I<output fileglob string>. The
378 output is the list of files that match the fileglob.
380 When C<$output> is an fileglob string, C<$input> must also be a fileglob
381 string. Anything else is an error.
385 If the C<$output> parameter is any other type, C<undef> will be returned.
392 When C<$input> maps to multiple compressed files/buffers and C<$output> is
393 a single file/buffer, after uncompression C<$output> will contain a
394 concatenation of all the uncompressed data from each of the input
401 =head2 Optional Parameters
403 Unless specified below, the optional parameters for C<anyuncompress>,
404 C<OPTS>, are the same as those used with the OO interface defined in the
405 L</"Constructor Options"> section below.
409 =item C<< AutoClose => 0|1 >>
411 This option applies to any input or output data streams to
412 C<anyuncompress> that are filehandles.
414 If C<AutoClose> is specified, and the value is true, it will result in all
415 input and/or output filehandles being closed once C<anyuncompress> has
418 This parameter defaults to 0.
421 =item C<< BinModeOut => 0|1 >>
423 When writing to a file or filehandle, set C<binmode> before writing to the
432 =item C<< Append => 0|1 >>
436 =item C<< MultiStream => 0|1 >>
438 If the input file/buffer contains multiple compressed data streams, this
439 option will uncompress the whole lot as a single data stream.
452 To read the contents of the file C<file1.txt.Compressed> and write the
453 compressed data to the file C<file1.txt>.
457 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
459 my $input = "file1.txt.Compressed";
460 my $output = "file1.txt";
461 anyuncompress $input => $output
462 or die "anyuncompress failed: $AnyUncompressError\n";
465 To read from an existing Perl filehandle, C<$input>, and write the
466 uncompressed data to a buffer, C<$buffer>.
470 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
473 my $input = new IO::File "<file1.txt.Compressed"
474 or die "Cannot open 'file1.txt.Compressed': $!\n" ;
476 anyuncompress $input => \$buffer
477 or die "anyuncompress failed: $AnyUncompressError\n";
479 To uncompress all files in the directory "/my/home" that match "*.txt.Compressed" and store the compressed data in the same directory
483 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
485 anyuncompress '</my/home/*.txt.Compressed>' => '</my/home/#1.txt>'
486 or die "anyuncompress failed: $AnyUncompressError\n";
488 and if you want to compress each file one at a time, this will do the trick
492 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
494 for my $input ( glob "/my/home/*.txt.Compressed" )
497 $output =~ s/.Compressed// ;
498 anyuncompress $input => $output
499 or die "Error compressing '$input': $AnyUncompressError\n";
506 The format of the constructor for IO::Uncompress::AnyUncompress is shown below
509 my $z = new IO::Uncompress::AnyUncompress $input [OPTS]
510 or die "IO::Uncompress::AnyUncompress failed: $AnyUncompressError\n";
512 Returns an C<IO::Uncompress::AnyUncompress> object on success and undef on failure.
513 The variable C<$AnyUncompressError> will contain an error message on failure.
515 If you are running Perl 5.005 or better the object, C<$z>, returned from
516 IO::Uncompress::AnyUncompress can be used exactly like an L<IO::File|IO::File> filehandle.
517 This means that all normal input file operations can be carried out with
518 C<$z>. For example, to read a line from a compressed file/buffer you can
519 use either of these forms
521 $line = $z->getline();
524 The mandatory parameter C<$input> is used to determine the source of the
525 compressed data. This parameter can take one of three forms.
531 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
532 file will be opened for reading and the compressed data will be read from it.
536 If the C<$input> parameter is a filehandle, the compressed data will be
538 The string '-' can be used as an alias for standard input.
541 =item A scalar reference
543 If C<$input> is a scalar reference, the compressed data will be read from
548 =head2 Constructor Options
551 The option names defined below are case insensitive and can be optionally
552 prefixed by a '-'. So all of the following are valid
559 OPTS is a combination of the following options:
563 =item C<< AutoClose => 0|1 >>
565 This option is only valid when the C<$input> parameter is a filehandle. If
566 specified, and the value is true, it will result in the file being closed once
567 either the C<close> method is called or the IO::Uncompress::AnyUncompress object is
570 This parameter defaults to 0.
572 =item C<< MultiStream => 0|1 >>
576 Allows multiple concatenated compressed streams to be treated as a single
577 compressed stream. Decompression will stop once either the end of the
578 file/buffer is reached, an error is encountered (premature eof, corrupt
579 compressed data) or the end of a stream is not immediately followed by the
580 start of another stream.
582 This parameter defaults to 0.
585 =item C<< Prime => $string >>
587 This option will uncompress the contents of C<$string> before processing the
590 This option can be useful when the compressed data is embedded in another
591 file/data structure and it is not possible to work out where the compressed
592 data begins without having to read the first few bytes. If this is the
593 case, the uncompression can be I<primed> with these bytes using this
596 =item C<< Transparent => 0|1 >>
598 If this option is set and the input file or buffer is not compressed data,
599 the module will allow reading of it anyway.
601 This option defaults to 1.
603 =item C<< BlockSize => $num >>
605 When reading the compressed input data, IO::Uncompress::AnyUncompress will read it in
606 blocks of C<$num> bytes.
608 This option defaults to 4096.
610 =item C<< InputLength => $size >>
612 When present this option will limit the number of compressed bytes read
613 from the input file/buffer to C<$size>. This option can be used in the
614 situation where there is useful data directly after the compressed data
615 stream and you know beforehand the exact length of the compressed data
618 This option is mostly used when reading from a filehandle, in which case
619 the file pointer will be left pointing to the first byte directly after the
620 compressed data stream.
624 This option defaults to off.
626 =item C<< Append => 0|1 >>
628 This option controls what the C<read> method does with uncompressed data.
630 If set to 1, all uncompressed data will be appended to the output parameter
631 of the C<read> method.
633 If set to 0, the contents of the output parameter of the C<read> method
634 will be overwritten by the uncompressed data.
638 =item C<< Strict => 0|1 >>
642 This option controls whether the extra checks defined below are used when
643 carrying out the decompression. When Strict is on, the extra tests are
644 carried out, when Strict is off they are not.
646 The default for this option is off.
673 $status = $z->read($buffer)
675 Reads a block of compressed data (the size the the compressed block is
676 determined by the C<Buffer> option in the constructor), uncompresses it and
677 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
678 set in the constructor, the uncompressed data will be appended to the
679 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
681 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
682 or a negative number on error.
688 $status = $z->read($buffer, $length)
689 $status = $z->read($buffer, $length, $offset)
691 $status = read($z, $buffer, $length)
692 $status = read($z, $buffer, $length, $offset)
694 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
696 The main difference between this form of the C<read> method and the
697 previous one, is that this one will attempt to return I<exactly> C<$length>
698 bytes. The only circumstances that this function will not is if end-of-file
699 or an IO error is encountered.
701 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
702 or a negative number on error.
709 $line = $z->getline()
714 This method fully supports the use of of the variable C<$/>
715 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
716 determine what constitutes an end of line. Both paragraph mode and file
717 slurp mode are supported.
726 Read a single character.
732 $char = $z->ungetc($string)
741 $hdr = $z->getHeaderInfo();
742 @hdrs = $z->getHeaderInfo();
744 This method returns either a hash reference (in scalar context) or a list
745 or hash references (in array context) that contains information about each
746 of the header fields in the compressed data stream(s).
758 Returns the uncompressed file offset.
769 Returns true if the end of the compressed input stream has been reached.
775 $z->seek($position, $whence);
776 seek($z, $position, $whence);
781 Provides a sub-set of the C<seek> functionality, with the restriction
782 that it is only legal to seek forward in the input file/buffer.
783 It is a fatal error to attempt to seek backward.
787 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
788 SEEK_CUR or SEEK_END.
790 Returns 1 on success, 0 on failure.
799 This is a noop provided for completeness.
805 Returns true if the object currently refers to a opened file/buffer.
809 my $prev = $z->autoflush()
810 my $prev = $z->autoflush(EXPR)
812 If the C<$z> object is associated with a file or a filehandle, this method
813 returns the current autoflush setting for the underlying filehandle. If
814 C<EXPR> is present, and is non-zero, it will enable flushing after every
815 write/print operation.
817 If C<$z> is associated with a buffer, this method has no effect and always
820 B<Note> that the special variable C<$|> B<cannot> be used to set or
821 retrieve the autoflush setting.
823 =head2 input_line_number
825 $z->input_line_number()
826 $z->input_line_number(EXPR)
830 Returns the current uncompressed line number. If C<EXPR> is present it has
831 the effect of setting the line number. Note that setting the line number
832 does not change the current position within the file/buffer being read.
834 The contents of C<$/> are used to to determine what constitutes a line
844 If the C<$z> object is associated with a file or a filehandle, this method
845 will return the underlying file descriptor.
847 If the C<$z> object is is associated with a buffer, this method will
857 Closes the output file/buffer.
861 For most versions of Perl this method will be automatically invoked if
862 the IO::Uncompress::AnyUncompress object is destroyed (either explicitly or by the
863 variable with the reference to the object going out of scope). The
864 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
865 these cases, the C<close> method will be called automatically, but
866 not until global destruction of all live objects when the program is
869 Therefore, if you want your scripts to be able to run on all versions
870 of Perl, you should call C<close> explicitly and not rely on automatic
873 Returns true on success, otherwise 0.
875 If the C<AutoClose> option has been enabled when the IO::Uncompress::AnyUncompress
876 object was created, and the object is associated with a file, the
877 underlying file will also be closed.
886 my $status = $z->nextStream();
888 Skips to the next compressed data stream in the input file/buffer. If a new
889 compressed data stream is found, the eof marker will be cleared, C<$.> will
892 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
893 error was encountered.
899 my $data = $z->trailingData();
901 Returns any data that
905 No symbolic constants are required by this IO::Uncompress::AnyUncompress at present.
911 Imports C<anyuncompress> and C<$AnyUncompressError>.
914 use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError) ;
925 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::Uncompress::AnyInflate>
927 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
929 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
930 L<Archive::Tar|Archive::Tar>,
939 This module was written by Paul Marquess, F<pmqs@cpan.org>.
943 =head1 MODIFICATION HISTORY
945 See the Changes file.
947 =head1 COPYRIGHT AND LICENSE
949 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
951 This program is free software; you can redistribute it and/or
952 modify it under the same terms as Perl itself.