This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / cpan / IO-Compress / lib / IO / Uncompress / Bunzip2.pm
1 package IO::Uncompress::Bunzip2 ;
2
3 use strict ;
4 use warnings;
5 use bytes;
6
7 use IO::Compress::Base::Common 2.206 qw(:Status );
8
9 use IO::Uncompress::Base 2.206 ;
10 use IO::Uncompress::Adapter::Bunzip2 2.206 ;
11
12 require Exporter ;
13 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $Bunzip2Error);
14
15 $VERSION = '2.206';
16 $Bunzip2Error = '';
17
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');
23
24
25 sub new
26 {
27     my $class = shift ;
28     my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$Bunzip2Error);
29
30     $obj->_create(undef, 0, @_);
31 }
32
33 sub bunzip2
34 {
35     my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$Bunzip2Error);
36     return $obj->_inf(@_);
37 }
38
39 sub getExtraParams
40 {
41     return (
42             'verbosity'     => [IO::Compress::Base::Common::Parse_boolean,   0],
43             'small'         => [IO::Compress::Base::Common::Parse_boolean,   0],
44         );
45 }
46
47
48 sub ckParams
49 {
50     my $self = shift ;
51     my $got = shift ;
52
53     return 1;
54 }
55
56 sub mkUncomp
57 {
58     my $self = shift ;
59     my $got = shift ;
60
61      my $magic = $self->ckMagic()
62         or return 0;
63
64     *$self->{Info} = $self->readHeader($magic)
65         or return undef ;
66
67     my $Small     = $got->getValue('small');
68     my $Verbosity = $got->getValue('verbosity');
69
70     my ($obj, $errstr, $errno) =  IO::Uncompress::Adapter::Bunzip2::mkUncompObject(
71                                                     $Small, $Verbosity);
72
73     return $self->saveErrorString(undef, $errstr, $errno)
74         if ! defined $obj;
75
76     *$self->{Uncomp} = $obj;
77
78     return 1;
79
80 }
81
82
83 sub ckMagic
84 {
85     my $self = shift;
86
87     my $magic ;
88     $self->smartReadExact(\$magic, 4);
89
90     *$self->{HeaderPending} = $magic ;
91
92     return $self->HeaderError("Header size is " .
93                                         4 . " bytes")
94         if length $magic != 4;
95
96     return $self->HeaderError("Bad Magic.")
97         if ! isBzip2Magic($magic) ;
98
99
100     *$self->{Type} = 'bzip2';
101     return $magic;
102 }
103
104 sub readHeader
105 {
106     my $self = shift;
107     my $magic = shift ;
108
109     $self->pushBack($magic);
110     *$self->{HeaderPending} = '';
111
112
113     return {
114         'Type'              => 'bzip2',
115         'FingerprintLength' => 4,
116         'HeaderLength'      => 4,
117         'TrailerLength'     => 0,
118         'Header'            => '$magic'
119         };
120
121 }
122
123 sub chkTrailer
124 {
125     return STATUS_OK;
126 }
127
128
129
130 sub isBzip2Magic
131 {
132     my $buffer = shift ;
133
134                   # ASCII:  B   Z   h    0    9
135     return $buffer =~ qr/^\x42\x5A\x68[\x30-\x39]$/;
136 }
137
138 1 ;
139
140 __END__
141
142
143 =head1 NAME
144
145 IO::Uncompress::Bunzip2 - Read bzip2 files/buffers
146
147 =head1 SYNOPSIS
148
149     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
150
151     my $status = bunzip2 $input => $output [,OPTS]
152         or die "bunzip2 failed: $Bunzip2Error\n";
153
154     my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
155         or die "bunzip2 failed: $Bunzip2Error\n";
156
157     $status = $z->read($buffer)
158     $status = $z->read($buffer, $length)
159     $status = $z->read($buffer, $length, $offset)
160     $line = $z->getline()
161     $char = $z->getc()
162     $char = $z->ungetc()
163     $char = $z->opened()
164
165     $data = $z->trailingData()
166     $status = $z->nextStream()
167     $data = $z->getHeaderInfo()
168     $z->tell()
169     $z->seek($position, $whence)
170     $z->binmode()
171     $z->fileno()
172     $z->eof()
173     $z->close()
174
175     $Bunzip2Error ;
176
177     # IO::File mode
178
179     <$z>
180     read($z, $buffer);
181     read($z, $buffer, $length);
182     read($z, $buffer, $length, $offset);
183     tell($z)
184     seek($z, $position, $whence)
185     binmode($z)
186     fileno($z)
187     eof($z)
188     close($z)
189
190 =head1 DESCRIPTION
191
192 This module provides a Perl interface that allows the reading of
193 bzip2 files/buffers.
194
195 For writing bzip2 files/buffers, see the companion module IO::Compress::Bzip2.
196
197 =head1 Functional Interface
198
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">
202 section.
203
204     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
205
206     bunzip2 $input_filename_or_reference => $output_filename_or_reference [,OPTS]
207         or die "bunzip2 failed: $Bunzip2Error\n";
208
209 The functional interface needs Perl5.005 or better.
210
211 =head2 bunzip2 $input_filename_or_reference => $output_filename_or_reference [, OPTS]
212
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>)
216
217 =head3 The C<$input_filename_or_reference> parameter
218
219 The parameter, C<$input_filename_or_reference>, is used to define the
220 source of the compressed data.
221
222 It can take one of the following forms:
223
224 =over 5
225
226 =item A filename
227
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.
231
232 =item A filehandle
233
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
236 standard input.
237
238 =item A scalar reference
239
240 If C<$input_filename_or_reference> is a scalar reference, the input data
241 will be read from C<$$input_filename_or_reference>.
242
243 =item An array reference
244
245 If C<$input_filename_or_reference> is an array reference, each element in
246 the array must be a filename.
247
248 The input data will be read from each file in turn.
249
250 The complete array will be walked to ensure that it only
251 contains valid filenames before any data is uncompressed.
252
253 =item An Input FileGlob string
254
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
258 fileglob.
259
260 See L<File::GlobMapper|File::GlobMapper> for more details.
261
262 =back
263
264 If the C<$input_filename_or_reference> parameter is any other type,
265 C<undef> will be returned.
266
267 =head3 The C<$output_filename_or_reference> parameter
268
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
271 these forms.
272
273 =over 5
274
275 =item A filename
276
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.
280
281 =item A filehandle
282
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.
286
287 =item A scalar reference
288
289 If C<$output_filename_or_reference> is a scalar reference, the
290 uncompressed data will be stored in C<$$output_filename_or_reference>.
291
292 =item An Array Reference
293
294 If C<$output_filename_or_reference> is an array reference,
295 the uncompressed data will be pushed onto the array.
296
297 =item An Output FileGlob
298
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
302 fileglob.
303
304 When C<$output_filename_or_reference> is an fileglob string,
305 C<$input_filename_or_reference> must also be a fileglob string. Anything
306 else is an error.
307
308 See L<File::GlobMapper|File::GlobMapper> for more details.
309
310 =back
311
312 If the C<$output_filename_or_reference> parameter is any other type,
313 C<undef> will be returned.
314
315 =head2 Notes
316
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
321 files/buffers.
322
323 =head2 Optional Parameters
324
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
328
329 =over 5
330
331 =item C<< AutoClose => 0|1 >>
332
333 This option applies to any input or output data streams to
334 C<bunzip2> that are filehandles.
335
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
338 completed.
339
340 This parameter defaults to 0.
341
342 =item C<< BinModeOut => 0|1 >>
343
344 This option is now a no-op. All files will be written  in binmode.
345
346 =item C<< Append => 0|1 >>
347
348 The behaviour of this option is dependent on the type of output data
349 stream.
350
351 =over 5
352
353 =item * A Buffer
354
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.
358
359 =item * A Filename
360
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.
364
365 =item * A Filehandle
366
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.
370
371 =back
372
373 When C<Append> is specified, and set to true, it will I<append> all uncompressed
374 data to the output data stream.
375
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.
380
381 Conversely when C<Append> is not specified, or it is present and is set to
382 false, it will operate as follows.
383
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.
388
389 Defaults to 0.
390
391 =item C<< MultiStream => 0|1 >>
392
393 If the input file/buffer contains multiple compressed data streams, this
394 option will uncompress the whole lot as a single data stream.
395
396 Defaults to 0.
397
398 =item C<< TrailingData => $scalar >>
399
400 Returns the data, if any, that is present immediately after the compressed
401 data stream once uncompression is complete.
402
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.
406
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.
409
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
413 of the input file.
414
415 Don't bother using C<trailingData> if the input is a filename.
416
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.
420
421 =back
422
423 =head2 OneShot Examples
424
425 To read the contents of the file C<file1.txt.bz2> and write the
426 uncompressed data to the file C<file1.txt>.
427
428     use strict ;
429     use warnings ;
430     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
431
432     my $input = "file1.txt.bz2";
433     my $output = "file1.txt";
434     bunzip2 $input => $output
435         or die "bunzip2 failed: $Bunzip2Error\n";
436
437 To read from an existing Perl filehandle, C<$input>, and write the
438 uncompressed data to a buffer, C<$buffer>.
439
440     use strict ;
441     use warnings ;
442     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
443     use IO::File ;
444
445     my $input = IO::File->new( "<file1.txt.bz2" )
446         or die "Cannot open 'file1.txt.bz2': $!\n" ;
447     my $buffer ;
448     bunzip2 $input => \$buffer
449         or die "bunzip2 failed: $Bunzip2Error\n";
450
451 To uncompress all files in the directory "/my/home" that match "*.txt.bz2" and store the compressed data in the same directory
452
453     use strict ;
454     use warnings ;
455     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
456
457     bunzip2 '</my/home/*.txt.bz2>' => '</my/home/#1.txt>'
458         or die "bunzip2 failed: $Bunzip2Error\n";
459
460 and if you want to compress each file one at a time, this will do the trick
461
462     use strict ;
463     use warnings ;
464     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
465
466     for my $input ( glob "/my/home/*.txt.bz2" )
467     {
468         my $output = $input;
469         $output =~ s/.bz2// ;
470         bunzip2 $input => $output
471             or die "Error compressing '$input': $Bunzip2Error\n";
472     }
473
474 =head1 OO Interface
475
476 =head2 Constructor
477
478 The format of the constructor for IO::Uncompress::Bunzip2 is shown below
479
480     my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
481         or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
482
483 The constructor takes one mandatory parameter, C<$input>, defined below, and
484 zero or more C<OPTS>, defined in L<Constructor Options>.
485
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.
488
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
494
495     $line = $z->getline();
496     $line = <$z>;
497
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.
500
501     my $filename = "myfile.bz2";
502     my $z = IO::Uncompress::Bunzip2->new($filename)
503         or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
504
505     while (<$z>) {
506         print $_;
507     }
508     $z->close();
509
510 See L</EXAMPLES> for further examples
511
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.
514
515 =over 5
516
517 =item A filename
518
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.
521
522 =item A filehandle
523
524 If the C<$input> parameter is a filehandle, the compressed data will be
525 read from it.
526 The string '-' can be used as an alias for standard input.
527
528 =item A scalar reference
529
530 If C<$input> is a scalar reference, the compressed data will be read from
531 C<$$input>.
532
533 =back
534
535 =head2 Constructor Options
536
537 The option names defined below are case insensitive and can be optionally
538 prefixed by a '-'.  So all of the following are valid
539
540     -AutoClose
541     -autoclose
542     AUTOCLOSE
543     autoclose
544
545 OPTS is a combination of the following options:
546
547 =over 5
548
549 =item C<< AutoClose => 0|1 >>
550
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
554 destroyed.
555
556 This parameter defaults to 0.
557
558 =item C<< MultiStream => 0|1 >>
559
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.
565
566 This parameter defaults to 0.
567
568 =item C<< Prime => $string >>
569
570 This option will uncompress the contents of C<$string> before processing the
571 input file/buffer.
572
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
577 option.
578
579 =item C<< Transparent => 0|1 >>
580
581 If this option is set and the input file/buffer is not compressed data,
582 the module will allow reading of it anyway.
583
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.
587
588 This option defaults to 1.
589
590 =item C<< BlockSize => $num >>
591
592 When reading the compressed input data, IO::Uncompress::Bunzip2 will read it in
593 blocks of C<$num> bytes.
594
595 This option defaults to 4096.
596
597 =item C<< InputLength => $size >>
598
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
603 stream.
604
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.
608
609 This option defaults to off.
610
611 =item C<< Append => 0|1 >>
612
613 This option controls what the C<read> method does with uncompressed data.
614
615 If set to 1, all uncompressed data will be appended to the output parameter
616 of the C<read> method.
617
618 If set to 0, the contents of the output parameter of the C<read> method
619 will be overwritten by the uncompressed data.
620
621 Defaults to 0.
622
623 =item C<< Strict => 0|1 >>
624
625 This option is a no-op.
626
627 =item C<< Small => 0|1 >>
628
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.
632
633 Default is 0.
634
635 =back
636
637 =head1 Methods
638
639 =head2 read
640
641 Usage is
642
643     $status = $z->read($buffer)
644
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.
650
651 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
652 or a negative number on error.
653
654 =head2 read
655
656 Usage is
657
658     $status = $z->read($buffer, $length)
659     $status = $z->read($buffer, $length, $offset)
660
661     $status = read($z, $buffer, $length)
662     $status = read($z, $buffer, $length, $offset)
663
664 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
665
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.
670
671 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
672 or a negative number on error.
673
674 =head2 getline
675
676 Usage is
677
678     $line = $z->getline()
679     $line = <$z>
680
681 Reads a single line.
682
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.
687
688 =head2 getc
689
690 Usage is
691
692     $char = $z->getc()
693
694 Read a single character.
695
696 =head2 ungetc
697
698 Usage is
699
700     $char = $z->ungetc($string)
701
702 =head2 getHeaderInfo
703
704 Usage is
705
706     $hdr  = $z->getHeaderInfo();
707     @hdrs = $z->getHeaderInfo();
708
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).
712
713 =head2 tell
714
715 Usage is
716
717     $z->tell()
718     tell $z
719
720 Returns the uncompressed file offset.
721
722 =head2 eof
723
724 Usage is
725
726     $z->eof();
727     eof($z);
728
729 Returns true if the end of the compressed input stream has been reached.
730
731 =head2 seek
732
733     $z->seek($position, $whence);
734     seek($z, $position, $whence);
735
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.
739
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
745 unacceptable delay.
746
747 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
748 SEEK_CUR or SEEK_END.
749
750 Returns 1 on success, 0 on failure.
751
752 =head2 binmode
753
754 Usage is
755
756     $z->binmode
757     binmode $z ;
758
759 This is a noop provided for completeness.
760
761 =head2 opened
762
763     $z->opened()
764
765 Returns true if the object currently refers to a opened file/buffer.
766
767 =head2 autoflush
768
769     my $prev = $z->autoflush()
770     my $prev = $z->autoflush(EXPR)
771
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.
776
777 If C<$z> is associated with a buffer, this method has no effect and always
778 returns C<undef>.
779
780 B<Note> that the special variable C<$|> B<cannot> be used to set or
781 retrieve the autoflush setting.
782
783 =head2 input_line_number
784
785     $z->input_line_number()
786     $z->input_line_number(EXPR)
787
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.
791
792 The contents of C<$/> are used to determine what constitutes a line
793 terminator.
794
795 =head2 fileno
796
797     $z->fileno()
798     fileno($z)
799
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>.
803
804 If the C<$z> object is associated with a buffer, this method will return
805 C<undef>.
806
807 =head2 close
808
809     $z->close() ;
810     close $z ;
811
812 Closes the output file/buffer.
813
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
820 terminating.
821
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
824 closing.
825
826 Returns true on success, otherwise 0.
827
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.
831
832 =head2 nextStream
833
834 Usage is
835
836     my $status = $z->nextStream();
837
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<$.>
840 will be reset to 0.
841
842 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
843 error was encountered.
844
845 =head2 trailingData
846
847 Usage is
848
849     my $data = $z->trailingData();
850
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
854 encountered.
855
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.
859
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.
862
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
866 of the input file.
867
868 Don't bother using C<trailingData> if the input is a filename.
869
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.
873
874 =head1 Importing
875
876 No symbolic constants are required by IO::Uncompress::Bunzip2 at present.
877
878 =over 5
879
880 =item :all
881
882 Imports C<bunzip2> and C<$Bunzip2Error>.
883 Same as doing this
884
885     use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
886
887 =back
888
889 =head1 EXAMPLES
890
891 =head2 Working with Net::FTP
892
893 See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
894
895 =head1 SUPPORT
896
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>.
900
901 =head1 SEE ALSO
902
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>
904
905 L<IO::Compress::FAQ|IO::Compress::FAQ>
906
907 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
908 L<Archive::Tar|Archive::Tar>,
909 L<IO::Zlib|IO::Zlib>
910
911 The primary site for the bzip2 program is L<https://sourceware.org/bzip2/>.
912
913 See the module L<Compress::Bzip2|Compress::Bzip2>
914
915 =head1 AUTHOR
916
917 This module was written by Paul Marquess, C<pmqs@cpan.org>.
918
919 =head1 MODIFICATION HISTORY
920
921 See the Changes file.
922
923 =head1 COPYRIGHT AND LICENSE
924
925 Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
926
927 This program is free software; you can redistribute it and/or
928 modify it under the same terms as Perl itself.