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