1IO::Uncompress::Bunzip2U(s3e)r Contributed Perl DocumentaItOi:o:nUncompress::Bunzip2(3)
2
3
4

NAME

6       IO::Uncompress::Bunzip2 - Read bzip2 files/buffers
7

SYNOPSIS

9           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
10
11           my $status = bunzip2 $input => $output [,OPTS]
12               or die "bunzip2 failed: $Bunzip2Error\n";
13
14           my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
15               or die "bunzip2 failed: $Bunzip2Error\n";
16
17           $status = $z->read($buffer)
18           $status = $z->read($buffer, $length)
19           $status = $z->read($buffer, $length, $offset)
20           $line = $z->getline()
21           $char = $z->getc()
22           $char = $z->ungetc()
23           $char = $z->opened()
24
25           $data = $z->trailingData()
26           $status = $z->nextStream()
27           $data = $z->getHeaderInfo()
28           $z->tell()
29           $z->seek($position, $whence)
30           $z->binmode()
31           $z->fileno()
32           $z->eof()
33           $z->close()
34
35           $Bunzip2Error ;
36
37           # IO::File mode
38
39           <$z>
40           read($z, $buffer);
41           read($z, $buffer, $length);
42           read($z, $buffer, $length, $offset);
43           tell($z)
44           seek($z, $position, $whence)
45           binmode($z)
46           fileno($z)
47           eof($z)
48           close($z)
49

DESCRIPTION

51       This module provides a Perl interface that allows the reading of bzip2
52       files/buffers.
53
54       For writing bzip2 files/buffers, see the companion module
55       IO::Compress::Bzip2.
56

Functional Interface

