1IO::Uncompress::Bunzip2U(s3e)r Contributed Perl DocumentaItOi:o:nUncompress::Bunzip2(3)
2
3
4
6 IO::Uncompress::Bunzip2 - Read bzip2 files/buffers
7
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 = new IO::Uncompress::Bunzip2 $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
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
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.
73
74 The $input_filename_or_reference parameter
75
76 The parameter, $input_filename_or_reference, is used to define the
77 source of the compressed data.
78
79 It can take one of the following forms:
80
81 A filename
82 If the <$input_filename_or_reference> parameter is a simple
83 scalar, it is assumed to be a filename. This file will be opened
84 for reading and the input data will be read from it.
85
86 A filehandle
87 If the $input_filename_or_reference parameter is a filehandle, the
88 input data will be read from it. The string '-' can be used as an
89 alias for standard input.
90
91 A scalar reference
92 If $input_filename_or_reference is a scalar reference, the input
93 data will be read from $$input_filename_or_reference.
94
95 An array reference
96 If $input_filename_or_reference is an array reference, each
97 element in the array must be a filename.
98
99 The input data will be read from each file in turn.
100
101 The complete array will be walked to ensure that it only contains
102 valid filenames before any data is uncompressed.
103
104 An Input FileGlob string
105 If $input_filename_or_reference is a string that is delimited by
106 the characters "<" and ">" "bunzip2" will assume that it is an
107 input fileglob string. The input is the list of files that match
108 the fileglob.
109
110 See File::GlobMapper for more details.
111
112 If the $input_filename_or_reference parameter is any other type,
113 "undef" will be returned.
114
115 The $output_filename_or_reference parameter
116
117 The parameter $output_filename_or_reference is used to control the
118 destination of the uncompressed data. This parameter can take one of
119 these forms.
120
121 A filename
122 If the $output_filename_or_reference parameter is a simple scalar,
123 it is assumed to be a filename. This file will be opened for
124 writing and the uncompressed data will be written to it.
125
126 A filehandle
127 If the $output_filename_or_reference parameter is a filehandle,
128 the uncompressed data will be written to it. The string '-' can
129 be used as an alias for standard output.
130
131 A scalar reference
132 If $output_filename_or_reference is a scalar reference, the
133 uncompressed data will be stored in
134 $$output_filename_or_reference.
135
136 An Array Reference
137 If $output_filename_or_reference is an array reference, the
138 uncompressed data will be pushed onto the array.
139
140 An Output FileGlob
141 If $output_filename_or_reference is a string that is delimited by
142 the characters "<" and ">" "bunzip2" will assume that it is an
143 output fileglob string. The output is the list of files that match
144 the fileglob.
145
146 When $output_filename_or_reference is an fileglob string,
147 $input_filename_or_reference must also be a fileglob string.
148 Anything else is an error.
149
150 See File::GlobMapper for more details.
151
152 If the $output_filename_or_reference parameter is any other type,
153 "undef" will be returned.
154
155 Notes
156 When $input_filename_or_reference maps to multiple compressed
157 files/buffers and $output_filename_or_reference is a single
158 file/buffer, after uncompression $output_filename_or_reference will
159 contain a concatenation of all the uncompressed data from each of the
160 input files/buffers.
161
162 Optional Parameters
163 Unless specified below, the optional parameters for "bunzip2", "OPTS",
164 are the same as those used with the OO interface defined in the
165 "Constructor Options" section below.
166
167 "AutoClose => 0|1"
168 This option applies to any input or output data streams to
169 "bunzip2" that are filehandles.
170
171 If "AutoClose" is specified, and the value is true, it will result
172 in all input and/or output filehandles being closed once "bunzip2"
173 has completed.
174
175 This parameter defaults to 0.
176
177 "BinModeOut => 0|1"
178 This option is now a no-op. All files will be written in binmode.
179
180 "Append => 0|1"
181 The behaviour of this option is dependent on the type of output
182 data stream.
183
184 · A Buffer
185
186 If "Append" is enabled, all uncompressed data will be append
187 to the end of the output buffer. Otherwise the output buffer
188 will be cleared before any uncompressed data is written to
189 it.
190
191 · A Filename
192
193 If "Append" is enabled, the file will be opened in append
194 mode. Otherwise the contents of the file, if any, will be
195 truncated before any uncompressed data is written to it.
196
197 · A Filehandle
198
199 If "Append" is enabled, the filehandle will be positioned to
200 the end of the file via a call to "seek" before any
201 uncompressed data is written to it. Otherwise the file
202 pointer will not be moved.
203
204 When "Append" is specified, and set to true, it will append all
205 uncompressed data to the output data stream.
206
207 So when the output is a filehandle it will carry out a seek to the
208 eof before writing any uncompressed data. If the output is a
209 filename, it will be opened for appending. If the output is a
210 buffer, all uncompressed data will be appended to the existing
211 buffer.
212
213 Conversely when "Append" is not specified, or it is present and is
214 set to false, it will operate as follows.
215
216 When the output is a filename, it will truncate the contents of
217 the file before writing any uncompressed data. If the output is a
218 filehandle its position will not be changed. If the output is a
219 buffer, it will be wiped before any uncompressed data is output.
220
221 Defaults to 0.
222
223 "MultiStream => 0|1"
224 If the input file/buffer contains multiple compressed data
225 streams, this option will uncompress the whole lot as a single
226 data stream.
227
228 Defaults to 0.
229
230 "TrailingData => $scalar"
231 Returns the data, if any, that is present immediately after the
232 compressed data stream once uncompression is complete.
233
234 This option can be used when there is useful information
235 immediately following the compressed data stream, and you don't
236 know the length of the compressed data stream.
237
238 If the input is a buffer, "trailingData" will return everything
239 from the end of the compressed data stream to the end of the
240 buffer.
241
242 If the input is a filehandle, "trailingData" will return the data
243 that is left in the filehandle input buffer once the end of the
244 compressed data stream has been reached. You can then use the
245 filehandle to read the rest of the input file.
246
247 Don't bother using "trailingData" if the input is a filename.
248
249 If you know the length of the compressed data stream before you
250 start uncompressing, you can avoid having to use "trailingData" by
251 setting the "InputLength" option.
252
253 Examples
254 To read the contents of the file "file1.txt.bz2" and write the
255 uncompressed data to the file "file1.txt".
256
257 use strict ;
258 use warnings ;
259 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
260
261 my $input = "file1.txt.bz2";
262 my $output = "file1.txt";
263 bunzip2 $input => $output
264 or die "bunzip2 failed: $Bunzip2Error\n";
265
266 To read from an existing Perl filehandle, $input, and write the
267 uncompressed data to a buffer, $buffer.
268
269 use strict ;
270 use warnings ;
271 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
272 use IO::File ;
273
274 my $input = new IO::File "<file1.txt.bz2"
275 or die "Cannot open 'file1.txt.bz2': $!\n" ;
276 my $buffer ;
277 bunzip2 $input => \$buffer
278 or die "bunzip2 failed: $Bunzip2Error\n";
279
280 To uncompress all files in the directory "/my/home" that match
281 "*.txt.bz2" and store the compressed data in the same directory
282
283 use strict ;
284 use warnings ;
285 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
286
287 bunzip2 '</my/home/*.txt.bz2>' => '</my/home/#1.txt>'
288 or die "bunzip2 failed: $Bunzip2Error\n";
289
290 and if you want to compress each file one at a time, this will do the
291 trick
292
293 use strict ;
294 use warnings ;
295 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
296
297 for my $input ( glob "/my/home/*.txt.bz2" )
298 {
299 my $output = $input;
300 $output =~ s/.bz2// ;
301 bunzip2 $input => $output
302 or die "Error compressing '$input': $Bunzip2Error\n";
303 }
304
306 Constructor
307 The format of the constructor for IO::Uncompress::Bunzip2 is shown
308 below
309
310 my $z = new IO::Uncompress::Bunzip2 $input [OPTS]
311 or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
312
313 Returns an "IO::Uncompress::Bunzip2" object on success and undef on
314 failure. The variable $Bunzip2Error will contain an error message on
315 failure.
316
317 If you are running Perl 5.005 or better the object, $z, returned from
318 IO::Uncompress::Bunzip2 can be used exactly like an IO::File
319 filehandle. This means that all normal input file operations can be
320 carried out with $z. For example, to read a line from a compressed
321 file/buffer you can use either of these forms
322
323 $line = $z->getline();
324 $line = <$z>;
325
326 The mandatory parameter $input is used to determine the source of the
327 compressed data. This parameter can take one of three forms.
328
329 A filename
330 If the $input parameter is a scalar, it is assumed to be a
331 filename. This file will be opened for reading and the compressed
332 data will be read from it.
333
334 A filehandle
335 If the $input parameter is a filehandle, the compressed data will
336 be read from it. The string '-' can be used as an alias for
337 standard input.
338
339 A scalar reference
340 If $input is a scalar reference, the compressed data will be read
341 from $$input.
342
343 Constructor Options
344 The option names defined below are case insensitive and can be
345 optionally prefixed by a '-'. So all of the following are valid
346
347 -AutoClose
348 -autoclose
349 AUTOCLOSE
350 autoclose
351
352 OPTS is a combination of the following options:
353
354 "AutoClose => 0|1"
355 This option is only valid when the $input parameter is a
356 filehandle. If specified, and the value is true, it will result in
357 the file being closed once either the "close" method is called or
358 the IO::Uncompress::Bunzip2 object is destroyed.
359
360 This parameter defaults to 0.
361
362 "MultiStream => 0|1"
363 Allows multiple concatenated compressed streams to be treated as a
364 single compressed stream. Decompression will stop once either the
365 end of the file/buffer is reached, an error is encountered
366 (premature eof, corrupt compressed data) or the end of a stream is
367 not immediately followed by the start of another stream.
368
369 This parameter defaults to 0.
370
371 "Prime => $string"
372 This option will uncompress the contents of $string before
373 processing the input file/buffer.
374
375 This option can be useful when the compressed data is embedded in
376 another file/data structure and it is not possible to work out
377 where the compressed data begins without having to read the first
378 few bytes. If this is the case, the uncompression can be primed
379 with these bytes using this option.
380
381 "Transparent => 0|1"
382 If this option is set and the input file/buffer is not compressed
383 data, the module will allow reading of it anyway.
384
385 In addition, if the input file/buffer does contain compressed data
386 and there is non-compressed data immediately following it, setting
387 this option will make this module treat the whole file/buffer as a
388 single data stream.
389
390 This option defaults to 1.
391
392 "BlockSize => $num"
393 When reading the compressed input data, IO::Uncompress::Bunzip2
394 will read it in blocks of $num bytes.
395
396 This option defaults to 4096.
397
398 "InputLength => $size"
399 When present this option will limit the number of compressed bytes
400 read from the input file/buffer to $size. This option can be used
401 in the situation where there is useful data directly after the
402 compressed data stream and you know beforehand the exact length of
403 the compressed data stream.
404
405 This option is mostly used when reading from a filehandle, in
406 which case the file pointer will be left pointing to the first
407 byte directly after the compressed data stream.
408
409 This option defaults to off.
410
411 "Append => 0|1"
412 This option controls what the "read" method does with uncompressed
413 data.
414
415 If set to 1, all uncompressed data will be appended to the output
416 parameter of the "read" method.
417
418 If set to 0, the contents of the output parameter of the "read"
419 method will be overwritten by the uncompressed data.
420
421 Defaults to 0.
422
423 "Strict => 0|1"
424 This option is a no-op.
425
426 "Small => 0|1"
427 When non-zero this options will make bzip2 use a decompression
428 algorithm that uses less memory at the expense of increasing the
429 amount of time taken for decompression.
430
431 Default is 0.
432
433 Examples
434 TODO
435
437 read
438 Usage is
439
440 $status = $z->read($buffer)
441
442 Reads a block of compressed data (the size of the compressed block is
443 determined by the "Buffer" option in the constructor), uncompresses it
444 and writes any uncompressed data into $buffer. If the "Append"
445 parameter is set in the constructor, the uncompressed data will be
446 appended to the $buffer parameter. Otherwise $buffer will be
447 overwritten.
448
449 Returns the number of uncompressed bytes written to $buffer, zero if
450 eof or a negative number on error.
451
452 read
453 Usage is
454
455 $status = $z->read($buffer, $length)
456 $status = $z->read($buffer, $length, $offset)
457
458 $status = read($z, $buffer, $length)
459 $status = read($z, $buffer, $length, $offset)
460
461 Attempt to read $length bytes of uncompressed data into $buffer.
462
463 The main difference between this form of the "read" method and the
464 previous one, is that this one will attempt to return exactly $length
465 bytes. The only circumstances that this function will not is if end-of-
466 file or an IO error is encountered.
467
468 Returns the number of uncompressed bytes written to $buffer, zero if
469 eof or a negative number on error.
470
471 getline
472 Usage is
473
474 $line = $z->getline()
475 $line = <$z>
476
477 Reads a single line.
478
479 This method fully supports the use of the variable $/ (or
480 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
481 what constitutes an end of line. Paragraph mode, record mode and file
482 slurp mode are all supported.
483
484 getc
485 Usage is
486
487 $char = $z->getc()
488
489 Read a single character.
490
491 ungetc
492 Usage is
493
494 $char = $z->ungetc($string)
495
496 getHeaderInfo
497 Usage is
498
499 $hdr = $z->getHeaderInfo();
500 @hdrs = $z->getHeaderInfo();
501
502 This method returns either a hash reference (in scalar context) or a
503 list or hash references (in array context) that contains information
504 about each of the header fields in the compressed data stream(s).
505
506 tell
507 Usage is
508
509 $z->tell()
510 tell $z
511
512 Returns the uncompressed file offset.
513
514 eof
515 Usage is
516
517 $z->eof();
518 eof($z);
519
520 Returns true if the end of the compressed input stream has been
521 reached.
522
523 seek
524 $z->seek($position, $whence);
525 seek($z, $position, $whence);
526
527 Provides a sub-set of the "seek" functionality, with the restriction
528 that it is only legal to seek forward in the input file/buffer. It is
529 a fatal error to attempt to seek backward.
530
531 Note that the implementation of "seek" in this module does not provide
532 true random access to a compressed file/buffer. It works by
533 uncompressing data from the current offset in the file/buffer until it
534 reaches the uncompressed offset specified in the parameters to "seek".
535 For very small files this may be acceptable behaviour. For large files
536 it may cause an unacceptable delay.
537
538 The $whence parameter takes one the usual values, namely SEEK_SET,
539 SEEK_CUR or SEEK_END.
540
541 Returns 1 on success, 0 on failure.
542
543 binmode
544 Usage is
545
546 $z->binmode
547 binmode $z ;
548
549 This is a noop provided for completeness.
550
551 opened
552 $z->opened()
553
554 Returns true if the object currently refers to a opened file/buffer.
555
556 autoflush
557 my $prev = $z->autoflush()
558 my $prev = $z->autoflush(EXPR)
559
560 If the $z object is associated with a file or a filehandle, this method
561 returns the current autoflush setting for the underlying filehandle. If
562 "EXPR" is present, and is non-zero, it will enable flushing after every
563 write/print operation.
564
565 If $z is associated with a buffer, this method has no effect and always
566 returns "undef".
567
568 Note that the special variable $| cannot be used to set or retrieve the
569 autoflush setting.
570
571 input_line_number
572 $z->input_line_number()
573 $z->input_line_number(EXPR)
574
575 Returns the current uncompressed line number. If "EXPR" is present it
576 has the effect of setting the line number. Note that setting the line
577 number does not change the current position within the file/buffer
578 being read.
579
580 The contents of $/ are used to determine what constitutes a line
581 terminator.
582
583 fileno
584 $z->fileno()
585 fileno($z)
586
587 If the $z object is associated with a file or a filehandle, "fileno"
588 will return the underlying file descriptor. Once the "close" method is
589 called "fileno" will return "undef".
590
591 If the $z object is associated with a buffer, this method will return
592 "undef".
593
594 close
595 $z->close() ;
596 close $z ;
597
598 Closes the output file/buffer.
599
600 For most versions of Perl this method will be automatically invoked if
601 the IO::Uncompress::Bunzip2 object is destroyed (either explicitly or
602 by the variable with the reference to the object going out of scope).
603 The exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
604 these cases, the "close" method will be called automatically, but not
605 until global destruction of all live objects when the program is
606 terminating.
607
608 Therefore, if you want your scripts to be able to run on all versions
609 of Perl, you should call "close" explicitly and not rely on automatic
610 closing.
611
612 Returns true on success, otherwise 0.
613
614 If the "AutoClose" option has been enabled when the
615 IO::Uncompress::Bunzip2 object was created, and the object is
616 associated with a file, the underlying file will also be closed.
617
618 nextStream
619 Usage is
620
621 my $status = $z->nextStream();
622
623 Skips to the next compressed data stream in the input file/buffer. If a
624 new compressed data stream is found, the eof marker will be cleared and
625 $. will be reset to 0.
626
627 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
628 error was encountered.
629
630 trailingData
631 Usage is
632
633 my $data = $z->trailingData();
634
635 Returns the data, if any, that is present immediately after the
636 compressed data stream once uncompression is complete. It only makes
637 sense to call this method once the end of the compressed data stream
638 has been encountered.
639
640 This option can be used when there is useful information immediately
641 following the compressed data stream, and you don't know the length of
642 the compressed data stream.
643
644 If the input is a buffer, "trailingData" will return everything from
645 the end of the compressed data stream to the end of the buffer.
646
647 If the input is a filehandle, "trailingData" will return the data that
648 is left in the filehandle input buffer once the end of the compressed
649 data stream has been reached. You can then use the filehandle to read
650 the rest of the input file.
651
652 Don't bother using "trailingData" if the input is a filename.
653
654 If you know the length of the compressed data stream before you start
655 uncompressing, you can avoid having to use "trailingData" by setting
656 the "InputLength" option in the constructor.
657
659 No symbolic constants are required by this IO::Uncompress::Bunzip2 at
660 present.
661
662 :all Imports "bunzip2" and $Bunzip2Error. Same as doing this
663
664 use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
665
667 Working with Net::FTP
668 See IO::Compress::FAQ
669
671 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip,
672 IO::Compress::Deflate, IO::Uncompress::Inflate,
673 IO::Compress::RawDeflate, IO::Uncompress::RawInflate,
674 IO::Compress::Bzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma,
675 IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip,
676 IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
677 IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
678 IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
679 IO::Uncompress::AnyUncompress
680
681 IO::Compress::FAQ
682
683 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
684
685 The primary site for the bzip2 program is <http://www.bzip.org>.
686
687 See the module Compress::Bzip2
688
690 This module was written by Paul Marquess, "pmqs@cpan.org".
691
693 See the Changes file.
694
696 Copyright (c) 2005-2019 Paul Marquess. All rights reserved.
697
698 This program is free software; you can redistribute it and/or modify it
699 under the same terms as Perl itself.
700
701
702
703perl v5.30.0 2019-08-12 IO::Uncompress::Bunzip2(3)