Move IO::Compress from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / IO-Compress / lib / IO / Compress / Bzip2.pm
CommitLineData
319fab50 1package IO::Compress::Bzip2 ;
25f0751f 2
3use strict ;
4use warnings;
5use bytes;
25f0751f 6require Exporter ;
7
10c2b2bb 8use IO::Compress::Base 2.021 ;
319fab50 9
10c2b2bb 10use IO::Compress::Base::Common 2.021 qw(createSelfTiedObject);
11use IO::Compress::Adapter::Bzip2 2.021 ;
25f0751f 12
25f0751f 13
14
319fab50 15our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $Bzip2Error);
25f0751f 16
10c2b2bb 17$VERSION = '2.021';
319fab50 18$Bzip2Error = '';
25f0751f 19
319fab50 20@ISA = qw(Exporter IO::Compress::Base);
21@EXPORT_OK = qw( $Bzip2Error bzip2 ) ;
22%EXPORT_TAGS = %IO::Compress::Base::EXPORT_TAGS ;
25f0751f 23push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
24Exporter::export_ok_tags('all');
25
26
319fab50 27
25f0751f 28sub new
29{
30 my $class = shift ;
31
319fab50 32 my $obj = createSelfTiedObject($class, \$Bzip2Error);
25f0751f 33 return $obj->_create(undef, @_);
34}
35
319fab50 36sub bzip2
25f0751f 37{
319fab50 38 my $obj = createSelfTiedObject(undef, \$Bzip2Error);
39 $obj->_def(@_);
25f0751f 40}
41
42
319fab50 43sub mkHeader
25f0751f 44{
319fab50 45 my $self = shift ;
46 return '';
25f0751f 47
25f0751f 48}
49
319fab50 50sub getExtraParams
25f0751f 51{
319fab50 52 my $self = shift ;
25f0751f 53
10c2b2bb 54 use IO::Compress::Base::Common 2.021 qw(:Parse);
319fab50 55
56 return (
57 'BlockSize100K' => [0, 1, Parse_unsigned, 1],
58 'WorkFactor' => [0, 1, Parse_unsigned, 0],
59 'Verbosity' => [0, 1, Parse_boolean, 0],
60 );
61}
25f0751f 62
25f0751f 63
25f0751f 64
319fab50 65sub ckParams
25f0751f 66{
67 my $self = shift ;
319fab50 68 my $got = shift;
69
70 # check that BlockSize100K is a number between 1 & 9
71 if ($got->parsed('BlockSize100K')) {
72 my $value = $got->value('BlockSize100K');
73 return $self->saveErrorString(undef, "Parameter 'BlockSize100K' not between 1 and 9, got $value")
74 unless defined $value && $value >= 1 && $value <= 9;
25f0751f 75
25f0751f 76 }
319fab50 77
78 # check that WorkFactor between 0 & 250
79 if ($got->parsed('WorkFactor')) {
80 my $value = $got->value('WorkFactor');
81 return $self->saveErrorString(undef, "Parameter 'WorkFactor' not between 0 and 250, got $value")
82 unless $value >= 0 && $value <= 250;
25f0751f 83 }
84
319fab50 85 return 1 ;
25f0751f 86}
87
319fab50 88
89sub mkComp
25f0751f 90{
91 my $self = shift ;
319fab50 92 my $got = shift ;
93
94 my $BlockSize100K = $got->value('BlockSize100K');
95 my $WorkFactor = $got->value('WorkFactor');
96 my $Verbosity = $got->value('Verbosity');
97
98 my ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject(
99 $BlockSize100K, $WorkFactor,
100 $Verbosity);
101
102 return $self->saveErrorString(undef, $errstr, $errno)
103 if ! defined $obj;
25f0751f 104
319fab50 105 return $obj;
25f0751f 106}
107
108
109sub mkTrailer
110{
111 my $self = shift ;
319fab50 112 return '';
25f0751f 113}
114
115sub mkFinalTrailer
116{
117 return '';
118}
119
120#sub newHeader
121#{
122# my $self = shift ;
319fab50 123# return '';
25f0751f 124#}
125
25f0751f 126sub getInverseClass
127{
319fab50 128 return ('IO::Uncompress::Bunzip2');
25f0751f 129}
130
131sub getFileInfo
132{
133 my $self = shift ;
134 my $params = shift;
135 my $file = shift ;
136
137}
138
25f0751f 1391;
140
141__END__
142
143=head1 NAME
144
319fab50 145IO::Compress::Bzip2 - Write bzip2 files/buffers
cb7abd7f 146
25f0751f 147
148
149=head1 SYNOPSIS
150
319fab50 151 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 152
319fab50 153 my $status = bzip2 $input => $output [,OPTS]
154 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 155
319fab50 156 my $z = new IO::Compress::Bzip2 $output [,OPTS]
157 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 158
159 $z->print($string);
160 $z->printf($format, $string);
161 $z->write($string);
162 $z->syswrite($string [, $length, $offset]);
163 $z->flush();
164 $z->tell();
165 $z->eof();
166 $z->seek($position, $whence);
167 $z->binmode();
168 $z->fileno();
169 $z->opened();
170 $z->autoflush();
171 $z->input_line_number();
172 $z->newStream( [OPTS] );
173
25f0751f 174 $z->close() ;
175
319fab50 176 $Bzip2Error ;
25f0751f 177
178 # IO::File mode
179
180 print $z $string;
181 printf $z $format, $string;
182 tell $z
183 eof $z
184 seek $z, $position, $whence
185 binmode $z
186 fileno $z
187 close $z ;
188
189
190=head1 DESCRIPTION
191
319fab50 192This module provides a Perl interface that allows writing bzip2
193compressed data to files or buffer.
25f0751f 194
319fab50 195For reading bzip2 files/buffers, see the companion module
196L<IO::Uncompress::Bunzip2|IO::Uncompress::Bunzip2>.
25f0751f 197
25f0751f 198=head1 Functional Interface
199
319fab50 200A top-level function, C<bzip2>, is provided to carry out
25f0751f 201"one-shot" compression between buffers and/or files. For finer
202control over the compression process, see the L</"OO Interface">
203section.
204
319fab50 205 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 206
319fab50 207 bzip2 $input => $output [,OPTS]
208 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 209
25f0751f 210The functional interface needs Perl5.005 or better.
211
319fab50 212=head2 bzip2 $input => $output [, OPTS]
25f0751f 213
319fab50 214C<bzip2> expects at least two parameters, C<$input> and C<$output>.
25f0751f 215
216=head3 The C<$input> parameter
217
218The parameter, C<$input>, is used to define the source of
219the uncompressed data.
220
221It can take one of the following forms:
222
223=over 5
224
225=item A filename
226
227If the C<$input> parameter is a simple scalar, it is assumed to be a
228filename. This file will be opened for reading and the input data
229will be read from it.
230
231=item A filehandle
232
233If the C<$input> parameter is a filehandle, the input data will be
234read from it.
235The string '-' can be used as an alias for standard input.
236
237=item A scalar reference
238
239If C<$input> is a scalar reference, the input data will be read
240from C<$$input>.
241
242=item An array reference
243
244If C<$input> is an array reference, each element in the array must be a
245filename.
246
247The input data will be read from each file in turn.
248
249The complete array will be walked to ensure that it only
250contains valid filenames before any data is compressed.
251
25f0751f 252=item An Input FileGlob string
253
254If C<$input> is a string that is delimited by the characters "<" and ">"
319fab50 255C<bzip2> will assume that it is an I<input fileglob string>. The
25f0751f 256input is the list of files that match the fileglob.
257
258If the fileglob does not match any files ...
259
260See L<File::GlobMapper|File::GlobMapper> for more details.
261
25f0751f 262=back
263
264If the C<$input> parameter is any other type, C<undef> will be returned.
265
25f0751f 266=head3 The C<$output> parameter
267
268The parameter C<$output> is used to control the destination of the
269compressed data. This parameter can take one of these forms.
270
271=over 5
272
273=item A filename
274
275If the C<$output> parameter is a simple scalar, it is assumed to be a
276filename. This file will be opened for writing and the compressed
277data will be written to it.
278
279=item A filehandle
280
281If the C<$output> parameter is a filehandle, the compressed data
282will be written to it.
283The string '-' can be used as an alias for standard output.
284
25f0751f 285=item A scalar reference
286
287If C<$output> is a scalar reference, the compressed data will be
288stored in C<$$output>.
289
25f0751f 290=item An Array Reference
291
292If C<$output> is an array reference, the compressed data will be
293pushed onto the array.
294
295=item An Output FileGlob
296
297If C<$output> is a string that is delimited by the characters "<" and ">"
319fab50 298C<bzip2> will assume that it is an I<output fileglob string>. The
25f0751f 299output is the list of files that match the fileglob.
300
301When C<$output> is an fileglob string, C<$input> must also be a fileglob
302string. Anything else is an error.
303
304=back
305
306If the C<$output> parameter is any other type, C<undef> will be returned.
307
25f0751f 308=head2 Notes
309
310When C<$input> maps to multiple files/buffers and C<$output> is a single
c70c1701 311file/buffer the input files/buffers will be stored
312in C<$output> as a concatenated series of compressed data streams.
313
25f0751f 314=head2 Optional Parameters
315
319fab50 316Unless specified below, the optional parameters for C<bzip2>,
25f0751f 317C<OPTS>, are the same as those used with the OO interface defined in the
318L</"Constructor Options"> section below.
319
320=over 5
321
e7d45986 322=item C<< AutoClose => 0|1 >>
25f0751f 323
324This option applies to any input or output data streams to
319fab50 325C<bzip2> that are filehandles.
25f0751f 326
327If C<AutoClose> is specified, and the value is true, it will result in all
319fab50 328input and/or output filehandles being closed once C<bzip2> has
25f0751f 329completed.
330
331This parameter defaults to 0.
332
e7d45986 333=item C<< BinModeIn => 0|1 >>
25f0751f 334
335When reading from a file or filehandle, set C<binmode> before reading.
336
337Defaults to 0.
338
e7d45986 339=item C<< Append => 0|1 >>
25f0751f 340
341TODO
342
25f0751f 343=back
344
25f0751f 345=head2 Examples
346
347To read the contents of the file C<file1.txt> and write the compressed
319fab50 348data to the file C<file1.txt.bz2>.
25f0751f 349
350 use strict ;
351 use warnings ;
319fab50 352 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 353
354 my $input = "file1.txt";
319fab50 355 bzip2 $input => "$input.bz2"
356 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 357
25f0751f 358To read from an existing Perl filehandle, C<$input>, and write the
359compressed data to a buffer, C<$buffer>.
360
361 use strict ;
362 use warnings ;
319fab50 363 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 364 use IO::File ;
365
366 my $input = new IO::File "<file1.txt"
367 or die "Cannot open 'file1.txt': $!\n" ;
368 my $buffer ;
319fab50 369 bzip2 $input => \$buffer
370 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 371
372To compress all files in the directory "/my/home" that match "*.txt"
373and store the compressed data in the same directory
374
375 use strict ;
376 use warnings ;
319fab50 377 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 378
319fab50 379 bzip2 '</my/home/*.txt>' => '<*.bz2>'
380 or die "bzip2 failed: $Bzip2Error\n";
25f0751f 381
382and if you want to compress each file one at a time, this will do the trick
383
384 use strict ;
385 use warnings ;
319fab50 386 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 387
388 for my $input ( glob "/my/home/*.txt" )
389 {
319fab50 390 my $output = "$input.bz2" ;
391 bzip2 $input => $output
392 or die "Error compressing '$input': $Bzip2Error\n";
25f0751f 393 }
394
25f0751f 395=head1 OO Interface
396
397=head2 Constructor
398
319fab50 399The format of the constructor for C<IO::Compress::Bzip2> is shown below
25f0751f 400
319fab50 401 my $z = new IO::Compress::Bzip2 $output [,OPTS]
402 or die "IO::Compress::Bzip2 failed: $Bzip2Error\n";
25f0751f 403
319fab50 404It returns an C<IO::Compress::Bzip2> object on success and undef on failure.
405The variable C<$Bzip2Error> will contain an error message on failure.
25f0751f 406
407If you are running Perl 5.005 or better the object, C<$z>, returned from
319fab50 408IO::Compress::Bzip2 can be used exactly like an L<IO::File|IO::File> filehandle.
25f0751f 409This means that all normal output file operations can be carried out
410with C<$z>.
411For example, to write to a compressed file/buffer you can use either of
412these forms
413
414 $z->print("hello world\n");
415 print $z "hello world\n";
416
417The mandatory parameter C<$output> is used to control the destination
418of the compressed data. This parameter can take one of these forms.
419
420=over 5
421
422=item A filename
423
424If the C<$output> parameter is a simple scalar, it is assumed to be a
425filename. This file will be opened for writing and the compressed data
426will be written to it.
427
428=item A filehandle
429
430If the C<$output> parameter is a filehandle, the compressed data will be
431written to it.
432The string '-' can be used as an alias for standard output.
433
25f0751f 434=item A scalar reference
435
436If C<$output> is a scalar reference, the compressed data will be stored
437in C<$$output>.
438
439=back
440
319fab50 441If the C<$output> parameter is any other type, C<IO::Compress::Bzip2>::new will
25f0751f 442return undef.
443
444=head2 Constructor Options
445
446C<OPTS> is any combination of the following options:
447
448=over 5
449
e7d45986 450=item C<< AutoClose => 0|1 >>
25f0751f 451
452This option is only valid when the C<$output> parameter is a filehandle. If
453specified, and the value is true, it will result in the C<$output> being
319fab50 454closed once either the C<close> method is called or the C<IO::Compress::Bzip2>
25f0751f 455object is destroyed.
456
457This parameter defaults to 0.
458
e7d45986 459=item C<< Append => 0|1 >>
25f0751f 460
461Opens C<$output> in append mode.
462
463The behaviour of this option is dependent on the type of C<$output>.
464
465=over 5
466
467=item * A Buffer
468
469If C<$output> is a buffer and C<Append> is enabled, all compressed data
470will be append to the end if C<$output>. Otherwise C<$output> will be
471cleared before any data is written to it.
472
473=item * A Filename
474
475If C<$output> is a filename and C<Append> is enabled, the file will be
476opened in append mode. Otherwise the contents of the file, if any, will be
477truncated before any compressed data is written to it.
478
479=item * A Filehandle
480
481If C<$output> is a filehandle, the file pointer will be positioned to the
482end of the file via a call to C<seek> before any compressed data is written
483to it. Otherwise the file pointer will not be moved.
484
485=back
486
487This parameter defaults to 0.
488
319fab50 489=item C<< BlockSize100K => number >>
25f0751f 490
319fab50 491Specify the number of 100K blocks bzip2 uses during compression.
25f0751f 492
319fab50 493Valid values are from 1 to 9, where 9 is best compression.
25f0751f 494
319fab50 495The default is 1.
25f0751f 496
319fab50 497=item C<< WorkFactor => number >>
25f0751f 498
319fab50 499Specifies how much effort bzip2 should take before resorting to a slower
500fallback compression algorithm.
25f0751f 501
319fab50 502Valid values range from 0 to 250, where 0 means use the default value 30.
25f0751f 503
319fab50 504The default is 0.
25f0751f 505
e7d45986 506=item C<< Strict => 0|1 >>
25f0751f 507
25f0751f 508This is a placeholder option.
509
25f0751f 510=back
511
512=head2 Examples
513
514TODO
515
516=head1 Methods
517
518=head2 print
519
520Usage is
521
522 $z->print($data)
523 print $z $data
524
525Compresses and outputs the contents of the C<$data> parameter. This
526has the same behaviour as the C<print> built-in.
527
528Returns true if successful.
529
530=head2 printf
531
532Usage is
533
534 $z->printf($format, $data)
535 printf $z $format, $data
536
537Compresses and outputs the contents of the C<$data> parameter.
538
539Returns true if successful.
540
541=head2 syswrite
542
543Usage is
544
545 $z->syswrite $data
546 $z->syswrite $data, $length
547 $z->syswrite $data, $length, $offset
548
549Compresses and outputs the contents of the C<$data> parameter.
550
551Returns the number of uncompressed bytes written, or C<undef> if
552unsuccessful.
553
554=head2 write
555
556Usage is
557
558 $z->write $data
559 $z->write $data, $length
560 $z->write $data, $length, $offset
561
562Compresses and outputs the contents of the C<$data> parameter.
563
564Returns the number of uncompressed bytes written, or C<undef> if
565unsuccessful.
566
567=head2 flush
568
569Usage is
570
25f0751f 571 $z->flush;
25f0751f 572
25f0751f 573Flushes any pending compressed data to the output file/buffer.
574
319fab50 575TODO
25f0751f 576
25f0751f 577Returns true on success.
578
25f0751f 579=head2 tell
580
581Usage is
582
583 $z->tell()
584 tell $z
585
586Returns the uncompressed file offset.
587
588=head2 eof
589
590Usage is
591
592 $z->eof();
593 eof($z);
594
25f0751f 595Returns true if the C<close> method has been called.
596
25f0751f 597=head2 seek
598
599 $z->seek($position, $whence);
600 seek($z, $position, $whence);
601
25f0751f 602Provides a sub-set of the C<seek> functionality, with the restriction
603that it is only legal to seek forward in the output file/buffer.
604It is a fatal error to attempt to seek backward.
605
606Empty parts of the file/buffer will have NULL (0x00) bytes written to them.
607
25f0751f 608The C<$whence> parameter takes one the usual values, namely SEEK_SET,
609SEEK_CUR or SEEK_END.
610
611Returns 1 on success, 0 on failure.
612
613=head2 binmode
614
615Usage is
616
617 $z->binmode
618 binmode $z ;
619
620This is a noop provided for completeness.
621
622=head2 opened
623
624 $z->opened()
625
626Returns true if the object currently refers to a opened file/buffer.
627
628=head2 autoflush
629
630 my $prev = $z->autoflush()
631 my $prev = $z->autoflush(EXPR)
632
633If the C<$z> object is associated with a file or a filehandle, this method
634returns the current autoflush setting for the underlying filehandle. If
635C<EXPR> is present, and is non-zero, it will enable flushing after every
636write/print operation.
637
638If C<$z> is associated with a buffer, this method has no effect and always
639returns C<undef>.
640
641B<Note> that the special variable C<$|> B<cannot> be used to set or
642retrieve the autoflush setting.
643
644=head2 input_line_number
645
646 $z->input_line_number()
647 $z->input_line_number(EXPR)
648
25f0751f 649This method always returns C<undef> when compressing.
650
25f0751f 651=head2 fileno
652
653 $z->fileno()
654 fileno($z)
655
d54256af 656If the C<$z> object is associated with a file or a filehandle, C<fileno>
657will return the underlying file descriptor. Once the C<close> method is
658called C<fileno> will return C<undef>.
25f0751f 659
d54256af 660If the C<$z> object is is associated with a buffer, this method will return
661C<undef>.
25f0751f 662
663=head2 close
664
665 $z->close() ;
666 close $z ;
667
25f0751f 668Flushes any pending compressed data and then closes the output file/buffer.
669
25f0751f 670For most versions of Perl this method will be automatically invoked if
319fab50 671the IO::Compress::Bzip2 object is destroyed (either explicitly or by the
25f0751f 672variable with the reference to the object going out of scope). The
673exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
674these cases, the C<close> method will be called automatically, but
675not until global destruction of all live objects when the program is
676terminating.
677
678Therefore, if you want your scripts to be able to run on all versions
679of Perl, you should call C<close> explicitly and not rely on automatic
680closing.
681
682Returns true on success, otherwise 0.
683
319fab50 684If the C<AutoClose> option has been enabled when the IO::Compress::Bzip2
25f0751f 685object was created, and the object is associated with a file, the
686underlying file will also be closed.
687
25f0751f 688=head2 newStream([OPTS])
689
690Usage is
691
692 $z->newStream( [OPTS] )
693
694Closes the current compressed data stream and starts a new one.
695
e7d45986 696OPTS consists of any of the the options that are available when creating
697the C<$z> object.
25f0751f 698
e7d45986 699See the L</"Constructor Options"> section for more details.
25f0751f 700
25f0751f 701=head1 Importing
702
319fab50 703No symbolic constants are required by this IO::Compress::Bzip2 at present.
25f0751f 704
25f0751f 705=over 5
706
707=item :all
708
319fab50 709Imports C<bzip2> and C<$Bzip2Error>.
710Same as doing this
25f0751f 711
319fab50 712 use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error) ;
25f0751f 713
25f0751f 714
715
716=back
717
25f0751f 718=head1 EXAMPLES
719
d54256af 720=head2 Apache::GZip Revisited
e7d45986 721
319fab50 722See L<IO::Compress::Bzip2::FAQ|IO::Compress::Bzip2::FAQ/"Apache::GZip Revisited">
e7d45986 723
d54256af 724
e7d45986 725
d54256af 726=head2 Working with Net::FTP
e7d45986 727
319fab50 728See L<IO::Compress::Bzip2::FAQ|IO::Compress::Bzip2::FAQ/"Compressed files and Net::FTP">
e7d45986 729
25f0751f 730=head1 SEE ALSO
731
319fab50 732L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
25f0751f 733
734L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
735
736L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
737L<Archive::Tar|Archive::Tar>,
738L<IO::Zlib|IO::Zlib>
739
319fab50 740The primary site for the bzip2 program is F<http://www.bzip.org>.
25f0751f 741
319fab50 742See the module L<Compress::Bzip2|Compress::Bzip2>
25f0751f 743
25f0751f 744=head1 AUTHOR
745
cb7abd7f 746This module was written by Paul Marquess, F<pmqs@cpan.org>.
25f0751f 747
25f0751f 748=head1 MODIFICATION HISTORY
749
750See the Changes file.
751
752=head1 COPYRIGHT AND LICENSE
25f0751f 753
d54256af 754Copyright (c) 2005-2008 Paul Marquess. All rights reserved.
25f0751f 755
756This program is free software; you can redistribute it and/or
757modify it under the same terms as Perl itself.
758