58       A top-level function, "bunzip2", is provided to carry out "one-shot"
59       uncompression between buffers and/or files. For finer control over the
60       uncompression process, see the "OO Interface" section.
61
62           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
63
64           bunzip2 $input_filename_or_reference => $output_filename_or_reference [,OPTS]
65               or die "bunzip2 failed: $Bunzip2Error\n";
66
67       The functional interface needs Perl5.005 or better.
68
69   bunzip2 $input_filename_or_reference => $output_filename_or_reference [,
70       OPTS]
71       "bunzip2" expects at least two parameters, $input_filename_or_reference
72       and $output_filename_or_reference and zero or more optional parameters
73       (see "Optional Parameters")
74
75       The $input_filename_or_reference parameter
76
77       The parameter, $input_filename_or_reference, is used to define the
78       source of the compressed data.
79
80       It can take one of the following forms:
81
82       A filename
83            If the $input_filename_or_reference parameter is a simple scalar,
84            it is assumed to be a filename. This file will be opened for
85            reading and the input data will be read from it.
86
87       A filehandle
88            If the $input_filename_or_reference parameter is a filehandle, the
89            input data will be read from it.  The string '-' can be used as an
90            alias for standard input.
91
92       A scalar reference
93            If $input_filename_or_reference is a scalar reference, the input
94            data will be read from $$input_filename_or_reference.
95
96       An array reference
97            If $input_filename_or_reference is an array reference, each
98            element in the array must be a filename.
99
100            The input data will be read from each file in turn.
101
102            The complete array will be walked to ensure that it only contains
103            valid filenames before any data is uncompressed.
104
105       An Input FileGlob string
106            If $input_filename_or_reference is a string that is delimited by
107            the characters "<" and ">" "bunzip2" will assume that it is an
108            input fileglob string. The input is the list of files that match
109            the fileglob.
110
111            See File::GlobMapper for more details.
112
113       If the $input_filename_or_reference parameter is any other type,
114       "undef" will be returned.
115
116       The $output_filename_or_reference parameter
117
118       The parameter $output_filename_or_reference is used to control the
119       destination of the uncompressed data. This parameter can take one of
120       these forms.
121
122       A filename
123            If the $output_filename_or_reference parameter is a simple scalar,
124            it is assumed to be a filename.  This file will be opened for
125            writing and the uncompressed data will be written to it.
126
127       A filehandle
128            If the $output_filename_or_reference parameter is a filehandle,
129            the uncompressed data will be written to it.  The string '-' can
130            be used as an alias for standard output.
131
132       A scalar reference
133            If $output_filename_or_reference is a scalar reference, the
134            uncompressed data will be stored in
135            $$output_filename_or_reference.
136
137       An Array Reference
138            If $output_filename_or_reference is an array reference, the
139            uncompressed data will be pushed onto the array.
140
141       An Output FileGlob
142            If $output_filename_or_reference is a string that is delimited by
143            the characters "<" and ">" "bunzip2" will assume that it is an
144            output fileglob string. The output is the list of files that match
145            the fileglob.
146
147            When $output_filename_or_reference is an fileglob string,
148            $input_filename_or_reference must also be a fileglob string.
149            Anything else is an error.
150
151            See File::GlobMapper for more details.
152
153       If the $output_filename_or_reference parameter is any other type,
154       "undef" will be returned.
155
156   Notes
157       When $input_filename_or_reference maps to multiple compressed
158       files/buffers and $output_filename_or_reference is a single
159       file/buffer, after uncompression $output_filename_or_reference will
160       contain a concatenation of all the uncompressed data from each of the
161       input files/buffers.
162
163   Optional Parameters
164       The optional parameters for the one-shot function "bunzip2" are (for
165       the most part) identical to those used with the OO interface defined in
166       the "Constructor Options" section. The exceptions are listed below
167
168       "AutoClose => 0|1"
169            This option applies to any input or output data streams to
170            "bunzip2" that are filehandles.
171
172            If "AutoClose" is specified, and the value is true, it will result
173            in all input and/or output filehandles being closed once "bunzip2"
174            has completed.
175
176            This parameter defaults to 0.
177
178       "BinModeOut => 0|1"
179            This option is now a no-op. All files will be written  in binmode.
180
181       "Append => 0|1"
182            The behaviour of this option is dependent on the type of output
183            data stream.
184
185            •    A Buffer
186
187                 If "Append" is enabled, all uncompressed data will be append
188                 to the end of the output buffer. Otherwise the output buffer
189                 will be cleared before any uncompressed data is written to
190                 it.
191
192            •    A Filename
193
194                 If "Append" is enabled, the file will be opened in append
195                 mode. Otherwise the contents of the file, if any, will be
196                 truncated before any uncompressed data is written to it.
197
198            •    A Filehandle
199
200                 If "Append" is enabled, the filehandle will be positioned to
201                 the end of the file via a call to "seek" before any
202                 uncompressed data is written to it.  Otherwise the file
203                 pointer will not be moved.
204
205            When "Append" is specified, and set to true, it will append all
206            uncompressed data to the output data stream.
207
208            So when the output is a filehandle it will carry out a seek to the
209            eof before writing any uncompressed data. If the output is a
210            filename, it will be opened for appending. If the output is a
211            buffer, all uncompressed data will be appended to the existing
212            buffer.
213
214            Conversely when "Append" is not specified, or it is present and is
215            set to false, it will operate as follows.
216
217            When the output is a filename, it will truncate the contents of
218            the file before writing any uncompressed data. If the output is a
219            filehandle its position will not be changed. If the output is a
220            buffer, it will be wiped before any uncompressed data is output.
221
222            Defaults to 0.
223
224       "MultiStream => 0|1"
225            If the input file/buffer contains multiple compressed data
226            streams, this option will uncompress the whole lot as a single
227            data stream.
228
229            Defaults to 0.
230
231       "TrailingData => $scalar"
232            Returns the data, if any, that is present immediately after the
233            compressed data stream once uncompression is complete.
234
235            This option can be used when there is useful information
236            immediately following the compressed data stream, and you don't
237            know the length of the compressed data stream.
238
239            If the input is a buffer, "trailingData" will return everything
240            from the end of the compressed data stream to the end of the
241            buffer.
242
243            If the input is a filehandle, "trailingData" will return the data
244            that is left in the filehandle input buffer once the end of the
245            compressed data stream has been reached. You can then use the
246            filehandle to read the rest of the input file.
247
248            Don't bother using "trailingData" if the input is a filename.
249
250            If you know the length of the compressed data stream before you
251            start uncompressing, you can avoid having to use "trailingData" by
252            setting the "InputLength" option.
253
254   OneShot Examples
255       To read the contents of the file "file1.txt.bz2" and write the
256       uncompressed data to the file "file1.txt".
257
258           use strict ;
259           use warnings ;
260           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
261
262           my $input = "file1.txt.bz2";
263           my $output = "file1.txt";
264           bunzip2 $input => $output
265               or die "bunzip2 failed: $Bunzip2Error\n";
266
267       To read from an existing Perl filehandle, $input, and write the
268       uncompressed data to a buffer, $buffer.
269
270           use strict ;
271           use warnings ;
272           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
273           use IO::File ;
274
275           my $input = IO::File->new( "<file1.txt.bz2" )
276               or die "Cannot open 'file1.txt.bz2': $!\n" ;
277           my $buffer ;
278           bunzip2 $input => \$buffer
279               or die "bunzip2 failed: $Bunzip2Error\n";
280
281       To uncompress all files in the directory "/my/home" that match
282       "*.txt.bz2" and store the compressed data in the same directory
283
284           use strict ;
285           use warnings ;
286           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
287
288           bunzip2 '</my/home/*.txt.bz2>' => '</my/home/#1.txt>'
289               or die "bunzip2 failed: $Bunzip2Error\n";
290
291       and if you want to compress each file one at a time, this will do the
292       trick
293
294           use strict ;
295           use warnings ;
296           use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
297
298           for my $input ( glob "/my/home/*.txt.bz2" )
299           {
300               my $output = $input;
301               $output =~ s/.bz2// ;
302               bunzip2 $input => $output
303                   or die "Error compressing '$input': $Bunzip2Error\n";
304           }
305

