1 package IO::Uncompress::Bunzip2 ;
7 use IO::Compress::Base::Common 2.206 qw(:Status );
9 use IO::Uncompress::Base 2.206 ;
10 use IO::Uncompress::Adapter::Bunzip2 2.206 ;
13 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $Bunzip2Error);
18 @ISA = qw(IO::Uncompress::Base Exporter);
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 = IO::Compress::Base::Common::createSelfTiedObject($class, \$Bunzip2Error);
30 $obj->_create(undef, 0, @_);
35 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$Bunzip2Error);
36 return $obj->_inf(@_);
42 'verbosity' => [IO::Compress::Base::Common::Parse_boolean, 0],
43 'small' => [IO::Compress::Base::Common::Parse_boolean, 0],
61 my $magic = $self->ckMagic()
64 *$self->{Info} = $self->readHeader($magic)
67 my $Small = $got->getValue('small');
68 my $Verbosity = $got->getValue('verbosity');
70 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Bunzip2::mkUncompObject(
73 return $self->saveErrorString(undef, $errstr, $errno)
76 *$self->{Uncomp} = $obj;
88 $self->smartReadExact(\$magic, 4);
90 *$self->{HeaderPending} = $magic ;
92 return $self->HeaderError("Header size is " .
94 if length $magic != 4;
96 return $self->HeaderError("Bad Magic.")
97 if ! isBzip2Magic($magic) ;
100 *$self->{Type} = 'bzip2';
109 $self->pushBack($magic);
110 *$self->{HeaderPending} = '';
115 'FingerprintLength' => 4,
117 'TrailerLength' => 0,
135 return $buffer =~ qr/^\x42\x5A\x68[\x30-\x39]$/;
145 IO::Uncompress::Bunzip2 - Read bzip2 files/buffers
149 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
151 my $status = bunzip2 $input => $output [,OPTS]
152 or die "bunzip2 failed: $Bunzip2Error\n";
154 my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
155 or die "bunzip2 failed: $Bunzip2Error\n";
157 $status = $z->read($buffer)
158 $status = $z->read($buffer, $length)
159 $status = $z->read($buffer, $length, $offset)
160 $line = $z->getline()
165 $data = $z->trailingData()
166 $status = $z->nextStream()
167 $data = $z->getHeaderInfo()
169 $z->seek($position, $whence)
181 read($z, $buffer, $length);
182 read($z, $buffer, $length, $offset);
184 seek($z, $position, $whence)
192 This module provides a Perl interface that allows the reading of
195 For writing bzip2 files/buffers, see the companion module IO::Compress::Bzip2.
197 =head1 Functional Interface
199 A top-level function, C<bunzip2>, is provided to carry out
200 "one-shot" uncompression between buffers and/or files. For finer
201 control over the uncompression process, see the L</"OO Interface">
204 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
206 bunzip2 $input_filename_or_reference => $output_filename_or_reference [,OPTS]
207 or die "bunzip2 failed: $Bunzip2Error\n";
209 The functional interface needs Perl5.005 or better.
211 =head2 bunzip2 $input_filename_or_reference => $output_filename_or_reference [, OPTS]
213 C<bunzip2> expects at least two parameters,
214 C<$input_filename_or_reference> and C<$output_filename_or_reference>
215 and zero or more optional parameters (see L</Optional Parameters>)
217 =head3 The C<$input_filename_or_reference> parameter
219 The parameter, C<$input_filename_or_reference>, is used to define the
220 source of the compressed data.
222 It can take one of the following forms:
228 If the C<$input_filename_or_reference> parameter is a simple scalar, it is
229 assumed to be a filename. This file will be opened for reading and the
230 input data will be read from it.
234 If the C<$input_filename_or_reference> parameter is a filehandle, the input
235 data will be read from it. The string '-' can be used as an alias for
238 =item A scalar reference
240 If C<$input_filename_or_reference> is a scalar reference, the input data
241 will be read from C<$$input_filename_or_reference>.
243 =item An array reference
245 If C<$input_filename_or_reference> is an array reference, each element in
246 the array must be a filename.
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_filename_or_reference> is a string that is delimited by the
256 characters "<" and ">" C<bunzip2> will assume that it is an
257 I<input fileglob string>. The input is the list of files that match the
260 See L<File::GlobMapper|File::GlobMapper> for more details.
264 If the C<$input_filename_or_reference> parameter is any other type,
265 C<undef> will be returned.
267 =head3 The C<$output_filename_or_reference> parameter
269 The parameter C<$output_filename_or_reference> is used to control the
270 destination of the uncompressed data. This parameter can take one of
277 If the C<$output_filename_or_reference> parameter is a simple scalar, it is
278 assumed to be a filename. This file will be opened for writing and the
279 uncompressed data will be written to it.
283 If the C<$output_filename_or_reference> parameter is a filehandle, the
284 uncompressed data will be written to it. The string '-' can be used as
285 an alias for standard output.
287 =item A scalar reference
289 If C<$output_filename_or_reference> is a scalar reference, the
290 uncompressed data will be stored in C<$$output_filename_or_reference>.
292 =item An Array Reference
294 If C<$output_filename_or_reference> is an array reference,
295 the uncompressed data will be pushed onto the array.
297 =item An Output FileGlob
299 If C<$output_filename_or_reference> is a string that is delimited by the
300 characters "<" and ">" C<bunzip2> will assume that it is an
301 I<output fileglob string>. The output is the list of files that match the
304 When C<$output_filename_or_reference> is an fileglob string,
305 C<$input_filename_or_reference> must also be a fileglob string. Anything
308 See L<File::GlobMapper|File::GlobMapper> for more details.
312 If the C<$output_filename_or_reference> parameter is any other type,
313 C<undef> will be returned.
317 When C<$input_filename_or_reference> maps to multiple compressed
318 files/buffers and C<$output_filename_or_reference> is
319 a single file/buffer, after uncompression C<$output_filename_or_reference> will contain a
320 concatenation of all the uncompressed data from each of the input
323 =head2 Optional Parameters
325 The optional parameters for the one-shot function C<bunzip2>
326 are (for the most part) identical to those used with the OO interface defined in the
327 L</"Constructor Options"> section. The exceptions are listed below
331 =item C<< AutoClose => 0|1 >>
333 This option applies to any input or output data streams to
334 C<bunzip2> that are filehandles.
336 If C<AutoClose> is specified, and the value is true, it will result in all
337 input and/or output filehandles being closed once C<bunzip2> has
340 This parameter defaults to 0.
342 =item C<< BinModeOut => 0|1 >>
344 This option is now a no-op. All files will be written in binmode.
346 =item C<< Append => 0|1 >>
348 The behaviour of this option is dependent on the type of output data
355 If C<Append> is enabled, all uncompressed data will be append to the end of
356 the output buffer. Otherwise the output buffer will be cleared before any
357 uncompressed data is written to it.
361 If C<Append> is enabled, the file will be opened in append mode. Otherwise
362 the contents of the file, if any, will be truncated before any uncompressed
363 data is written to it.
367 If C<Append> is enabled, the filehandle will be positioned to the end of
368 the file via a call to C<seek> before any uncompressed data is
369 written to it. Otherwise the file pointer will not be moved.
373 When C<Append> is specified, and set to true, it will I<append> all uncompressed
374 data to the output data stream.
376 So when the output is a filehandle it will carry out a seek to the eof
377 before writing any uncompressed data. If the output is a filename, it will be opened for
378 appending. If the output is a buffer, all uncompressed data will be
379 appended to the existing buffer.
381 Conversely when C<Append> is not specified, or it is present and is set to
382 false, it will operate as follows.
384 When the output is a filename, it will truncate the contents of the file
385 before writing any uncompressed data. If the output is a filehandle
386 its position will not be changed. If the output is a buffer, it will be
387 wiped before any uncompressed data is output.
391 =item C<< MultiStream => 0|1 >>
393 If the input file/buffer contains multiple compressed data streams, this
394 option will uncompress the whole lot as a single data stream.
398 =item C<< TrailingData => $scalar >>
400 Returns the data, if any, that is present immediately after the compressed
401 data stream once uncompression is complete.
403 This option can be used when there is useful information immediately
404 following the compressed data stream, and you don't know the length of the
405 compressed data stream.
407 If the input is a buffer, C<trailingData> will return everything from the
408 end of the compressed data stream to the end of the buffer.
410 If the input is a filehandle, C<trailingData> will return the data that is
411 left in the filehandle input buffer once the end of the compressed data
412 stream has been reached. You can then use the filehandle to read the rest
415 Don't bother using C<trailingData> if the input is a filename.
417 If you know the length of the compressed data stream before you start
418 uncompressing, you can avoid having to use C<trailingData> by setting the
419 C<InputLength> option.
423 =head2 OneShot Examples
425 To read the contents of the file C<file1.txt.bz2> and write the
426 uncompressed data to the file C<file1.txt>.
430 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
432 my $input = "file1.txt.bz2";
433 my $output = "file1.txt";
434 bunzip2 $input => $output
435 or die "bunzip2 failed: $Bunzip2Error\n";
437 To read from an existing Perl filehandle, C<$input>, and write the
438 uncompressed data to a buffer, C<$buffer>.
442 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
445 my $input = IO::File->new( "<file1.txt.bz2" )
446 or die "Cannot open 'file1.txt.bz2': $!\n" ;
448 bunzip2 $input => \$buffer
449 or die "bunzip2 failed: $Bunzip2Error\n";
451 To uncompress all files in the directory "/my/home" that match "*.txt.bz2" and store the compressed data in the same directory
455 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
457 bunzip2 '</my/home/*.txt.bz2>' => '</my/home/#1.txt>'
458 or die "bunzip2 failed: $Bunzip2Error\n";
460 and if you want to compress each file one at a time, this will do the trick
464 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
466 for my $input ( glob "/my/home/*.txt.bz2" )
469 $output =~ s/.bz2// ;
470 bunzip2 $input => $output
471 or die "Error compressing '$input': $Bunzip2Error\n";
478 The format of the constructor for IO::Uncompress::Bunzip2 is shown below
480 my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
481 or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
483 The constructor takes one mandatory parameter, C<$input>, defined below, and
484 zero or more C<OPTS>, defined in L<Constructor Options>.
486 Returns an C<IO::Uncompress::Bunzip2> object on success and undef on failure.
487 The variable C<$Bunzip2Error> will contain an error message on failure.
489 If you are running Perl 5.005 or better the object, C<$z>, returned from
490 IO::Uncompress::Bunzip2 can be used exactly like an L<IO::File|IO::File> filehandle.
491 This means that all normal input file operations can be carried out with
492 C<$z>. For example, to read a line from a compressed file/buffer you can
493 use either of these forms
495 $line = $z->getline();
498 Below is a simple exaple of using the OO interface to read the compressed file
499 C<myfile.bz2> and write its contents to stdout.
501 my $filename = "myfile.bz2";
502 my $z = IO::Uncompress::Bunzip2->new($filename)
503 or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
510 See L</EXAMPLES> for further examples
512 The mandatory parameter C<$input> is used to determine the source of the
513 compressed data. This parameter can take one of three forms.
519 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
520 file will be opened for reading and the compressed data will be read from it.
524 If the C<$input> parameter is a filehandle, the compressed data will be
526 The string '-' can be used as an alias for standard input.
528 =item A scalar reference
530 If C<$input> is a scalar reference, the compressed data will be read from
535 =head2 Constructor Options
537 The option names defined below are case insensitive and can be optionally
538 prefixed by a '-'. So all of the following are valid
545 OPTS is a combination of the following options:
549 =item C<< AutoClose => 0|1 >>
551 This option is only valid when the C<$input> parameter is a filehandle. If
552 specified, and the value is true, it will result in the file being closed once
553 either the C<close> method is called or the IO::Uncompress::Bunzip2 object is
556 This parameter defaults to 0.
558 =item C<< MultiStream => 0|1 >>
560 Allows multiple concatenated compressed streams to be treated as a single
561 compressed stream. Decompression will stop once either the end of the
562 file/buffer is reached, an error is encountered (premature eof, corrupt
563 compressed data) or the end of a stream is not immediately followed by the
564 start of another stream.
566 This parameter defaults to 0.
568 =item C<< Prime => $string >>
570 This option will uncompress the contents of C<$string> before processing the
573 This option can be useful when the compressed data is embedded in another
574 file/data structure and it is not possible to work out where the compressed
575 data begins without having to read the first few bytes. If this is the
576 case, the uncompression can be I<primed> with these bytes using this
579 =item C<< Transparent => 0|1 >>
581 If this option is set and the input file/buffer is not compressed data,
582 the module will allow reading of it anyway.
584 In addition, if the input file/buffer does contain compressed data and
585 there is non-compressed data immediately following it, setting this option
586 will make this module treat the whole file/buffer as a single data stream.
588 This option defaults to 1.
590 =item C<< BlockSize => $num >>
592 When reading the compressed input data, IO::Uncompress::Bunzip2 will read it in
593 blocks of C<$num> bytes.
595 This option defaults to 4096.
597 =item C<< InputLength => $size >>
599 When present this option will limit the number of compressed bytes read
600 from the input file/buffer to C<$size>. This option can be used in the
601 situation where there is useful data directly after the compressed data
602 stream and you know beforehand the exact length of the compressed data
605 This option is mostly used when reading from a filehandle, in which case
606 the file pointer will be left pointing to the first byte directly after the
607 compressed data stream.
609 This option defaults to off.
611 =item C<< Append => 0|1 >>
613 This option controls what the C<read> method does with uncompressed data.
615 If set to 1, all uncompressed data will be appended to the output parameter
616 of the C<read> method.
618 If set to 0, the contents of the output parameter of the C<read> method
619 will be overwritten by the uncompressed data.
623 =item C<< Strict => 0|1 >>
625 This option is a no-op.
627 =item C<< Small => 0|1 >>
629 When non-zero this options will make bzip2 use a decompression algorithm
630 that uses less memory at the expense of increasing the amount of time
631 taken for decompression.
643 $status = $z->read($buffer)
645 Reads a block of compressed data (the size of the compressed block is
646 determined by the C<Buffer> option in the constructor), uncompresses it and
647 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
648 set in the constructor, the uncompressed data will be appended to the
649 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
651 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
652 or a negative number on error.
658 $status = $z->read($buffer, $length)
659 $status = $z->read($buffer, $length, $offset)
661 $status = read($z, $buffer, $length)
662 $status = read($z, $buffer, $length, $offset)
664 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
666 The main difference between this form of the C<read> method and the
667 previous one, is that this one will attempt to return I<exactly> C<$length>
668 bytes. The only circumstances that this function will not is if end-of-file
669 or an IO error is encountered.
671 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
672 or a negative number on error.
678 $line = $z->getline()
683 This method fully supports the use of the variable C<$/> (or
684 C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
685 determine what constitutes an end of line. Paragraph mode, record mode and
686 file slurp mode are all supported.
694 Read a single character.
700 $char = $z->ungetc($string)
706 $hdr = $z->getHeaderInfo();
707 @hdrs = $z->getHeaderInfo();
709 This method returns either a hash reference (in scalar context) or a list
710 or hash references (in array context) that contains information about each
711 of the header fields in the compressed data stream(s).
720 Returns the uncompressed file offset.
729 Returns true if the end of the compressed input stream has been reached.
733 $z->seek($position, $whence);
734 seek($z, $position, $whence);
736 Provides a sub-set of the C<seek> functionality, with the restriction
737 that it is only legal to seek forward in the input file/buffer.
738 It is a fatal error to attempt to seek backward.
740 Note that the implementation of C<seek> in this module does not provide
741 true random access to a compressed file/buffer. It works by uncompressing
742 data from the current offset in the file/buffer until it reaches the
743 uncompressed offset specified in the parameters to C<seek>. For very small
744 files this may be acceptable behaviour. For large files it may cause an
747 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
748 SEEK_CUR or SEEK_END.
750 Returns 1 on success, 0 on failure.
759 This is a noop provided for completeness.
765 Returns true if the object currently refers to a opened file/buffer.
769 my $prev = $z->autoflush()
770 my $prev = $z->autoflush(EXPR)
772 If the C<$z> object is associated with a file or a filehandle, this method
773 returns the current autoflush setting for the underlying filehandle. If
774 C<EXPR> is present, and is non-zero, it will enable flushing after every
775 write/print operation.
777 If C<$z> is associated with a buffer, this method has no effect and always
780 B<Note> that the special variable C<$|> B<cannot> be used to set or
781 retrieve the autoflush setting.
783 =head2 input_line_number
785 $z->input_line_number()
786 $z->input_line_number(EXPR)
788 Returns the current uncompressed line number. If C<EXPR> is present it has
789 the effect of setting the line number. Note that setting the line number
790 does not change the current position within the file/buffer being read.
792 The contents of C<$/> are used to determine what constitutes a line
800 If the C<$z> object is associated with a file or a filehandle, C<fileno>
801 will return the underlying file descriptor. Once the C<close> method is
802 called C<fileno> will return C<undef>.
804 If the C<$z> object is associated with a buffer, this method will return
812 Closes the output file/buffer.
814 For most versions of Perl this method will be automatically invoked if
815 the IO::Uncompress::Bunzip2 object is destroyed (either explicitly or by the
816 variable with the reference to the object going out of scope). The
817 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
818 these cases, the C<close> method will be called automatically, but
819 not until global destruction of all live objects when the program is
822 Therefore, if you want your scripts to be able to run on all versions
823 of Perl, you should call C<close> explicitly and not rely on automatic
826 Returns true on success, otherwise 0.
828 If the C<AutoClose> option has been enabled when the IO::Uncompress::Bunzip2
829 object was created, and the object is associated with a file, the
830 underlying file will also be closed.
836 my $status = $z->nextStream();
838 Skips to the next compressed data stream in the input file/buffer. If a new
839 compressed data stream is found, the eof marker will be cleared and C<$.>
842 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
843 error was encountered.
849 my $data = $z->trailingData();
851 Returns the data, if any, that is present immediately after the compressed
852 data stream once uncompression is complete. It only makes sense to call
853 this method once the end of the compressed data stream has been
856 This option can be used when there is useful information immediately
857 following the compressed data stream, and you don't know the length of the
858 compressed data stream.
860 If the input is a buffer, C<trailingData> will return everything from the
861 end of the compressed data stream to the end of the buffer.
863 If the input is a filehandle, C<trailingData> will return the data that is
864 left in the filehandle input buffer once the end of the compressed data
865 stream has been reached. You can then use the filehandle to read the rest
868 Don't bother using C<trailingData> if the input is a filename.
870 If you know the length of the compressed data stream before you start
871 uncompressing, you can avoid having to use C<trailingData> by setting the
872 C<InputLength> option in the constructor.
876 No symbolic constants are required by IO::Uncompress::Bunzip2 at present.
882 Imports C<bunzip2> and C<$Bunzip2Error>.
885 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
891 =head2 Working with Net::FTP
893 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
897 General feedback/questions/bug reports should be sent to
898 L<https://github.com/pmqs/IO-Compress/issues> (preferred) or
899 L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
903 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::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
905 L<IO::Compress::FAQ|IO::Compress::FAQ>
907 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
908 L<Archive::Tar|Archive::Tar>,
911 The primary site for the bzip2 program is L<https://sourceware.org/bzip2/>.
913 See the module L<Compress::Bzip2|Compress::Bzip2>
917 This module was written by Paul Marquess, C<pmqs@cpan.org>.
919 =head1 MODIFICATION HISTORY
921 See the Changes file.
923 =head1 COPYRIGHT AND LICENSE
925 Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
927 This program is free software; you can redistribute it and/or
928 modify it under the same terms as Perl itself.