1 package IO::Uncompress::Bunzip2 ;
7 use IO::Compress::Base::Common 2.021 qw(:Status createSelfTiedObject);
9 use IO::Uncompress::Base 2.021 ;
10 use IO::Uncompress::Adapter::Bunzip2 2.021 ;
13 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $Bunzip2Error);
18 @ISA = qw( Exporter IO::Uncompress::Base );
19 @EXPORT_OK = qw( $Bunzip2Error bunzip2 ) ;
20 #%EXPORT_TAGS = %IO::Uncompress::Base::EXPORT_TAGS ;
21 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
22 #Exporter::export_ok_tags('all');
28 my $obj = createSelfTiedObject($class, \$Bunzip2Error);
30 $obj->_create(undef, 0, @_);
35 my $obj = createSelfTiedObject(undef, \$Bunzip2Error);
36 return $obj->_inf(@_);
43 use IO::Compress::Base::Common 2.021 qw(:Parse);
46 'Verbosity' => [1, 1, Parse_boolean, 0],
47 'Small' => [1, 1, Parse_boolean, 0],
65 my $magic = $self->ckMagic()
68 *$self->{Info} = $self->readHeader($magic)
71 my $Small = $got->value('Small');
72 my $Verbosity = $got->value('Verbosity');
74 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject(
77 return $self->saveErrorString(undef, $errstr, $errno)
80 *$self->{Uncomp} = $obj;
92 $self->smartReadExact(\$magic, 4);
94 *$self->{HeaderPending} = $magic ;
96 return $self->HeaderError("Header size is " .
98 if length $magic != 4;
100 return $self->HeaderError("Bad Magic.")
101 if ! isBzip2Magic($magic) ;
104 *$self->{Type} = 'bzip2';
113 $self->pushBack($magic);
114 *$self->{HeaderPending} = '';
119 'FingerprintLength' => 4,
121 'TrailerLength' => 0,
137 return $buffer =~ /^BZh\d$/;
147 IO::Uncompress::Bunzip2 - Read bzip2 files/buffers
151 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
153 my $status = bunzip2 $input => $output [,OPTS]
154 or die "bunzip2 failed: $Bunzip2Error\n";
156 my $z = new IO::Uncompress::Bunzip2 $input [OPTS]
157 or die "bunzip2 failed: $Bunzip2Error\n";
159 $status = $z->read($buffer)
160 $status = $z->read($buffer, $length)
161 $status = $z->read($buffer, $length, $offset)
162 $line = $z->getline()
167 $data = $z->trailingData()
168 $status = $z->nextStream()
169 $data = $z->getHeaderInfo()
171 $z->seek($position, $whence)
183 read($z, $buffer, $length);
184 read($z, $buffer, $length, $offset);
186 seek($z, $position, $whence)
194 This module provides a Perl interface that allows the reading of
197 For writing bzip2 files/buffers, see the companion module IO::Compress::Bzip2.
199 =head1 Functional Interface
201 A top-level function, C<bunzip2>, is provided to carry out
202 "one-shot" uncompression between buffers and/or files. For finer
203 control over the uncompression process, see the L</"OO Interface">
206 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
208 bunzip2 $input => $output [,OPTS]
209 or die "bunzip2 failed: $Bunzip2Error\n";
211 The functional interface needs Perl5.005 or better.
213 =head2 bunzip2 $input => $output [, OPTS]
215 C<bunzip2> expects at least two parameters, C<$input> and C<$output>.
217 =head3 The C<$input> parameter
219 The parameter, C<$input>, is used to define the source of
222 It can take one of the following forms:
228 If the C<$input> parameter is a simple scalar, it is assumed to be a
229 filename. This file will be opened for reading and the input data
230 will be read from it.
234 If the C<$input> parameter is a filehandle, the input data will be
236 The string '-' can be used as an alias for standard input.
238 =item A scalar reference
240 If C<$input> is a scalar reference, the input data will be read
243 =item An array reference
245 If C<$input> is an array reference, each element in the array must be a
248 The input data will be read from each file in turn.
250 The complete array will be walked to ensure that it only
251 contains valid filenames before any data is uncompressed.
253 =item An Input FileGlob string
255 If C<$input> is a string that is delimited by the characters "<" and ">"
256 C<bunzip2> will assume that it is an I<input fileglob string>. The
257 input is the list of files that match the fileglob.
259 If the fileglob does not match any files ...
261 See L<File::GlobMapper|File::GlobMapper> for more details.
265 If the C<$input> parameter is any other type, C<undef> will be returned.
267 =head3 The C<$output> parameter
269 The parameter C<$output> is used to control the destination of the
270 uncompressed data. This parameter can take one of these forms.
276 If the C<$output> parameter is a simple scalar, it is assumed to be a
277 filename. This file will be opened for writing and the uncompressed
278 data will be written to it.
282 If the C<$output> parameter is a filehandle, the uncompressed data
283 will be written to it.
284 The string '-' can be used as an alias for standard output.
286 =item A scalar reference
288 If C<$output> is a scalar reference, the uncompressed data will be
289 stored in C<$$output>.
291 =item An Array Reference
293 If C<$output> is an array reference, the uncompressed data will be
294 pushed onto the array.
296 =item An Output FileGlob
298 If C<$output> is a string that is delimited by the characters "<" and ">"
299 C<bunzip2> will assume that it is an I<output fileglob string>. The
300 output is the list of files that match the fileglob.
302 When C<$output> is an fileglob string, C<$input> must also be a fileglob
303 string. Anything else is an error.
307 If the C<$output> parameter is any other type, C<undef> will be returned.
311 When C<$input> maps to multiple compressed files/buffers and C<$output> is
312 a single file/buffer, after uncompression C<$output> will contain a
313 concatenation of all the uncompressed data from each of the input
316 =head2 Optional Parameters
318 Unless specified below, the optional parameters for C<bunzip2>,
319 C<OPTS>, are the same as those used with the OO interface defined in the
320 L</"Constructor Options"> section below.
324 =item C<< AutoClose => 0|1 >>
326 This option applies to any input or output data streams to
327 C<bunzip2> that are filehandles.
329 If C<AutoClose> is specified, and the value is true, it will result in all
330 input and/or output filehandles being closed once C<bunzip2> has
333 This parameter defaults to 0.
335 =item C<< BinModeOut => 0|1 >>
337 When writing to a file or filehandle, set C<binmode> before writing to the
342 =item C<< Append => 0|1 >>
346 =item C<< MultiStream => 0|1 >>
348 If the input file/buffer contains multiple compressed data streams, this
349 option will uncompress the whole lot as a single data stream.
353 =item C<< TrailingData => $scalar >>
355 Returns the data, if any, that is present immediately after the compressed
356 data stream once uncompression is complete.
358 This option can be used when there is useful information immediately
359 following the compressed data stream, and you don't know the length of the
360 compressed data stream.
362 If the input is a buffer, C<trailingData> will return everything from the
363 end of the compressed data stream to the end of the buffer.
365 If the input is a filehandle, C<trailingData> will return the data that is
366 left in the filehandle input buffer once the end of the compressed data
367 stream has been reached. You can then use the filehandle to read the rest
370 Don't bother using C<trailingData> if the input is a filename.
372 If you know the length of the compressed data stream before you start
373 uncompressing, you can avoid having to use C<trailingData> by setting the
374 C<InputLength> option.
380 To read the contents of the file C<file1.txt.bz2> and write the
381 compressed data to the file C<file1.txt>.
385 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
387 my $input = "file1.txt.bz2";
388 my $output = "file1.txt";
389 bunzip2 $input => $output
390 or die "bunzip2 failed: $Bunzip2Error\n";
392 To read from an existing Perl filehandle, C<$input>, and write the
393 uncompressed data to a buffer, C<$buffer>.
397 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
400 my $input = new IO::File "<file1.txt.bz2"
401 or die "Cannot open 'file1.txt.bz2': $!\n" ;
403 bunzip2 $input => \$buffer
404 or die "bunzip2 failed: $Bunzip2Error\n";
406 To uncompress all files in the directory "/my/home" that match "*.txt.bz2" and store the compressed data in the same directory
410 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
412 bunzip2 '</my/home/*.txt.bz2>' => '</my/home/#1.txt>'
413 or die "bunzip2 failed: $Bunzip2Error\n";
415 and if you want to compress each file one at a time, this will do the trick
419 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
421 for my $input ( glob "/my/home/*.txt.bz2" )
424 $output =~ s/.bz2// ;
425 bunzip2 $input => $output
426 or die "Error compressing '$input': $Bunzip2Error\n";
433 The format of the constructor for IO::Uncompress::Bunzip2 is shown below
435 my $z = new IO::Uncompress::Bunzip2 $input [OPTS]
436 or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
438 Returns an C<IO::Uncompress::Bunzip2> object on success and undef on failure.
439 The variable C<$Bunzip2Error> will contain an error message on failure.
441 If you are running Perl 5.005 or better the object, C<$z>, returned from
442 IO::Uncompress::Bunzip2 can be used exactly like an L<IO::File|IO::File> filehandle.
443 This means that all normal input file operations can be carried out with
444 C<$z>. For example, to read a line from a compressed file/buffer you can
445 use either of these forms
447 $line = $z->getline();
450 The mandatory parameter C<$input> is used to determine the source of the
451 compressed data. This parameter can take one of three forms.
457 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
458 file will be opened for reading and the compressed data will be read from it.
462 If the C<$input> parameter is a filehandle, the compressed data will be
464 The string '-' can be used as an alias for standard input.
466 =item A scalar reference
468 If C<$input> is a scalar reference, the compressed data will be read from
473 =head2 Constructor Options
475 The option names defined below are case insensitive and can be optionally
476 prefixed by a '-'. So all of the following are valid
483 OPTS is a combination of the following options:
487 =item C<< AutoClose => 0|1 >>
489 This option is only valid when the C<$input> parameter is a filehandle. If
490 specified, and the value is true, it will result in the file being closed once
491 either the C<close> method is called or the IO::Uncompress::Bunzip2 object is
494 This parameter defaults to 0.
496 =item C<< MultiStream => 0|1 >>
498 Allows multiple concatenated compressed streams to be treated as a single
499 compressed stream. Decompression will stop once either the end of the
500 file/buffer is reached, an error is encountered (premature eof, corrupt
501 compressed data) or the end of a stream is not immediately followed by the
502 start of another stream.
504 This parameter defaults to 0.
506 =item C<< Prime => $string >>
508 This option will uncompress the contents of C<$string> before processing the
511 This option can be useful when the compressed data is embedded in another
512 file/data structure and it is not possible to work out where the compressed
513 data begins without having to read the first few bytes. If this is the
514 case, the uncompression can be I<primed> with these bytes using this
517 =item C<< Transparent => 0|1 >>
519 If this option is set and the input file/buffer is not compressed data,
520 the module will allow reading of it anyway.
522 In addition, if the input file/buffer does contain compressed data and
523 there is non-compressed data immediately following it, setting this option
524 will make this module treat the whole file/bufffer as a single data stream.
526 This option defaults to 1.
528 =item C<< BlockSize => $num >>
530 When reading the compressed input data, IO::Uncompress::Bunzip2 will read it in
531 blocks of C<$num> bytes.
533 This option defaults to 4096.
535 =item C<< InputLength => $size >>
537 When present this option will limit the number of compressed bytes read
538 from the input file/buffer to C<$size>. This option can be used in the
539 situation where there is useful data directly after the compressed data
540 stream and you know beforehand the exact length of the compressed data
543 This option is mostly used when reading from a filehandle, in which case
544 the file pointer will be left pointing to the first byte directly after the
545 compressed data stream.
547 This option defaults to off.
549 =item C<< Append => 0|1 >>
551 This option controls what the C<read> method does with uncompressed data.
553 If set to 1, all uncompressed data will be appended to the output parameter
554 of the C<read> method.
556 If set to 0, the contents of the output parameter of the C<read> method
557 will be overwritten by the uncompressed data.
561 =item C<< Strict => 0|1 >>
563 This option is a no-op.
565 =item C<< Small => 0|1 >>
567 When non-zero this options will make bzip2 use a decompression algorithm
568 that uses less memory at the expense of increasing the amount of time
569 taken for decompression.
585 $status = $z->read($buffer)
587 Reads a block of compressed data (the size the the compressed block is
588 determined by the C<Buffer> option in the constructor), uncompresses it and
589 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
590 set in the constructor, the uncompressed data will be appended to the
591 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
593 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
594 or a negative number on error.
600 $status = $z->read($buffer, $length)
601 $status = $z->read($buffer, $length, $offset)
603 $status = read($z, $buffer, $length)
604 $status = read($z, $buffer, $length, $offset)
606 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
608 The main difference between this form of the C<read> method and the
609 previous one, is that this one will attempt to return I<exactly> C<$length>
610 bytes. The only circumstances that this function will not is if end-of-file
611 or an IO error is encountered.
613 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
614 or a negative number on error.
620 $line = $z->getline()
625 This method fully supports the use of of the variable C<$/> (or
626 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
627 determine what constitutes an end of line. Paragraph mode, record mode and
628 file slurp mode are all supported.
636 Read a single character.
642 $char = $z->ungetc($string)
648 $hdr = $z->getHeaderInfo();
649 @hdrs = $z->getHeaderInfo();
651 This method returns either a hash reference (in scalar context) or a list
652 or hash references (in array context) that contains information about each
653 of the header fields in the compressed data stream(s).
662 Returns the uncompressed file offset.
671 Returns true if the end of the compressed input stream has been reached.
675 $z->seek($position, $whence);
676 seek($z, $position, $whence);
678 Provides a sub-set of the C<seek> functionality, with the restriction
679 that it is only legal to seek forward in the input file/buffer.
680 It is a fatal error to attempt to seek backward.
682 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
683 SEEK_CUR or SEEK_END.
685 Returns 1 on success, 0 on failure.
694 This is a noop provided for completeness.
700 Returns true if the object currently refers to a opened file/buffer.
704 my $prev = $z->autoflush()
705 my $prev = $z->autoflush(EXPR)
707 If the C<$z> object is associated with a file or a filehandle, this method
708 returns the current autoflush setting for the underlying filehandle. If
709 C<EXPR> is present, and is non-zero, it will enable flushing after every
710 write/print operation.
712 If C<$z> is associated with a buffer, this method has no effect and always
715 B<Note> that the special variable C<$|> B<cannot> be used to set or
716 retrieve the autoflush setting.
718 =head2 input_line_number
720 $z->input_line_number()
721 $z->input_line_number(EXPR)
723 Returns the current uncompressed line number. If C<EXPR> is present it has
724 the effect of setting the line number. Note that setting the line number
725 does not change the current position within the file/buffer being read.
727 The contents of C<$/> are used to to determine what constitutes a line
735 If the C<$z> object is associated with a file or a filehandle, C<fileno>
736 will return the underlying file descriptor. Once the C<close> method is
737 called C<fileno> will return C<undef>.
739 If the C<$z> object is is associated with a buffer, this method will return
747 Closes the output file/buffer.
749 For most versions of Perl this method will be automatically invoked if
750 the IO::Uncompress::Bunzip2 object is destroyed (either explicitly or by the
751 variable with the reference to the object going out of scope). The
752 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
753 these cases, the C<close> method will be called automatically, but
754 not until global destruction of all live objects when the program is
757 Therefore, if you want your scripts to be able to run on all versions
758 of Perl, you should call C<close> explicitly and not rely on automatic
761 Returns true on success, otherwise 0.
763 If the C<AutoClose> option has been enabled when the IO::Uncompress::Bunzip2
764 object was created, and the object is associated with a file, the
765 underlying file will also be closed.
771 my $status = $z->nextStream();
773 Skips to the next compressed data stream in the input file/buffer. If a new
774 compressed data stream is found, the eof marker will be cleared and C<$.>
777 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
778 error was encountered.
784 my $data = $z->trailingData();
786 Returns the data, if any, that is present immediately after the compressed
787 data stream once uncompression is complete. It only makes sense to call
788 this method once the end of the compressed data stream has been
791 This option can be used when there is useful information immediately
792 following the compressed data stream, and you don't know the length of the
793 compressed data stream.
795 If the input is a buffer, C<trailingData> will return everything from the
796 end of the compressed data stream to the end of the buffer.
798 If the input is a filehandle, C<trailingData> will return the data that is
799 left in the filehandle input buffer once the end of the compressed data
800 stream has been reached. You can then use the filehandle to read the rest
803 Don't bother using C<trailingData> if the input is a filename.
805 If you know the length of the compressed data stream before you start
806 uncompressing, you can avoid having to use C<trailingData> by setting the
807 C<InputLength> option in the constructor.
811 No symbolic constants are required by this IO::Uncompress::Bunzip2 at present.
817 Imports C<bunzip2> and C<$Bunzip2Error>.
820 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
826 =head2 Working with Net::FTP
828 See L<IO::Uncompress::Bunzip2::FAQ|IO::Uncompress::Bunzip2::FAQ/"Compressed files and Net::FTP">
832 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::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
834 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
836 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
837 L<Archive::Tar|Archive::Tar>,
840 The primary site for the bzip2 program is F<http://www.bzip.org>.
842 See the module L<Compress::Bzip2|Compress::Bzip2>
846 This module was written by Paul Marquess, F<pmqs@cpan.org>.
848 =head1 MODIFICATION HISTORY
850 See the Changes file.
852 =head1 COPYRIGHT AND LICENSE
854 Copyright (c) 2005-2008 Paul Marquess. All rights reserved.
856 This program is free software; you can redistribute it and/or
857 modify it under the same terms as Perl itself.