OO Interface

307   Constructor
308       The format of the constructor for IO::Uncompress::Bunzip2 is shown
309       below
310
311           my $z = IO::Uncompress::Bunzip2->new( $input [OPTS] )
312               or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
313
314       The constructor takes one mandatory parameter, $input, defined below,
315       and zero or more "OPTS", defined in "Constructor Options".
316
317       Returns an "IO::Uncompress::Bunzip2" object on success and undef on
318       failure.  The variable $Bunzip2Error will contain an error message on
319       failure.
320
321       If you are running Perl 5.005 or better the object, $z, returned from
322       IO::Uncompress::Bunzip2 can be used exactly like an IO::File
323       filehandle.  This means that all normal input file operations can be
324       carried out with $z.  For example, to read a line from a compressed
325       file/buffer you can use either of these forms
326
327           $line = $z->getline();
328           $line = <$z>;
329
330       Below is a simple exaple of using the OO interface to read the
331       compressed file "myfile.bz2" and write its contents to stdout.
332
333           my $filename = "myfile.bz2";
334           my $z = IO::Uncompress::Bunzip2->new($filename)
335               or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
336
337           while (<$z>) {
338               print $_;
339           }
340           $z->close();
341
342       See "EXAMPLES" for further examples
343
344       The mandatory parameter $input is used to determine the source of the
345       compressed data. This parameter can take one of three forms.
346
347       A filename
348            If the $input parameter is a scalar, it is assumed to be a
349            filename. This file will be opened for reading and the compressed
350            data will be read from it.
351
352       A filehandle
353            If the $input parameter is a filehandle, the compressed data will
354            be read from it.  The string '-' can be used as an alias for
355            standard input.
356
357       A scalar reference
358            If $input is a scalar reference, the compressed data will be read
359            from $$input.
360
361   Constructor Options
362       The option names defined below are case insensitive and can be
363       optionally prefixed by a '-'.  So all of the following are valid
364
365           -AutoClose
366           -autoclose
367           AUTOCLOSE
368           autoclose
369
370       OPTS is a combination of the following options:
371
372       "AutoClose => 0|1"
373            This option is only valid when the $input parameter is a
374            filehandle. If specified, and the value is true, it will result in
375            the file being closed once either the "close" method is called or
376            the IO::Uncompress::Bunzip2 object is destroyed.
377
378            This parameter defaults to 0.
379
380       "MultiStream => 0|1"
381            Allows multiple concatenated compressed streams to be treated as a
382            single compressed stream. Decompression will stop once either the
383            end of the file/buffer is reached, an error is encountered
384            (premature eof, corrupt compressed data) or the end of a stream is
385            not immediately followed by the start of another stream.
386
387            This parameter defaults to 0.
388
389       "Prime => $string"
390            This option will uncompress the contents of $string before
391            processing the input file/buffer.
392
393            This option can be useful when the compressed data is embedded in
394            another file/data structure and it is not possible to work out
395            where the compressed data begins without having to read the first
396            few bytes. If this is the case, the uncompression can be primed
397            with these bytes using this option.
398
399       "Transparent => 0|1"
400            If this option is set and the input file/buffer is not compressed
401            data, the module will allow reading of it anyway.
402
403            In addition, if the input file/buffer does contain compressed data
404            and there is non-compressed data immediately following it, setting
405            this option will make this module treat the whole file/buffer as a
406            single data stream.
407
408            This option defaults to 1.
409
410       "BlockSize => $num"
411            When reading the compressed input data, IO::Uncompress::Bunzip2
412            will read it in blocks of $num bytes.
413
414            This option defaults to 4096.
415
416       "InputLength => $size"
417            When present this option will limit the number of compressed bytes
418            read from the input file/buffer to $size. This option can be used
419            in the situation where there is useful data directly after the
420            compressed data stream and you know beforehand the exact length of
421            the compressed data stream.
422
423            This option is mostly used when reading from a filehandle, in
424            which case the file pointer will be left pointing to the first
425            byte directly after the compressed data stream.
426
427            This option defaults to off.
428
429       "Append => 0|1"
430            This option controls what the "read" method does with uncompressed
431            data.
432
433            If set to 1, all uncompressed data will be appended to the output
434            parameter of the "read" method.
435
436            If set to 0, the contents of the output parameter of the "read"
437            method will be overwritten by the uncompressed data.
438
439            Defaults to 0.
440
441       "Strict => 0|1"
442            This option is a no-op.
443
444       "Small => 0|1"
445            When non-zero this options will make bzip2 use a decompression
446            algorithm that uses less memory at the expense of increasing the
447            amount of time taken for decompression.
448
449            Default is 0.
450

