File Coverage

blib/lib/IO/Compress/Bzip2.pm
Criterion Covered Total %
statement 53 54 98.1
branch 9 10 90.0
condition 7 9 77.7
subroutine 16 17 94.1
pod 2 10 20.0
total 87 100 87.0


line stmt bran cond sub pod time code
1             package IO::Compress::Bzip2 ;
2              
3 59     59   103709 use strict ;
  59         134  
  59         2384  
4 59     59   309 use warnings;
  59         115  
  59         2989  
5 59     59   5345 use bytes;
  59         4907  
  59         364  
6             require Exporter ;
7              
8 59     59   12804 use IO::Compress::Base 2.219 ;
  59         1348  
  59         3872  
9              
10 59     59   381 use IO::Compress::Base::Common 2.219 qw();
  59         856  
  59         1812  
11 59     59   8559 use IO::Compress::Adapter::Bzip2 2.219 ;
  59         2007  
  59         20190  
12              
13              
14              
15             our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $Bzip2Error);
16              
17             $VERSION = '2.219';
18             $Bzip2Error = '';
19              
20             @ISA = qw(IO::Compress::Base Exporter);
21             @EXPORT_OK = qw( $Bzip2Error bzip2 ) ;
22             %EXPORT_TAGS = %IO::Compress::Base::EXPORT_TAGS ;
23             $EXPORT_TAGS{all} = [ defined $EXPORT_TAGS{all} ? @{ $EXPORT_TAGS{all} } : (), @EXPORT_OK ] ;
24             Exporter::export_ok_tags('all');
25              
26              
27              
28             sub new
29             {
30 401     401 1 762069 my $class = shift ;
31              
32 401         2014 my $obj = IO::Compress::Base::Common::createSelfTiedObject($class, \$Bzip2Error);
33 401         1855 return $obj->_create(undef, @_);
34             }
35              
36             sub bzip2
37             {
38 139     139 1 414828 my $obj = IO::Compress::Base::Common::createSelfTiedObject(undef, \$Bzip2Error);
39 139         570 $obj->_def(@_);
40             }
41              
42              
43             sub mkHeader
44             {
45 526     526 0 1015 my $self = shift ;
46 526         2486 return '';
47              
48             }
49              
50             sub getExtraParams
51             {
52 586     586 0 1100 my $self = shift ;
53              
54 59     59   456 use IO::Compress::Base::Common 2.219 qw(:Parse);
  59         1238  
  59         36305  
55              
56             return (
57 586         7405 'blocksize100k' => [IO::Compress::Base::Common::Parse_unsigned, 1],
58             'workfactor' => [IO::Compress::Base::Common::Parse_unsigned, 0],
59             'verbosity' => [IO::Compress::Base::Common::Parse_boolean, 0],
60             );
61             }
62              
63              
64              
65             sub ckParams
66             {
67 947     947 0 1923 my $self = shift ;
68 947         1790 my $got = shift;
69              
70             # check that BlockSize100K is a number between 1 & 9
71 947 100       2694 if ($got->parsed('blocksize100k')) {
72 12         28 my $value = $got->getValue('blocksize100k');
73 12 100 66     88 return $self->saveErrorString(undef, "Parameter 'BlockSize100K' not between 1 and 9, got $value")
      100        
74             unless defined $value && $value >= 1 && $value <= 9;
75              
76             }
77              
78             # check that WorkFactor between 0 & 250
79 944 100       2590 if ($got->parsed('workfactor')) {
80 253         550 my $value = $got->getValue('workfactor');
81 253 100 66     1213 return $self->saveErrorString(undef, "Parameter 'WorkFactor' not between 0 and 250, got $value")
82             unless $value >= 0 && $value <= 250;
83             }
84              
85 942         3506 return 1 ;
86             }
87              
88              
89             sub mkComp
90             {
91 527     527 0 944 my $self = shift ;
92 527         797 my $got = shift ;
93              
94 527         1303 my $BlockSize100K = $got->getValue('blocksize100k');
95 527         1188 my $WorkFactor = $got->getValue('workfactor');
96 527         1475 my $Verbosity = $got->getValue('verbosity');
97              
98 527         2194 my ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject(
99             $BlockSize100K, $WorkFactor,
100             $Verbosity);
101              
102 527 50       1464 return $self->saveErrorString(undef, $errstr, $errno)
103             if ! defined $obj;
104              
105 527         3465 return $obj;
106             }
107              
108              
109             sub mkTrailer
110             {
111 526     526 0 900 my $self = shift ;
112 526         1581 return '';
113             }
114              
115             sub mkFinalTrailer
116             {
117 499     499 0 1329 return '';
118             }
119              
120             #sub newHeader
121             #{
122             # my $self = shift ;
123             # return '';
124             #}
125              
126             sub getInverseClass
127             {
128 0     0 0 0 return ('IO::Uncompress::Bunzip2');
129             }
130              
131             sub getFileInfo
132             {
133 90     90 0 156 my $self = shift ;
134 90         157 my $params = shift;
135 90         221 my $file = shift ;
136              
137             }
138              
139             1;
140              
141             __END__
142              
143             =head1 NAME
144              
145             IO::Compress::Bzip2 - Write bzip2 files/buffers
146              
147             =head1 SYNOPSIS
148              
149             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
150              
151             my $status = bzip2 $input => $output [,OPTS]
152             or die "bzip2 failed: $Bzip2Error\n";
153              
154             my $z = IO::Compress::Bzip2->new( $output [,OPTS] )
155             or die "bzip2 failed: $Bzip2Error\n";
156              
157             $z->print($string);
158             $z->printf($format, $string);
159             $z->write($string);
160             $z->syswrite($string [, $length, $offset]);
161             $z->flush();
162             $z->tell();
163             $z->eof();
164             $z->seek($position, $whence);
165             $z->binmode();
166             $z->fileno();
167             $z->opened();
168             $z->autoflush();
169             $z->input_line_number();
170             $z->newStream( [OPTS] );
171              
172             $z->close() ;
173              
174             $Bzip2Error ;
175              
176             # IO::File mode
177              
178             print $z $string;
179             printf $z $format, $string;
180             tell $z
181             eof $z
182             seek $z, $position, $whence
183             binmode $z
184             fileno $z
185             close $z ;
186              
187             =head1 DESCRIPTION
188              
189             This module provides a Perl interface that allows writing bzip2
190             compressed data to files or buffer.
191              
192             For reading bzip2 files/buffers, see the companion module
193             L<IO::Uncompress::Bunzip2|IO::Uncompress::Bunzip2>.
194              
195             =head1 Functional Interface
196              
197             A top-level function, C<bzip2>, is provided to carry out
198             "one-shot" compression between buffers and/or files. For finer
199             control over the compression process, see the L</"OO Interface">
200             section.
201              
202             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
203              
204             bzip2 $input_filename_or_reference => $output_filename_or_reference [,OPTS]
205             or die "bzip2 failed: $Bzip2Error\n";
206              
207             The functional interface needs Perl5.005 or better.
208              
209             =head2 bzip2 $input_filename_or_reference => $output_filename_or_reference [, OPTS]
210              
211             C<bzip2> expects at least two parameters,
212             C<$input_filename_or_reference> and C<$output_filename_or_reference>
213             and zero or more optional parameters (see L</Optional Parameters>)
214              
215             =head3 The C<$input_filename_or_reference> parameter
216              
217             The parameter, C<$input_filename_or_reference>, is used to define the
218             source of the uncompressed data.
219              
220             It can take one of the following forms:
221              
222             =over 5
223              
224             =item A filename
225              
226             If the C<$input_filename_or_reference> parameter is a simple scalar, it is
227             assumed to be a filename. This file will be opened for reading and the
228             input data will be read from it.
229              
230             =item A filehandle
231              
232             If the C<$input_filename_or_reference> parameter is a filehandle, the input
233             data will be read from it. The string '-' can be used as an alias for
234             standard input.
235              
236             =item A scalar reference
237              
238             If C<$input_filename_or_reference> is a scalar reference, the input data
239             will be read from C<$$input_filename_or_reference>.
240              
241             =item An array reference
242              
243             If C<$input_filename_or_reference> is an array reference, each element in
244             the array must be a filename.
245              
246             The input data will be read from each file in turn.
247              
248             The complete array will be walked to ensure that it only
249             contains valid filenames before any data is compressed.
250              
251             =item An Input FileGlob string
252              
253             If C<$input_filename_or_reference> is a string that is delimited by the
254             characters "<" and ">" C<bzip2> will assume that it is an
255             I<input fileglob string>. The input is the list of files that match the
256             fileglob.
257              
258             See L<File::GlobMapper|File::GlobMapper> for more details.
259              
260             =back
261              
262             If the C<$input_filename_or_reference> parameter is any other type,
263             C<undef> will be returned.
264              
265             =head3 The C<$output_filename_or_reference> parameter
266              
267             The parameter C<$output_filename_or_reference> is used to control the
268             destination of the compressed data. This parameter can take one of
269             these forms.
270              
271             =over 5
272              
273             =item A filename
274              
275             If the C<$output_filename_or_reference> parameter is a simple scalar, it is
276             assumed to be a filename. This file will be opened for writing and the
277             compressed data will be written to it.
278              
279             =item A filehandle
280              
281             If the C<$output_filename_or_reference> parameter is a filehandle, the
282             compressed data will be written to it. The string '-' can be used as
283             an alias for standard output.
284              
285             =item A scalar reference
286              
287             If C<$output_filename_or_reference> is a scalar reference, the
288             compressed data will be stored in C<$$output_filename_or_reference>.
289              
290             =item An Array Reference
291              
292             If C<$output_filename_or_reference> is an array reference,
293             the compressed data will be pushed onto the array.
294              
295             =item An Output FileGlob
296              
297             If C<$output_filename_or_reference> is a string that is delimited by the
298             characters "<" and ">" C<bzip2> will assume that it is an
299             I<output fileglob string>. The output is the list of files that match the
300             fileglob.
301              
302             When C<$output_filename_or_reference> is an fileglob string,
303             C<$input_filename_or_reference> must also be a fileglob string. Anything
304             else is an error.
305              
306             See L<File::GlobMapper|File::GlobMapper> for more details.
307              
308             =back
309              
310             If the C<$output_filename_or_reference> parameter is any other type,
311             C<undef> will be returned.
312              
313             =head2 Notes
314              
315             When C<$input_filename_or_reference> maps to multiple files/buffers and
316             C<$output_filename_or_reference> is a single
317             file/buffer the input files/buffers will be stored
318             in C<$output_filename_or_reference> as a concatenated series of compressed data streams.
319              
320             =head2 Optional Parameters
321              
322             The optional parameters for the one-shot function C<bzip2>
323             are (for the most part) identical to those used with the OO interface defined in the
324             L</"Constructor Options"> section. The exceptions are listed below
325              
326             =over 5
327              
328             =item C<< AutoClose => 0|1 >>
329              
330             This option applies to any input or output data streams to
331             C<bzip2> that are filehandles.
332              
333             If C<AutoClose> is specified, and the value is true, it will result in all
334             input and/or output filehandles being closed once C<bzip2> has
335             completed.
336              
337             This parameter defaults to 0.
338              
339             =item C<< BinModeIn => 0|1 >>
340              
341             This option is now a no-op. All files will be read in binmode.
342              
343             =item C<< Append => 0|1 >>
344              
345             The behaviour of this option is dependent on the type of output data
346             stream.
347              
348             =over 5
349              
350             =item * A Buffer
351              
352             If C<Append> is enabled, all compressed data will be append to the end of
353             the output buffer. Otherwise the output buffer will be cleared before any
354             compressed data is written to it.
355              
356             =item * A Filename
357              
358             If C<Append> is enabled, the file will be opened in append mode. Otherwise
359             the contents of the file, if any, will be truncated before any compressed
360             data is written to it.
361              
362             =item * A Filehandle
363              
364             If C<Append> is enabled, the filehandle will be positioned to the end of
365             the file via a call to C<seek> before any compressed data is
366             written to it. Otherwise the file pointer will not be moved.
367              
368             =back
369              
370             When C<Append> is specified, and set to true, it will I<append> all compressed
371             data to the output data stream.
372              
373             So when the output is a filehandle it will carry out a seek to the eof
374             before writing any compressed data. If the output is a filename, it will be opened for
375             appending. If the output is a buffer, all compressed data will be
376             appended to the existing buffer.
377              
378             Conversely when C<Append> is not specified, or it is present and is set to
379             false, it will operate as follows.
380              
381             When the output is a filename, it will truncate the contents of the file
382             before writing any compressed data. If the output is a filehandle
383             its position will not be changed. If the output is a buffer, it will be
384             wiped before any compressed data is output.
385              
386             Defaults to 0.
387              
388             =back
389              
390             =head2 Oneshot Examples
391              
392             Here are a few example that show the capabilities of the module.
393              
394             =head3 Streaming
395              
396             This very simple command line example demonstrates the streaming capabilities of the module.
397             The code reads data from STDIN, compresses it, and writes the compressed data to STDOUT.
398              
399             $ echo hello world | perl -MIO::Compress::Bzip2=bzip2 -e 'bzip2 \*STDIN => \*STDOUT' >output.bz2
400              
401             The special filename "-" can be used as a standin for both C<\*STDIN> and C<\*STDOUT>,
402             so the above can be rewritten as
403              
404             $ echo hello world | perl -MIO::Compress::Bzip2=bzip2 -e 'bzip2 "-" => "-"' >output.bz2
405              
406             =head3 Compressing a file from the filesystem
407              
408             To read the contents of the file C<file1.txt> and write the compressed
409             data to the file C<file1.txt.bz2>.
410              
411             use strict ;
412             use warnings ;
413             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
414              
415             my $input = "file1.txt";
416             bzip2 $input => "$input.bz2"
417             or die "bzip2 failed: $Bzip2Error\n";
418              
419             =head3 Reading from a Filehandle and writing to an in-memory buffer
420              
421             To read from an existing Perl filehandle, C<$input>, and write the
422             compressed data to a buffer, C<$buffer>.
423              
424             use strict ;
425             use warnings ;
426             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
427             use IO::File ;
428              
429             my $input = IO::File->new( "<file1.txt" )
430             or die "Cannot open 'file1.txt': $!\n" ;
431             my $buffer ;
432             bzip2 $input => \$buffer
433             or die "bzip2 failed: $Bzip2Error\n";
434              
435             =head3 Compressing multiple files
436              
437             To compress all files in the directory "/my/home" that match "*.txt"
438             and store the compressed data in the same directory
439              
440             use strict ;
441             use warnings ;
442             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
443              
444             bzip2 '</my/home/*.txt>' => '<*.bz2>'
445             or die "bzip2 failed: $Bzip2Error\n";
446              
447             and if you want to compress each file one at a time, this will do the trick
448              
449             use strict ;
450             use warnings ;
451             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
452              
453             for my $input ( glob "/my/home/*.txt" )
454             {
455             my $output = "$input.bz2" ;
456             bzip2 $input => $output
457             or die "Error compressing '$input': $Bzip2Error\n";
458             }
459              
460             =head1 OO Interface
461              
462             =head2 Constructor
463              
464             The format of the constructor for C<IO::Compress::Bzip2> is shown below
465              
466             my $z = IO::Compress::Bzip2->new( $output [,OPTS] )
467             or die "IO::Compress::Bzip2 failed: $Bzip2Error\n";
468              
469             The constructor takes one mandatory parameter, C<$output>, defined below and
470             zero or more C<OPTS>, defined in L<Constructor Options>.
471              
472             It returns an C<IO::Compress::Bzip2> object on success and C<undef> on failure.
473             The variable C<$Bzip2Error> will contain an error message on failure.
474              
475             If you are running Perl 5.005 or better the object, C<$z>, returned from
476             IO::Compress::Bzip2 can be used exactly like an L<IO::File|IO::File> filehandle.
477             This means that all normal output file operations can be carried out
478             with C<$z>.
479             For example, to write to a compressed file/buffer you can use either of
480             these forms
481              
482             $z->print("hello world\n");
483             print $z "hello world\n";
484              
485             Below is a simple exaple of using the OO interface to create an output file
486             C<myfile.bz2> and write some data to it.
487              
488             my $filename = "myfile.bz2";
489             my $z = IO::Compress::Bzip2->new($filename)
490             or die "IO::Compress::Bzip2 failed: $Bzip2Error\n";
491              
492             $z->print("abcde");
493             $z->close();
494              
495             See the L</Examples> for more.
496              
497             The mandatory parameter C<$output> is used to control the destination
498             of the compressed data. This parameter can take one of these forms.
499              
500             =over 5
501              
502             =item A filename
503              
504             If the C<$output> parameter is a simple scalar, it is assumed to be a
505             filename. This file will be opened for writing and the compressed data
506             will be written to it.
507              
508             =item A filehandle
509              
510             If the C<$output> parameter is a filehandle, the compressed data will be
511             written to it.
512             The string '-' can be used as an alias for standard output.
513              
514             =item A scalar reference
515              
516             If C<$output> is a scalar reference, the compressed data will be stored
517             in C<$$output>.
518              
519             =back
520              
521             If the C<$output> parameter is any other type, C<IO::Compress::Bzip2>::new will
522             return undef.
523              
524             =head2 Constructor Options
525              
526             C<OPTS> is any combination of zero or more the following options:
527              
528             =over 5
529              
530             =item C<< AutoClose => 0|1 >>
531              
532             This option is only valid when the C<$output> parameter is a filehandle. If
533             specified, and the value is true, it will result in the C<$output> being
534             closed once either the C<close> method is called or the C<IO::Compress::Bzip2>
535             object is destroyed.
536              
537             This parameter defaults to 0.
538              
539             =item C<< Append => 0|1 >>
540              
541             Opens C<$output> in append mode.
542              
543             The behaviour of this option is dependent on the type of C<$output>.
544              
545             =over 5
546              
547             =item * A Buffer
548              
549             If C<$output> is a buffer and C<Append> is enabled, all compressed data
550             will be append to the end of C<$output>. Otherwise C<$output> will be
551             cleared before any data is written to it.
552              
553             =item * A Filename
554              
555             If C<$output> is a filename and C<Append> is enabled, the file will be
556             opened in append mode. Otherwise the contents of the file, if any, will be
557             truncated before any compressed data is written to it.
558              
559             =item * A Filehandle
560              
561             If C<$output> is a filehandle, the file pointer will be positioned to the
562             end of the file via a call to C<seek> before any compressed data is written
563             to it. Otherwise the file pointer will not be moved.
564              
565             =back
566              
567             This parameter defaults to 0.
568              
569             =item C<< BlockSize100K => number >>
570              
571             Specify the number of 100K blocks bzip2 uses during compression.
572              
573             Valid values are from 1 to 9, where 9 is best compression.
574              
575             The default is 1.
576              
577             =item C<< WorkFactor => number >>
578              
579             Specifies how much effort bzip2 should take before resorting to a slower
580             fallback compression algorithm.
581              
582             Valid values range from 0 to 250, where 0 means use the default value 30.
583              
584             The default is 0.
585              
586             =item C<< Strict => 0|1 >>
587              
588             This is a placeholder option.
589              
590             =back
591              
592             =head2 Examples
593              
594             =head3 Streaming
595              
596             This very simple command line example demonstrates the streaming capabilities
597             of the module. The code reads data from STDIN or all the files given on the
598             commandline, compresses it, and writes the compressed data to STDOUT.
599              
600             use strict ;
601             use warnings ;
602             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
603              
604             my $z = IO::Compress::Bzip2->new("-", Stream => 1)
605             or die "IO::Compress::Bzip2 failed: $Bzip2Error\n";
606              
607             while (<>) {
608             $z->print("abcde");
609             }
610             $z->close();
611              
612             Note the use of C<"-"> to means C<STDOUT>. Alternatively you can use C<\*STDOUT>.
613              
614             =head3 Compressing a file from the filesystem
615              
616             To read the contents of the file C<file1.txt> and write the compressed
617             data to the file C<file1.txt.bz2> there are a few options
618              
619             Start by creating the compression object and opening the input file
620              
621             use strict ;
622             use warnings ;
623             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
624              
625             my $input = "file1.txt";
626             my $z = IO::Compress::Bzip2->new("file1.txt.bz2")
627             or die "IO::Compress::Bzip2 failed: $Bzip2Error\n";
628              
629             # open the input file
630             open my $fh, "<", "file1.txt"
631             or die "Cannot open file1.txt: $!\n";
632              
633             # loop through the input file & write to the compressed file
634             while (<$fh>) {
635             $z->print($_);
636             }
637              
638             # not forgetting to close the compressed file
639             $z->close();
640              
641             =head1 Methods
642              
643             =head2 print
644              
645             Usage is
646              
647             $z->print($data)
648             print $z $data
649              
650             Compresses and outputs the contents of the C<$data> parameter. This
651             has the same behaviour as the C<print> built-in.
652              
653             Returns true if successful.
654              
655             =head2 printf
656              
657             Usage is
658              
659             $z->printf($format, $data)
660             printf $z $format, $data
661              
662             Compresses and outputs the contents of the C<$data> parameter.
663              
664             Returns true if successful.
665              
666             =head2 syswrite
667              
668             Usage is
669              
670             $z->syswrite $data
671             $z->syswrite $data, $length
672             $z->syswrite $data, $length, $offset
673              
674             Compresses and outputs the contents of the C<$data> parameter.
675              
676             Returns the number of uncompressed bytes written, or C<undef> if
677             unsuccessful.
678              
679             =head2 write
680              
681             Usage is
682              
683             $z->write $data
684             $z->write $data, $length
685             $z->write $data, $length, $offset
686              
687             Compresses and outputs the contents of the C<$data> parameter.
688              
689             Returns the number of uncompressed bytes written, or C<undef> if
690             unsuccessful.
691              
692             =head2 flush
693              
694             Usage is
695              
696             $z->flush;
697              
698             Flushes any pending compressed data to the output file/buffer.
699              
700             TODO
701              
702             Returns true on success.
703              
704             =head2 tell
705              
706             Usage is
707              
708             $z->tell()
709             tell $z
710              
711             Returns the uncompressed file offset.
712              
713             =head2 eof
714              
715             Usage is
716              
717             $z->eof();
718             eof($z);
719              
720             Returns true if the C<close> method has been called.
721              
722             =head2 seek
723              
724             $z->seek($position, $whence);
725             seek($z, $position, $whence);
726              
727             Provides a sub-set of the C<seek> functionality, with the restriction
728             that it is only legal to seek forward in the output file/buffer.
729             It is a fatal error to attempt to seek backward.
730              
731             Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
732              
733             The C<$whence> parameter takes one the usual values, namely SEEK_SET,
734             SEEK_CUR or SEEK_END.
735              
736             Returns 1 on success, 0 on failure.
737              
738             =head2 binmode
739              
740             Usage is
741              
742             $z->binmode
743             binmode $z ;
744              
745             This is a noop provided for completeness.
746              
747             =head2 opened
748              
749             $z->opened()
750              
751             Returns true if the object currently refers to a opened file/buffer.
752              
753             =head2 autoflush
754              
755             my $prev = $z->autoflush()
756             my $prev = $z->autoflush(EXPR)
757              
758             If the C<$z> object is associated with a file or a filehandle, this method
759             returns the current autoflush setting for the underlying filehandle. If
760             C<EXPR> is present, and is non-zero, it will enable flushing after every
761             write/print operation.
762              
763             If C<$z> is associated with a buffer, this method has no effect and always
764             returns C<undef>.
765              
766             B<Note> that the special variable C<$|> B<cannot> be used to set or
767             retrieve the autoflush setting.
768              
769             =head2 input_line_number
770              
771             $z->input_line_number()
772             $z->input_line_number(EXPR)
773              
774             This method always returns C<undef> when compressing.
775              
776             =head2 fileno
777              
778             $z->fileno()
779             fileno($z)
780              
781             If the C<$z> object is associated with a file or a filehandle, C<fileno>
782             will return the underlying file descriptor. Once the C<close> method is
783             called C<fileno> will return C<undef>.
784              
785             If the C<$z> object is associated with a buffer, this method will return
786             C<undef>.
787              
788             =head2 close
789              
790             $z->close() ;
791             close $z ;
792              
793             Flushes any pending compressed data and then closes the output file/buffer.
794              
795             For most versions of Perl this method will be automatically invoked if
796             the IO::Compress::Bzip2 object is destroyed (either explicitly or by the
797             variable with the reference to the object going out of scope). The
798             exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
799             these cases, the C<close> method will be called automatically, but
800             not until global destruction of all live objects when the program is
801             terminating.
802              
803             Therefore, if you want your scripts to be able to run on all versions
804             of Perl, you should call C<close> explicitly and not rely on automatic
805             closing.
806              
807             Returns true on success, otherwise 0.
808              
809             If the C<AutoClose> option has been enabled when the IO::Compress::Bzip2
810             object was created, and the object is associated with a file, the
811             underlying file will also be closed.
812              
813             =head2 newStream([OPTS])
814              
815             Usage is
816              
817             $z->newStream( [OPTS] )
818              
819             Closes the current compressed data stream and starts a new one.
820              
821             OPTS consists of any of the options that are available when creating
822             the C<$z> object.
823              
824             See the L</"Constructor Options"> section for more details.
825              
826             =head1 Importing
827              
828             No symbolic constants are required by IO::Compress::Bzip2 at present.
829              
830             =over 5
831              
832             =item :all
833              
834             Imports C<bzip2> and C<$Bzip2Error>.
835             Same as doing this
836              
837             use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
838              
839             =back
840              
841             =head1 EXAMPLES
842              
843             =head2 Apache::GZip Revisited
844              
845             See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited">
846              
847             =head2 Working with Net::FTP
848              
849             See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP">
850              
851             =head1 SUPPORT
852              
853             General feedback/questions/bug reports should be sent to
854             L<https://github.com/pmqs/IO-Compress/issues> (preferred) or
855             L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
856              
857             =head1 SEE ALSO
858              
859             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::Uncompress::Bunzip2>, 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>
860              
861             L<IO::Compress::FAQ|IO::Compress::FAQ>
862              
863             L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
864             L<Archive::Tar|Archive::Tar>,
865             L<IO::Zlib|IO::Zlib>
866              
867             The primary site for the bzip2 program is L<https://sourceware.org/bzip2/>.
868              
869             See the module L<Compress::Bzip2|Compress::Bzip2>
870              
871             =head1 AUTHOR
872              
873             This module was written by Paul Marquess, C<pmqs@cpan.org>.
874              
875             =head1 MODIFICATION HISTORY
876              
877             See the Changes file.
878              
879             =head1 COPYRIGHT AND LICENSE
880              
881             Copyright (c) 2005-2026 Paul Marquess. All rights reserved.
882              
883             This program is free software; you can redistribute it and/or
884             modify it under the same terms as Perl itself.