1 package IO::Uncompress::RawInflate ;
6 use IO::Uncompress::Gunzip;
9 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $RawInflateError);
11 $VERSION = '2.000_05';
12 $RawInflateError = '';
14 @ISA = qw(Exporter IO::BaseInflate);
15 @EXPORT_OK = qw( $RawInflateError rawinflate ) ;
16 %EXPORT_TAGS = %IO::BaseInflate::EXPORT_TAGS ;
17 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
18 Exporter::export_ok_tags('all');
26 return IO::BaseInflate::new($pkg, 'rfc1951', undef, \$RawInflateError, 0, @_);
31 return IO::BaseInflate::_inf(__PACKAGE__, 'rfc1951', \$RawInflateError, @_);
41 IO::Uncompress::RawInflate - Perl interface to read RFC 1951 files/buffers
45 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
47 my $status = rawinflate $input => $output [,OPTS]
48 or die "rawinflate failed: $RawInflateError\n";
50 my $z = new IO::Uncompress::RawInflate $input [OPTS]
51 or die "rawinflate failed: $RawInflateError\n";
53 $status = $z->read($buffer)
54 $status = $z->read($buffer, $length)
55 $status = $z->read($buffer, $length, $offset)
59 $status = $z->inflateSync()
61 $data = $z->getHeaderInfo()
63 $z->seek($position, $whence)
75 read($z, $buffer, $length);
76 read($z, $buffer, $length, $offset);
78 seek($z, $position, $whence)
89 B<WARNING -- This is a Beta release>.
93 =item * DO NOT use in production code.
95 =item * The documentation is incomplete in places.
97 =item * Parts of the interface defined here are tentative.
99 =item * Please report any problems you find.
107 This module provides a Perl interface that allows the reading of
108 files/buffers that conform to RFC 1951.
110 For writing RFC 1951 files/buffers, see the companion module
111 IO::Compress::RawDeflate.
115 =head1 Functional Interface
117 A top-level function, C<rawinflate>, is provided to carry out "one-shot"
118 uncompression between buffers and/or files. For finer control over the uncompression process, see the L</"OO Interface"> section.
120 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
122 rawinflate $input => $output [,OPTS]
123 or die "rawinflate failed: $RawInflateError\n";
125 rawinflate \%hash [,OPTS]
126 or die "rawinflate failed: $RawInflateError\n";
128 The functional interface needs Perl5.005 or better.
131 =head2 rawinflate $input => $output [, OPTS]
133 If the first parameter is not a hash reference C<rawinflate> expects
134 at least two parameters, C<$input> and C<$output>.
136 =head3 The C<$input> parameter
138 The parameter, C<$input>, is used to define the source of
141 It can take one of the following forms:
147 If the C<$input> parameter is a simple scalar, it is assumed to be a
148 filename. This file will be opened for reading and the input data
149 will be read from it.
153 If the C<$input> parameter is a filehandle, the input data will be
155 The string '-' can be used as an alias for standard input.
157 =item A scalar reference
159 If C<$input> is a scalar reference, the input data will be read
162 =item An array reference
164 If C<$input> is an array reference, the input data will be read from each
165 element of the array in turn. The action taken by C<rawinflate> with
166 each element of the array will depend on the type of data stored
167 in it. You can mix and match any of the types defined in this list,
168 excluding other array or hash references.
169 The complete array will be walked to ensure that it only
170 contains valid data types before any data is uncompressed.
172 =item An Input FileGlob string
174 If C<$input> is a string that is delimited by the characters "<" and ">"
175 C<rawinflate> will assume that it is an I<input fileglob string>. The
176 input is the list of files that match the fileglob.
178 If the fileglob does not match any files ...
180 See L<File::GlobMapper|File::GlobMapper> for more details.
185 If the C<$input> parameter is any other type, C<undef> will be returned.
189 =head3 The C<$output> parameter
191 The parameter C<$output> is used to control the destination of the
192 uncompressed data. This parameter can take one of these forms.
198 If the C<$output> parameter is a simple scalar, it is assumed to be a filename.
199 This file will be opened for writing and the uncompressed data will be
204 If the C<$output> parameter is a filehandle, the uncompressed data will
206 The string '-' can be used as an alias for standard output.
209 =item A scalar reference
211 If C<$output> is a scalar reference, the uncompressed data will be stored
215 =item A Hash Reference
217 If C<$output> is a hash reference, the uncompressed data will be written
218 to C<$output{$input}> as a scalar reference.
220 When C<$output> is a hash reference, C<$input> must be either a filename or
221 list of filenames. Anything else is an error.
224 =item An Array Reference
226 If C<$output> is an array reference, the uncompressed data will be pushed
229 =item An Output FileGlob
231 If C<$output> is a string that is delimited by the characters "<" and ">"
232 C<rawinflate> will assume that it is an I<output fileglob string>. The
233 output is the list of files that match the fileglob.
235 When C<$output> is an fileglob string, C<$input> must also be a fileglob
236 string. Anything else is an error.
240 If the C<$output> parameter is any other type, C<undef> will be returned.
242 =head2 rawinflate \%hash [, OPTS]
244 If the first parameter is a hash reference, C<\%hash>, this will be used to
245 define both the source of compressed data and to control where the
246 uncompressed data is output. Each key/value pair in the hash defines a
247 mapping between an input filename, stored in the key, and an output
248 file/buffer, stored in the value. Although the input can only be a filename,
249 there is more flexibility to control the destination of the uncompressed
250 data. This is determined by the type of the value. Valid types are
256 If the value is C<undef> the uncompressed data will be written to the
257 value as a scalar reference.
261 If the value is a simple scalar, it is assumed to be a filename. This file will
262 be opened for writing and the uncompressed data will be written to it.
266 If the value is a filehandle, the uncompressed data will be
268 The string '-' can be used as an alias for standard output.
271 =item A scalar reference
273 If the value is a scalar reference, the uncompressed data will be stored
274 in the buffer that is referenced by the scalar.
277 =item A Hash Reference
279 If the value is a hash reference, the uncompressed data will be written
280 to C<$hash{$input}> as a scalar reference.
282 =item An Array Reference
284 If C<$output> is an array reference, the uncompressed data will be pushed
289 Any other type is a error.
293 When C<$input> maps to multiple files/buffers and C<$output> is a single
294 file/buffer the uncompressed input files/buffers will all be stored in
295 C<$output> as a single uncompressed stream.
299 =head2 Optional Parameters
301 Unless specified below, the optional parameters for C<rawinflate>,
302 C<OPTS>, are the same as those used with the OO interface defined in the
303 L</"Constructor Options"> section below.
307 =item AutoClose =E<gt> 0|1
309 This option applies to any input or output data streams to C<rawinflate>
310 that are filehandles.
312 If C<AutoClose> is specified, and the value is true, it will result in all
313 input and/or output filehandles being closed once C<rawinflate> has
316 This parameter defaults to 0.
320 =item -Append =E<gt> 0|1
333 To read the contents of the file C<file1.txt.1951> and write the
334 compressed data to the file C<file1.txt>.
338 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
340 my $input = "file1.txt.1951";
341 my $output = "file1.txt";
342 rawinflate $input => $output
343 or die "rawinflate failed: $RawInflateError\n";
346 To read from an existing Perl filehandle, C<$input>, and write the
347 uncompressed data to a buffer, C<$buffer>.
351 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
354 my $input = new IO::File "<file1.txt.1951"
355 or die "Cannot open 'file1.txt.1951': $!\n" ;
357 rawinflate $input => \$buffer
358 or die "rawinflate failed: $RawInflateError\n";
360 To uncompress all files in the directory "/my/home" that match "*.txt.1951" and store the compressed data in the same directory
364 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
366 rawinflate '</my/home/*.txt.1951>' => '</my/home/#1.txt>'
367 or die "rawinflate failed: $RawInflateError\n";
369 and if you want to compress each file one at a time, this will do the trick
373 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
375 for my $input ( glob "/my/home/*.txt.1951" )
378 $output =~ s/.1951// ;
379 rawinflate $input => $output
380 or die "Error compressing '$input': $RawInflateError\n";
387 The format of the constructor for IO::Uncompress::RawInflate is shown below
390 my $z = new IO::Uncompress::RawInflate $input [OPTS]
391 or die "IO::Uncompress::RawInflate failed: $RawInflateError\n";
393 Returns an C<IO::Uncompress::RawInflate> object on success and undef on failure.
394 The variable C<$RawInflateError> will contain an error message on failure.
396 If you are running Perl 5.005 or better the object, C<$z>, returned from
397 IO::Uncompress::RawInflate can be used exactly like an L<IO::File|IO::File> filehandle.
398 This means that all normal input file operations can be carried out with C<$z>.
399 For example, to read a line from a compressed file/buffer you can use either
402 $line = $z->getline();
405 The mandatory parameter C<$input> is used to determine the source of the
406 compressed data. This parameter can take one of three forms.
412 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
413 file will be opened for reading and the compressed data will be read from it.
417 If the C<$input> parameter is a filehandle, the compressed data will be
419 The string '-' can be used as an alias for standard input.
422 =item A scalar reference
424 If C<$input> is a scalar reference, the compressed data will be read from
429 =head2 Constructor Options
432 The option names defined below are case insensitive and can be optionally
433 prefixed by a '-'. So all of the following are valid
440 OPTS is a combination of the following options:
444 =item -AutoClose =E<gt> 0|1
446 This option is only valid when the C<$input> parameter is a filehandle. If
447 specified, and the value is true, it will result in the file being closed once
448 either the C<close> method is called or the IO::Uncompress::RawInflate object is
451 This parameter defaults to 0.
453 =item -MultiStream =E<gt> 0|1
457 This option is a no-op.
461 =item -Prime =E<gt> $string
463 This option will uncompress the contents of C<$string> before processing the
466 This option can be useful when the compressed data is embedded in another
467 file/data structure and it is not possible to work out where the compressed
468 data begins without having to read the first few bytes. If this is the case,
469 the uncompression can be I<primed> with these bytes using this option.
471 =item -Transparent =E<gt> 0|1
473 If this option is set and the input file or buffer is not compressed data,
474 the module will allow reading of it anyway.
476 This option defaults to 1.
478 =item -BlockSize =E<gt> $num
480 When reading the compressed input data, IO::Uncompress::RawInflate will read it in blocks
483 This option defaults to 4096.
485 =item -InputLength =E<gt> $size
487 When present this option will limit the number of compressed bytes read from
488 the input file/buffer to C<$size>. This option can be used in the situation
489 where there is useful data directly after the compressed data stream and you
490 know beforehand the exact length of the compressed data stream.
492 This option is mostly used when reading from a filehandle, in which case the
493 file pointer will be left pointing to the first byte directly after the
494 compressed data stream.
498 This option defaults to off.
500 =item -Append =E<gt> 0|1
502 This option controls what the C<read> method does with uncompressed data.
504 If set to 1, all uncompressed data will be appended to the output parameter of
507 If set to 0, the contents of the output parameter of the C<read> method will be
508 overwritten by the uncompressed data.
512 =item -Strict =E<gt> 0|1
516 This option is a no-op.
534 $status = $z->read($buffer)
536 Reads a block of compressed data (the size the the compressed block is
537 determined by the C<Buffer> option in the constructor), uncompresses it and
538 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is set
539 in the constructor, the uncompressed data will be appended to the C<$buffer>
540 parameter. Otherwise C<$buffer> will be overwritten.
542 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof or
543 a negative number on error.
549 $status = $z->read($buffer, $length)
550 $status = $z->read($buffer, $length, $offset)
552 $status = read($z, $buffer, $length)
553 $status = read($z, $buffer, $length, $offset)
555 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
557 The main difference between this form of the C<read> method and the previous
558 one, is that this one will attempt to return I<exactly> C<$length> bytes. The
559 only circumstances that this function will not is if end-of-file or an IO error
562 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof or
563 a negative number on error.
570 $line = $z->getline()
575 This method fully supports the use of of the variable C<$/>
576 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
577 determine what constitutes an end of line. Both paragraph mode and file
578 slurp mode are supported.
587 Read a single character.
593 $char = $z->ungetc($string)
600 $status = $z->inflateSync()
608 $hdr = $z->getHeaderInfo()
630 Returns the uncompressed file offset.
641 Returns true if the end of the compressed input stream has been reached.
647 $z->seek($position, $whence);
648 seek($z, $position, $whence);
653 Provides a sub-set of the C<seek> functionality, with the restriction
654 that it is only legal to seek forward in the input file/buffer.
655 It is a fatal error to attempt to seek backward.
659 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
660 SEEK_CUR or SEEK_END.
662 Returns 1 on success, 0 on failure.
671 This is a noop provided for completeness.
678 If the C<$z> object is associated with a file, this method will return
679 the underlying filehandle.
681 If the C<$z> object is is associated with a buffer, this method will
691 Closes the output file/buffer.
695 For most versions of Perl this method will be automatically invoked if
696 the IO::Uncompress::RawInflate object is destroyed (either explicitly or by the
697 variable with the reference to the object going out of scope). The
698 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
699 these cases, the C<close> method will be called automatically, but
700 not until global destruction of all live objects when the program is
703 Therefore, if you want your scripts to be able to run on all versions
704 of Perl, you should call C<close> explicitly and not rely on automatic
707 Returns true on success, otherwise 0.
709 If the C<AutoClose> option has been enabled when the IO::Uncompress::RawInflate
710 object was created, and the object is associated with a file, the
711 underlying file will also be closed.
718 No symbolic constants are required by this IO::Uncompress::RawInflate at present.
724 Imports C<rawinflate> and C<$RawInflateError>.
727 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
738 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::AnyInflate>
740 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
742 L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
745 For RFC 1950, 1951 and 1952 see
746 F<http://www.faqs.org/rfcs/rfc1950.html>,
747 F<http://www.faqs.org/rfcs/rfc1951.html> and
748 F<http://www.faqs.org/rfcs/rfc1952.html>
750 The primary site for the gzip program is F<http://www.gzip.org>.
754 The I<IO::Uncompress::RawInflate> module was written by Paul Marquess,
755 F<pmqs@cpan.org>. The latest copy of the module can be
756 found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
758 The I<zlib> compression library was written by Jean-loup Gailly
759 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
761 The primary site for the I<zlib> compression library is
762 F<http://www.zlib.org>.
764 =head1 MODIFICATION HISTORY
766 See the Changes file.
768 =head1 COPYRIGHT AND LICENSE
771 Copyright (c) 2005 Paul Marquess. All rights reserved.
772 This program is free software; you can redistribute it and/or
773 modify it under the same terms as Perl itself.