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