1 package IO::Uncompress::Inflate ;
6 use IO::Uncompress::Gunzip ;
10 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $InflateError);
12 $VERSION = '2.000_05';
15 @ISA = qw( Exporter IO::BaseInflate );
16 @EXPORT_OK = qw( $InflateError inflate ) ;
17 %EXPORT_TAGS = %IO::BaseInflate::EXPORT_TAGS ;
18 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
19 Exporter::export_ok_tags('all');
25 return IO::BaseInflate::new($pkg, 'rfc1950', undef, \$InflateError, 0, @_);
30 return IO::BaseInflate::_inf(__PACKAGE__, 'rfc1950', \$InflateError, @_);
40 IO::Uncompress::Inflate - Perl interface to read RFC 1950 files/buffers
44 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
46 my $status = inflate $input => $output [,OPTS]
47 or die "inflate failed: $InflateError\n";
49 my $z = new IO::Uncompress::Inflate $input [OPTS]
50 or die "inflate failed: $InflateError\n";
52 $status = $z->read($buffer)
53 $status = $z->read($buffer, $length)
54 $status = $z->read($buffer, $length, $offset)
58 $status = $z->inflateSync()
60 $data = $z->getHeaderInfo()
62 $z->seek($position, $whence)
74 read($z, $buffer, $length);
75 read($z, $buffer, $length, $offset);
77 seek($z, $position, $whence)
88 B<WARNING -- This is a Beta release>.
92 =item * DO NOT use in production code.
94 =item * The documentation is incomplete in places.
96 =item * Parts of the interface defined here are tentative.
98 =item * Please report any problems you find.
106 This module provides a Perl interface that allows the reading of
107 files/buffers that conform to RFC 1950.
109 For writing RFC 1950 files/buffers, see the companion module
110 IO::Compress::Deflate.
114 =head1 Functional Interface
116 A top-level function, C<inflate>, is provided to carry out "one-shot"
117 uncompression between buffers and/or files. For finer control over the uncompression process, see the L</"OO Interface"> section.
119 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
121 inflate $input => $output [,OPTS]
122 or die "inflate failed: $InflateError\n";
124 inflate \%hash [,OPTS]
125 or die "inflate failed: $InflateError\n";
127 The functional interface needs Perl5.005 or better.
130 =head2 inflate $input => $output [, OPTS]
132 If the first parameter is not a hash reference C<inflate> expects
133 at least two parameters, C<$input> and C<$output>.
135 =head3 The C<$input> parameter
137 The parameter, C<$input>, is used to define the source of
140 It can take one of the following forms:
146 If the C<$input> parameter is a simple scalar, it is assumed to be a
147 filename. This file will be opened for reading and the input data
148 will be read from it.
152 If the C<$input> parameter is a filehandle, the input data will be
154 The string '-' can be used as an alias for standard input.
156 =item A scalar reference
158 If C<$input> is a scalar reference, the input data will be read
161 =item An array reference
163 If C<$input> is an array reference, the input data will be read from each
164 element of the array in turn. The action taken by C<inflate> with
165 each element of the array will depend on the type of data stored
166 in it. You can mix and match any of the types defined in this list,
167 excluding other array or hash references.
168 The complete array will be walked to ensure that it only
169 contains valid data types before any data is uncompressed.
171 =item An Input FileGlob string
173 If C<$input> is a string that is delimited by the characters "<" and ">"
174 C<inflate> will assume that it is an I<input fileglob string>. The
175 input is the list of files that match the fileglob.
177 If the fileglob does not match any files ...
179 See L<File::GlobMapper|File::GlobMapper> for more details.
184 If the C<$input> parameter is any other type, C<undef> will be returned.
188 =head3 The C<$output> parameter
190 The parameter C<$output> is used to control the destination of the
191 uncompressed data. This parameter can take one of these forms.
197 If the C<$output> parameter is a simple scalar, it is assumed to be a filename.
198 This file will be opened for writing and the uncompressed data will be
203 If the C<$output> parameter is a filehandle, the uncompressed data will
205 The string '-' can be used as an alias for standard output.
208 =item A scalar reference
210 If C<$output> is a scalar reference, the uncompressed data will be stored
214 =item A Hash Reference
216 If C<$output> is a hash reference, the uncompressed data will be written
217 to C<$output{$input}> as a scalar reference.
219 When C<$output> is a hash reference, C<$input> must be either a filename or
220 list of filenames. Anything else is an error.
223 =item An Array Reference
225 If C<$output> is an array reference, the uncompressed data will be pushed
228 =item An Output FileGlob
230 If C<$output> is a string that is delimited by the characters "<" and ">"
231 C<inflate> will assume that it is an I<output fileglob string>. The
232 output is the list of files that match the fileglob.
234 When C<$output> is an fileglob string, C<$input> must also be a fileglob
235 string. Anything else is an error.
239 If the C<$output> parameter is any other type, C<undef> will be returned.
241 =head2 inflate \%hash [, OPTS]
243 If the first parameter is a hash reference, C<\%hash>, this will be used to
244 define both the source of compressed data and to control where the
245 uncompressed data is output. Each key/value pair in the hash defines a
246 mapping between an input filename, stored in the key, and an output
247 file/buffer, stored in the value. Although the input can only be a filename,
248 there is more flexibility to control the destination of the uncompressed
249 data. This is determined by the type of the value. Valid types are
255 If the value is C<undef> the uncompressed data will be written to the
256 value as a scalar reference.
260 If the value is a simple scalar, it is assumed to be a filename. This file will
261 be opened for writing and the uncompressed data will be written to it.
265 If the value is a filehandle, the uncompressed data will be
267 The string '-' can be used as an alias for standard output.
270 =item A scalar reference
272 If the value is a scalar reference, the uncompressed data will be stored
273 in the buffer that is referenced by the scalar.
276 =item A Hash Reference
278 If the value is a hash reference, the uncompressed data will be written
279 to C<$hash{$input}> as a scalar reference.
281 =item An Array Reference
283 If C<$output> is an array reference, the uncompressed data will be pushed
288 Any other type is a error.
292 When C<$input> maps to multiple files/buffers and C<$output> is a single
293 file/buffer the uncompressed input files/buffers will all be stored in
294 C<$output> as a single uncompressed stream.
298 =head2 Optional Parameters
300 Unless specified below, the optional parameters for C<inflate>,
301 C<OPTS>, are the same as those used with the OO interface defined in the
302 L</"Constructor Options"> section below.
306 =item AutoClose =E<gt> 0|1
308 This option applies to any input or output data streams to C<inflate>
309 that are filehandles.
311 If C<AutoClose> is specified, and the value is true, it will result in all
312 input and/or output filehandles being closed once C<inflate> has
315 This parameter defaults to 0.
319 =item -Append =E<gt> 0|1
332 To read the contents of the file C<file1.txt.1950> and write the
333 compressed data to the file C<file1.txt>.
337 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
339 my $input = "file1.txt.1950";
340 my $output = "file1.txt";
341 inflate $input => $output
342 or die "inflate failed: $InflateError\n";
345 To read from an existing Perl filehandle, C<$input>, and write the
346 uncompressed data to a buffer, C<$buffer>.
350 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
353 my $input = new IO::File "<file1.txt.1950"
354 or die "Cannot open 'file1.txt.1950': $!\n" ;
356 inflate $input => \$buffer
357 or die "inflate failed: $InflateError\n";
359 To uncompress all files in the directory "/my/home" that match "*.txt.1950" and store the compressed data in the same directory
363 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
365 inflate '</my/home/*.txt.1950>' => '</my/home/#1.txt>'
366 or die "inflate failed: $InflateError\n";
368 and if you want to compress each file one at a time, this will do the trick
372 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
374 for my $input ( glob "/my/home/*.txt.1950" )
377 $output =~ s/.1950// ;
378 inflate $input => $output
379 or die "Error compressing '$input': $InflateError\n";
386 The format of the constructor for IO::Uncompress::Inflate is shown below
389 my $z = new IO::Uncompress::Inflate $input [OPTS]
390 or die "IO::Uncompress::Inflate failed: $InflateError\n";
392 Returns an C<IO::Uncompress::Inflate> object on success and undef on failure.
393 The variable C<$InflateError> will contain an error message on failure.
395 If you are running Perl 5.005 or better the object, C<$z>, returned from
396 IO::Uncompress::Inflate can be used exactly like an L<IO::File|IO::File> filehandle.
397 This means that all normal input file operations can be carried out with C<$z>.
398 For example, to read a line from a compressed file/buffer you can use either
401 $line = $z->getline();
404 The mandatory parameter C<$input> is used to determine the source of the
405 compressed data. This parameter can take one of three forms.
411 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
412 file will be opened for reading and the compressed data will be read from it.
416 If the C<$input> parameter is a filehandle, the compressed data will be
418 The string '-' can be used as an alias for standard input.
421 =item A scalar reference
423 If C<$input> is a scalar reference, the compressed data will be read from
428 =head2 Constructor Options
431 The option names defined below are case insensitive and can be optionally
432 prefixed by a '-'. So all of the following are valid
439 OPTS is a combination of the following options:
443 =item -AutoClose =E<gt> 0|1
445 This option is only valid when the C<$input> parameter is a filehandle. If
446 specified, and the value is true, it will result in the file being closed once
447 either the C<close> method is called or the IO::Uncompress::Inflate object is
450 This parameter defaults to 0.
452 =item -MultiStream =E<gt> 0|1
456 Allows multiple concatenated compressed streams to be treated as a single
457 compressed stream. Decompression will stop once either the end of the
458 file/buffer is reached, an error is encountered (premature eof, corrupt
459 compressed data) or the end of a stream is not immediately followed by the
460 start of another stream.
462 This parameter defaults to 0.
466 =item -Prime =E<gt> $string
468 This option will uncompress the contents of C<$string> before processing the
471 This option can be useful when the compressed data is embedded in another
472 file/data structure and it is not possible to work out where the compressed
473 data begins without having to read the first few bytes. If this is the case,
474 the uncompression can be I<primed> with these bytes using this option.
476 =item -Transparent =E<gt> 0|1
478 If this option is set and the input file or buffer is not compressed data,
479 the module will allow reading of it anyway.
481 This option defaults to 1.
483 =item -BlockSize =E<gt> $num
485 When reading the compressed input data, IO::Uncompress::Inflate will read it in blocks
488 This option defaults to 4096.
490 =item -InputLength =E<gt> $size
492 When present this option will limit the number of compressed bytes read from
493 the input file/buffer to C<$size>. This option can be used in the situation
494 where there is useful data directly after the compressed data stream and you
495 know beforehand the exact length of the compressed data stream.
497 This option is mostly used when reading from a filehandle, in which case the
498 file pointer will be left pointing to the first byte directly after the
499 compressed data stream.
503 This option defaults to off.
505 =item -Append =E<gt> 0|1
507 This option controls what the C<read> method does with uncompressed data.
509 If set to 1, all uncompressed data will be appended to the output parameter of
512 If set to 0, the contents of the output parameter of the C<read> method will be
513 overwritten by the uncompressed data.
517 =item -Strict =E<gt> 0|1
521 This option controls whether the extra checks defined below are used when
522 carrying out the decompression. When Strict is on, the extra tests are carried
523 out, when Strict is off they are not.
525 The default for this option is off.
535 The ADLER32 checksum field must be present.
539 The value of the ADLER32 field read must match the adler32 value of the
540 uncompressed data actually contained in the file.
564 $status = $z->read($buffer)
566 Reads a block of compressed data (the size the the compressed block is
567 determined by the C<Buffer> option in the constructor), uncompresses it and
568 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is set
569 in the constructor, the uncompressed data will be appended to the C<$buffer>
570 parameter. Otherwise C<$buffer> will be overwritten.
572 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof or
573 a negative number on error.
579 $status = $z->read($buffer, $length)
580 $status = $z->read($buffer, $length, $offset)
582 $status = read($z, $buffer, $length)
583 $status = read($z, $buffer, $length, $offset)
585 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
587 The main difference between this form of the C<read> method and the previous
588 one, is that this one will attempt to return I<exactly> C<$length> bytes. The
589 only circumstances that this function will not is if end-of-file or an IO error
592 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof or
593 a negative number on error.
600 $line = $z->getline()
605 This method fully supports the use of of the variable C<$/>
606 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
607 determine what constitutes an end of line. Both paragraph mode and file
608 slurp mode are supported.
617 Read a single character.
623 $char = $z->ungetc($string)
630 $status = $z->inflateSync()
638 $hdr = $z->getHeaderInfo()
646 This method returns a hash reference that contains the contents of each of the
647 header fields defined in RFC1950.
662 Returns the uncompressed file offset.
673 Returns true if the end of the compressed input stream has been reached.
679 $z->seek($position, $whence);
680 seek($z, $position, $whence);
685 Provides a sub-set of the C<seek> functionality, with the restriction
686 that it is only legal to seek forward in the input file/buffer.
687 It is a fatal error to attempt to seek backward.
691 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
692 SEEK_CUR or SEEK_END.
694 Returns 1 on success, 0 on failure.
703 This is a noop provided for completeness.
710 If the C<$z> object is associated with a file, this method will return
711 the underlying filehandle.
713 If the C<$z> object is is associated with a buffer, this method will
723 Closes the output file/buffer.
727 For most versions of Perl this method will be automatically invoked if
728 the IO::Uncompress::Inflate object is destroyed (either explicitly or by the
729 variable with the reference to the object going out of scope). The
730 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
731 these cases, the C<close> method will be called automatically, but
732 not until global destruction of all live objects when the program is
735 Therefore, if you want your scripts to be able to run on all versions
736 of Perl, you should call C<close> explicitly and not rely on automatic
739 Returns true on success, otherwise 0.
741 If the C<AutoClose> option has been enabled when the IO::Uncompress::Inflate
742 object was created, and the object is associated with a file, the
743 underlying file will also be closed.
750 No symbolic constants are required by this IO::Uncompress::Inflate at present.
756 Imports C<inflate> and C<$InflateError>.
759 use IO::Uncompress::Inflate qw(inflate $InflateError) ;
770 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Uncompress::AnyInflate>
772 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
774 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
777 For RFC 1950, 1951 and 1952 see
778 F<http://www.faqs.org/rfcs/rfc1950.html>,
779 F<http://www.faqs.org/rfcs/rfc1951.html> and
780 F<http://www.faqs.org/rfcs/rfc1952.html>
782 The primary site for the gzip program is F<http://www.gzip.org>.
786 The I<IO::Uncompress::Inflate> module was written by Paul Marquess,
787 F<pmqs@cpan.org>. The latest copy of the module can be
788 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
790 The I<zlib> compression library was written by Jean-loup Gailly
791 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
793 The primary site for the I<zlib> compression library is
794 F<http://www.zlib.org>.
796 =head1 MODIFICATION HISTORY
798 See the Changes file.
800 =head1 COPYRIGHT AND LICENSE
803 Copyright (c) 2005 Paul Marquess. All rights reserved.
804 This program is free software; you can redistribute it and/or
805 modify it under the same terms as Perl itself.