Methods

452   read
453       Usage is
454
455           $status = $z->read($buffer)
456
457       Reads a block of compressed data (the size of the compressed block is
458       determined by the "Buffer" option in the constructor), uncompresses it
459       and writes any uncompressed data into $buffer. If the "Append"
460       parameter is set in the constructor, the uncompressed data will be
461       appended to the $buffer parameter. Otherwise $buffer will be
462       overwritten.
463
464       Returns the number of uncompressed bytes written to $buffer, zero if
465       eof or a negative number on error.
466
467   read
468       Usage is
469
470           $status = $z->read($buffer, $length)
471           $status = $z->read($buffer, $length, $offset)
472
473           $status = read($z, $buffer, $length)
474           $status = read($z, $buffer, $length, $offset)
475
476       Attempt to read $length bytes of uncompressed data into $buffer.
477
478       The main difference between this form of the "read" method and the
479       previous one, is that this one will attempt to return exactly $length
480       bytes. The only circumstances that this function will not is if end-of-
481       file or an IO error is encountered.
482
483       Returns the number of uncompressed bytes written to $buffer, zero if
484       eof or a negative number on error.
485
486   getline
487       Usage is
488
489           $line = $z->getline()
490           $line = <$z>
491
492       Reads a single line.
493
494       This method fully supports the use of the variable $/ (or
495       $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
496       what constitutes an end of line. Paragraph mode, record mode and file
497       slurp mode are all supported.
498
499   getc
500       Usage is
501
502           $char = $z->getc()
503
504       Read a single character.
505
506   ungetc
507       Usage is
508
509           $char = $z->ungetc($string)
510
511   getHeaderInfo
512       Usage is
513
514           $hdr  = $z->getHeaderInfo();
515           @hdrs = $z->getHeaderInfo();
516
517       This method returns either a hash reference (in scalar context) or a
518       list or hash references (in array context) that contains information
519       about each of the header fields in the compressed data stream(s).
520
521   tell
522       Usage is
523
524           $z->tell()
525           tell $z
526
527       Returns the uncompressed file offset.
528
529   eof
530       Usage is
531
532           $z->eof();
533           eof($z);
534
535       Returns true if the end of the compressed input stream has been
536       reached.
537
538   seek
539           $z->seek($position, $whence);
540           seek($z, $position, $whence);
541
542       Provides a sub-set of the "seek" functionality, with the restriction
543       that it is only legal to seek forward in the input file/buffer.  It is
544       a fatal error to attempt to seek backward.
545
546       Note that the implementation of "seek" in this module does not provide
547       true random access to a compressed file/buffer. It  works by
548       uncompressing data from the current offset in the file/buffer until it
549       reaches the uncompressed offset specified in the parameters to "seek".
550       For very small files this may be acceptable behaviour. For large files
551       it may cause an unacceptable delay.
552
553       The $whence parameter takes one the usual values, namely SEEK_SET,
554       SEEK_CUR or SEEK_END.
555
556       Returns 1 on success, 0 on failure.
557
558   binmode
559       Usage is
560
561           $z->binmode
562           binmode $z ;
563
564       This is a noop provided for completeness.
565
566   opened
567           $z->opened()
568
569       Returns true if the object currently refers to a opened file/buffer.
570
571   autoflush
572           my $prev = $z->autoflush()
573           my $prev = $z->autoflush(EXPR)
574
575       If the $z object is associated with a file or a filehandle, this method
576       returns the current autoflush setting for the underlying filehandle. If
577       "EXPR" is present, and is non-zero, it will enable flushing after every
578       write/print operation.
579
580       If $z is associated with a buffer, this method has no effect and always
581       returns "undef".
582
583       Note that the special variable $| cannot be used to set or retrieve the
584       autoflush setting.
585
586   input_line_number
587           $z->input_line_number()
588           $z->input_line_number(EXPR)
589
590       Returns the current uncompressed line number. If "EXPR" is present it
591       has the effect of setting the line number. Note that setting the line
592       number does not change the current position within the file/buffer
593       being read.
594
595       The contents of $/ are used to determine what constitutes a line
596       terminator.
597
598   fileno
599           $z->fileno()
600           fileno($z)
601
602       If the $z object is associated with a file or a filehandle, "fileno"
603       will return the underlying file descriptor. Once the "close" method is
604       called "fileno" will return "undef".
605
606       If the $z object is associated with a buffer, this method will return
607       "undef".
608
609   close
610           $z->close() ;
611           close $z ;
612
613       Closes the output file/buffer.
614
615       For most versions of Perl this method will be automatically invoked if
616       the IO::Uncompress::Bunzip2 object is destroyed (either explicitly or
617       by the variable with the reference to the object going out of scope).
618       The exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
619       these cases, the "close" method will be called automatically, but not
620       until global destruction of all live objects when the program is
621       terminating.
622
623       Therefore, if you want your scripts to be able to run on all versions
624       of Perl, you should call "close" explicitly and not rely on automatic
625       closing.
626
627       Returns true on success, otherwise 0.
628
629       If the "AutoClose" option has been enabled when the
630       IO::Uncompress::Bunzip2 object was created, and the object is
631       associated with a file, the underlying file will also be closed.
632
633   nextStream
634       Usage is
635
636           my $status = $z->nextStream();
637
638       Skips to the next compressed data stream in the input file/buffer. If a
639       new compressed data stream is found, the eof marker will be cleared and
640       $.  will be reset to 0.
641
642       Returns 1 if a new stream was found, 0 if none was found, and -1 if an
643       error was encountered.
644
645   trailingData
646       Usage is
647
648           my $data = $z->trailingData();
649
650       Returns the data, if any, that is present immediately after the
651       compressed data stream once uncompression is complete. It only makes
652       sense to call this method once the end of the compressed data stream
653       has been encountered.
654
655       This option can be used when there is useful information immediately
656       following the compressed data stream, and you don't know the length of
657       the compressed data stream.
658
659       If the input is a buffer, "trailingData" will return everything from
660       the end of the compressed data stream to the end of the buffer.
661
662       If the input is a filehandle, "trailingData" will return the data that
663       is left in the filehandle input buffer once the end of the compressed
664       data stream has been reached. You can then use the filehandle to read
665       the rest of the input file.
666
667       Don't bother using "trailingData" if the input is a filename.
668
669       If you know the length of the compressed data stream before you start
670       uncompressing, you can avoid having to use "trailingData" by setting
671       the "InputLength" option in the constructor.
672

Importing

674       No symbolic constants are required by IO::Uncompress::Bunzip2 at
675       present.
676
677       :all Imports "bunzip2" and $Bunzip2Error.  Same as doing this
678
679                use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
680

EXAMPLES

682   Working with Net::FTP
683       See IO::Compress::FAQ
684

SUPPORT

686       General feedback/questions/bug reports should be sent to
687       <https://github.com/pmqs/IO-Compress/issues> (preferred) or
688       <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
689

SEE ALSO

691       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
692       IO::Compress::Deflate, IO::Uncompress::Inflate,
693       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
694       IO::Compress::Bzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma,
695       IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip,
696       IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
697       IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
698       IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
699       IO::Uncompress::AnyUncompress
700
701       IO::Compress::FAQ
702
703       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
704
705       The primary site for the bzip2 program is
706       <https://sourceware.org/bzip2/>.
707
708       See the module Compress::Bzip2
709

AUTHOR

711       This module was written by Paul Marquess, "pmqs@cpan.org".
712

MODIFICATION HISTORY

714       See the Changes file.
715
717       Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
718
719       This program is free software; you can redistribute it and/or modify it
720       under the same terms as Perl itself.
721
722
723
724perl v5.38.0                      2023-07-26        IO::Uncompress::Bunzip2(3)
Impressum