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