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

Methods

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

Importing

660       No symbolic constants are required by IO::Uncompress::Bunzip2 at
661       present.
662
663       :all Imports "bunzip2" and $Bunzip2Error.  Same as doing this
664
665                use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
666

EXAMPLES

668   Working with Net::FTP
669       See IO::Compress::FAQ
670

SUPPORT

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

SEE ALSO

677       Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
678       IO::Compress::Deflate, IO::Uncompress::Inflate,
679       IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
680       IO::Compress::Bzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma,
681       IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip,
682       IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
683       IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
684       IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
685       IO::Uncompress::AnyUncompress
686
687       IO::Compress::FAQ
688
689       File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
690
691       The primary site for the bzip2 program is
692       <https://sourceware.org/bzip2/>.
693
694       See the module Compress::Bzip2
695

AUTHOR

697       This module was written by Paul Marquess, "pmqs@cpan.org".
698

MODIFICATION HISTORY

700       See the Changes file.
701
703       Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
704
705       This program is free software; you can redistribute it and/or modify it
706       under the same terms as Perl itself.
707
708
709
710perl v5.36.0                      2023-02-09        IO::Uncompress::Bunzip2(3)
Impressum