1 2package Compress::Raw::Zlib; 3 4require 5.006 ; 5require Exporter; 6use AutoLoader; 7use Carp ; 8 9use strict ; 10use warnings ; 11use bytes ; 12our ($VERSION, $XS_VERSION, @ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK, $AUTOLOAD, %DEFLATE_CONSTANTS, @DEFLATE_CONSTANTS ); 13 14$VERSION = '2.060'; 15$XS_VERSION = $VERSION; 16$VERSION = eval $VERSION; 17 18@ISA = qw(Exporter); 19%EXPORT_TAGS = ( flush => [qw{ 20 Z_NO_FLUSH 21 Z_PARTIAL_FLUSH 22 Z_SYNC_FLUSH 23 Z_FULL_FLUSH 24 Z_FINISH 25 Z_BLOCK 26 }], 27 level => [qw{ 28 Z_NO_COMPRESSION 29 Z_BEST_SPEED 30 Z_BEST_COMPRESSION 31 Z_DEFAULT_COMPRESSION 32 }], 33 strategy => [qw{ 34 Z_FILTERED 35 Z_HUFFMAN_ONLY 36 Z_RLE 37 Z_FIXED 38 Z_DEFAULT_STRATEGY 39 }], 40 status => [qw{ 41 Z_OK 42 Z_STREAM_END 43 Z_NEED_DICT 44 Z_ERRNO 45 Z_STREAM_ERROR 46 Z_DATA_ERROR 47 Z_MEM_ERROR 48 Z_BUF_ERROR 49 Z_VERSION_ERROR 50 }], 51 ); 52 53%DEFLATE_CONSTANTS = %EXPORT_TAGS; 54 55# Items to export into callers namespace by default. Note: do not export 56# names by default without a very good reason. Use EXPORT_OK instead. 57# Do not simply export all your public functions/methods/constants. 58@DEFLATE_CONSTANTS = 59@EXPORT = qw( 60 ZLIB_VERSION 61 ZLIB_VERNUM 62 63 64 OS_CODE 65 66 MAX_MEM_LEVEL 67 MAX_WBITS 68 69 Z_ASCII 70 Z_BEST_COMPRESSION 71 Z_BEST_SPEED 72 Z_BINARY 73 Z_BLOCK 74 Z_BUF_ERROR 75 Z_DATA_ERROR 76 Z_DEFAULT_COMPRESSION 77 Z_DEFAULT_STRATEGY 78 Z_DEFLATED 79 Z_ERRNO 80 Z_FILTERED 81 Z_FIXED 82 Z_FINISH 83 Z_FULL_FLUSH 84 Z_HUFFMAN_ONLY 85 Z_MEM_ERROR 86 Z_NEED_DICT 87 Z_NO_COMPRESSION 88 Z_NO_FLUSH 89 Z_NULL 90 Z_OK 91 Z_PARTIAL_FLUSH 92 Z_RLE 93 Z_STREAM_END 94 Z_STREAM_ERROR 95 Z_SYNC_FLUSH 96 Z_TREES 97 Z_UNKNOWN 98 Z_VERSION_ERROR 99 100 WANT_GZIP 101 WANT_GZIP_OR_ZLIB 102); 103 104push @EXPORT, qw(crc32 adler32 DEF_WBITS); 105 106use constant WANT_GZIP => 16; 107use constant WANT_GZIP_OR_ZLIB => 32; 108 109sub AUTOLOAD { 110 my($constname); 111 ($constname = $AUTOLOAD) =~ s/.*:://; 112 my ($error, $val) = constant($constname); 113 Carp::croak $error if $error; 114 no strict 'refs'; 115 *{$AUTOLOAD} = sub { $val }; 116 goto &{$AUTOLOAD}; 117} 118 119use constant FLAG_APPEND => 1 ; 120use constant FLAG_CRC => 2 ; 121use constant FLAG_ADLER => 4 ; 122use constant FLAG_CONSUME_INPUT => 8 ; 123use constant FLAG_LIMIT_OUTPUT => 16 ; 124 125eval { 126 require XSLoader; 127 XSLoader::load('Compress::Raw::Zlib', $XS_VERSION); 128 1; 129} 130or do { 131 require DynaLoader; 132 local @ISA = qw(DynaLoader); 133 bootstrap Compress::Raw::Zlib $XS_VERSION ; 134}; 135 136 137use constant Parse_any => 0x01; 138use constant Parse_unsigned => 0x02; 139use constant Parse_signed => 0x04; 140use constant Parse_boolean => 0x08; 141#use constant Parse_string => 0x10; 142#use constant Parse_custom => 0x12; 143 144#use constant Parse_store_ref => 0x100 ; 145 146use constant OFF_PARSED => 0 ; 147use constant OFF_TYPE => 1 ; 148use constant OFF_DEFAULT => 2 ; 149use constant OFF_FIXED => 3 ; 150use constant OFF_FIRST_ONLY => 4 ; 151use constant OFF_STICKY => 5 ; 152 153 154 155sub ParseParameters 156{ 157 my $level = shift || 0 ; 158 159 my $sub = (caller($level + 1))[3] ; 160 #local $Carp::CarpLevel = 1 ; 161 my $p = new Compress::Raw::Zlib::Parameters() ; 162 $p->parse(@_) 163 or croak "$sub: $p->{Error}" ; 164 165 return $p; 166} 167 168 169sub Compress::Raw::Zlib::Parameters::new 170{ 171 my $class = shift ; 172 173 my $obj = { Error => '', 174 Got => {}, 175 } ; 176 177 #return bless $obj, ref($class) || $class || __PACKAGE__ ; 178 return bless $obj, 'Compress::Raw::Zlib::Parameters' ; 179} 180 181sub Compress::Raw::Zlib::Parameters::setError 182{ 183 my $self = shift ; 184 my $error = shift ; 185 my $retval = @_ ? shift : undef ; 186 187 $self->{Error} = $error ; 188 return $retval; 189} 190 191#sub getError 192#{ 193# my $self = shift ; 194# return $self->{Error} ; 195#} 196 197sub Compress::Raw::Zlib::Parameters::parse 198{ 199 my $self = shift ; 200 201 my $default = shift ; 202 203 my $got = $self->{Got} ; 204 my $firstTime = keys %{ $got } == 0 ; 205 206 my (@Bad) ; 207 my @entered = () ; 208 209 # Allow the options to be passed as a hash reference or 210 # as the complete hash. 211 if (@_ == 0) { 212 @entered = () ; 213 } 214 elsif (@_ == 1) { 215 my $href = $_[0] ; 216 return $self->setError("Expected even number of parameters, got 1") 217 if ! defined $href or ! ref $href or ref $href ne "HASH" ; 218 219 foreach my $key (keys %$href) { 220 push @entered, $key ; 221 push @entered, \$href->{$key} ; 222 } 223 } 224 else { 225 my $count = @_; 226 return $self->setError("Expected even number of parameters, got $count") 227 if $count % 2 != 0 ; 228 229 for my $i (0.. $count / 2 - 1) { 230 push @entered, $_[2* $i] ; 231 push @entered, \$_[2* $i+1] ; 232 } 233 } 234 235 236 while (my ($key, $v) = each %$default) 237 { 238 croak "need 4 params [@$v]" 239 if @$v != 4 ; 240 241 my ($first_only, $sticky, $type, $value) = @$v ; 242 my $x ; 243 $self->_checkType($key, \$value, $type, 0, \$x) 244 or return undef ; 245 246 $key = lc $key; 247 248 if ($firstTime || ! $sticky) { 249 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ; 250 } 251 252 $got->{$key}[OFF_PARSED] = 0 ; 253 } 254 255 for my $i (0.. @entered / 2 - 1) { 256 my $key = $entered[2* $i] ; 257 my $value = $entered[2* $i+1] ; 258 259 #print "Key [$key] Value [$value]" ; 260 #print defined $$value ? "[$$value]\n" : "[undef]\n"; 261 262 $key =~ s/^-// ; 263 my $canonkey = lc $key; 264 265 if ($got->{$canonkey} && ($firstTime || 266 ! $got->{$canonkey}[OFF_FIRST_ONLY] )) 267 { 268 my $type = $got->{$canonkey}[OFF_TYPE] ; 269 my $s ; 270 $self->_checkType($key, $value, $type, 1, \$s) 271 or return undef ; 272 #$value = $$value unless $type & Parse_store_ref ; 273 $value = $$value ; 274 $got->{$canonkey} = [1, $type, $value, $s] ; 275 } 276 else 277 { push (@Bad, $key) } 278 } 279 280 if (@Bad) { 281 my ($bad) = join(", ", @Bad) ; 282 return $self->setError("unknown key value(s) @Bad") ; 283 } 284 285 return 1; 286} 287 288sub Compress::Raw::Zlib::Parameters::_checkType 289{ 290 my $self = shift ; 291 292 my $key = shift ; 293 my $value = shift ; 294 my $type = shift ; 295 my $validate = shift ; 296 my $output = shift; 297 298 #local $Carp::CarpLevel = $level ; 299 #print "PARSE $type $key $value $validate $sub\n" ; 300# if ( $type & Parse_store_ref) 301# { 302# #$value = $$value 303# # if ref ${ $value } ; 304# 305# $$output = $value ; 306# return 1; 307# } 308 309 $value = $$value ; 310 311 if ($type & Parse_any) 312 { 313 $$output = $value ; 314 return 1; 315 } 316 elsif ($type & Parse_unsigned) 317 { 318 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'") 319 if $validate && ! defined $value ; 320 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'") 321 if $validate && $value !~ /^\d+$/; 322 323 $$output = defined $value ? $value : 0 ; 324 return 1; 325 } 326 elsif ($type & Parse_signed) 327 { 328 return $self->setError("Parameter '$key' must be a signed int, got 'undef'") 329 if $validate && ! defined $value ; 330 return $self->setError("Parameter '$key' must be a signed int, got '$value'") 331 if $validate && $value !~ /^-?\d+$/; 332 333 $$output = defined $value ? $value : 0 ; 334 return 1 ; 335 } 336 elsif ($type & Parse_boolean) 337 { 338 return $self->setError("Parameter '$key' must be an int, got '$value'") 339 if $validate && defined $value && $value !~ /^\d*$/; 340 $$output = defined $value ? $value != 0 : 0 ; 341 return 1; 342 } 343# elsif ($type & Parse_string) 344# { 345# $$output = defined $value ? $value : "" ; 346# return 1; 347# } 348 349 $$output = $value ; 350 return 1; 351} 352 353 354 355sub Compress::Raw::Zlib::Parameters::parsed 356{ 357 my $self = shift ; 358 my $name = shift ; 359 360 return $self->{Got}{lc $name}[OFF_PARSED] ; 361} 362 363sub Compress::Raw::Zlib::Parameters::value 364{ 365 my $self = shift ; 366 my $name = shift ; 367 368 if (@_) 369 { 370 $self->{Got}{lc $name}[OFF_PARSED] = 1; 371 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ; 372 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ; 373 } 374 375 return $self->{Got}{lc $name}[OFF_FIXED] ; 376} 377 378our $OPTIONS_deflate = 379 { 380 'AppendOutput' => [1, 1, Parse_boolean, 0], 381 'CRC32' => [1, 1, Parse_boolean, 0], 382 'ADLER32' => [1, 1, Parse_boolean, 0], 383 'Bufsize' => [1, 1, Parse_unsigned, 4096], 384 385 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()], 386 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()], 387 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()], 388 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()], 389 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()], 390 'Dictionary' => [1, 1, Parse_any, ""], 391 }; 392 393sub Compress::Raw::Zlib::Deflate::new 394{ 395 my $pkg = shift ; 396 my ($got) = ParseParameters(0, $OPTIONS_deflate, @_); 397 398 croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " . 399 $got->value('Bufsize') 400 unless $got->value('Bufsize') >= 1; 401 402 my $flags = 0 ; 403 $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 404 $flags |= FLAG_CRC if $got->value('CRC32') ; 405 $flags |= FLAG_ADLER if $got->value('ADLER32') ; 406 407 my $windowBits = $got->value('WindowBits'); 408 $windowBits += MAX_WBITS() 409 if ($windowBits & MAX_WBITS()) == 0 ; 410 411 _deflateInit($flags, 412 $got->value('Level'), 413 $got->value('Method'), 414 $windowBits, 415 $got->value('MemLevel'), 416 $got->value('Strategy'), 417 $got->value('Bufsize'), 418 $got->value('Dictionary')) ; 419 420} 421 422sub Compress::Raw::Zlib::deflateStream::STORABLE_freeze 423{ 424 my $type = ref shift; 425 croak "Cannot freeze $type object\n"; 426} 427 428sub Compress::Raw::Zlib::deflateStream::STORABLE_thaw 429{ 430 my $type = ref shift; 431 croak "Cannot thaw $type object\n"; 432} 433 434 435our $OPTIONS_inflate = 436 { 437 'AppendOutput' => [1, 1, Parse_boolean, 0], 438 'LimitOutput' => [1, 1, Parse_boolean, 0], 439 'CRC32' => [1, 1, Parse_boolean, 0], 440 'ADLER32' => [1, 1, Parse_boolean, 0], 441 'ConsumeInput' => [1, 1, Parse_boolean, 1], 442 'Bufsize' => [1, 1, Parse_unsigned, 4096], 443 444 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()], 445 'Dictionary' => [1, 1, Parse_any, ""], 446 } ; 447 448sub Compress::Raw::Zlib::Inflate::new 449{ 450 my $pkg = shift ; 451 my ($got) = ParseParameters(0, $OPTIONS_inflate, @_); 452 453 croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " . 454 $got->value('Bufsize') 455 unless $got->value('Bufsize') >= 1; 456 457 my $flags = 0 ; 458 $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 459 $flags |= FLAG_CRC if $got->value('CRC32') ; 460 $flags |= FLAG_ADLER if $got->value('ADLER32') ; 461 $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ; 462 $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ; 463 464 465 my $windowBits = $got->value('WindowBits'); 466 $windowBits += MAX_WBITS() 467 if ($windowBits & MAX_WBITS()) == 0 ; 468 469 _inflateInit($flags, $windowBits, $got->value('Bufsize'), 470 $got->value('Dictionary')) ; 471} 472 473sub Compress::Raw::Zlib::inflateStream::STORABLE_freeze 474{ 475 my $type = ref shift; 476 croak "Cannot freeze $type object\n"; 477} 478 479sub Compress::Raw::Zlib::inflateStream::STORABLE_thaw 480{ 481 my $type = ref shift; 482 croak "Cannot thaw $type object\n"; 483} 484 485sub Compress::Raw::Zlib::InflateScan::new 486{ 487 my $pkg = shift ; 488 my ($got) = ParseParameters(0, 489 { 490 'CRC32' => [1, 1, Parse_boolean, 0], 491 'ADLER32' => [1, 1, Parse_boolean, 0], 492 'Bufsize' => [1, 1, Parse_unsigned, 4096], 493 494 'WindowBits' => [1, 1, Parse_signed, -MAX_WBITS()], 495 'Dictionary' => [1, 1, Parse_any, ""], 496 }, @_) ; 497 498 499 croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " . 500 $got->value('Bufsize') 501 unless $got->value('Bufsize') >= 1; 502 503 my $flags = 0 ; 504 #$flags |= FLAG_APPEND if $got->value('AppendOutput') ; 505 $flags |= FLAG_CRC if $got->value('CRC32') ; 506 $flags |= FLAG_ADLER if $got->value('ADLER32') ; 507 #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ; 508 509 _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'), 510 '') ; 511} 512 513sub Compress::Raw::Zlib::inflateScanStream::createDeflateStream 514{ 515 my $pkg = shift ; 516 my ($got) = ParseParameters(0, 517 { 518 'AppendOutput' => [1, 1, Parse_boolean, 0], 519 'CRC32' => [1, 1, Parse_boolean, 0], 520 'ADLER32' => [1, 1, Parse_boolean, 0], 521 'Bufsize' => [1, 1, Parse_unsigned, 4096], 522 523 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()], 524 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()], 525 'WindowBits' => [1, 1, Parse_signed, - MAX_WBITS()], 526 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()], 527 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()], 528 }, @_) ; 529 530 croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " . 531 $got->value('Bufsize') 532 unless $got->value('Bufsize') >= 1; 533 534 my $flags = 0 ; 535 $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 536 $flags |= FLAG_CRC if $got->value('CRC32') ; 537 $flags |= FLAG_ADLER if $got->value('ADLER32') ; 538 539 $pkg->_createDeflateStream($flags, 540 $got->value('Level'), 541 $got->value('Method'), 542 $got->value('WindowBits'), 543 $got->value('MemLevel'), 544 $got->value('Strategy'), 545 $got->value('Bufsize'), 546 ) ; 547 548} 549 550sub Compress::Raw::Zlib::inflateScanStream::inflate 551{ 552 my $self = shift ; 553 my $buffer = $_[1]; 554 my $eof = $_[2]; 555 556 my $status = $self->scan(@_); 557 558 if ($status == Z_OK() && $_[2]) { 559 my $byte = ' '; 560 561 $status = $self->scan(\$byte, $_[1]) ; 562 } 563 564 return $status ; 565} 566 567sub Compress::Raw::Zlib::deflateStream::deflateParams 568{ 569 my $self = shift ; 570 my ($got) = ParseParameters(0, { 571 'Level' => [1, 1, Parse_signed, undef], 572 'Strategy' => [1, 1, Parse_unsigned, undef], 573 'Bufsize' => [1, 1, Parse_unsigned, undef], 574 }, 575 @_) ; 576 577 croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy" 578 unless $got->parsed('Level') + $got->parsed('Strategy') + 579 $got->parsed('Bufsize'); 580 581 croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " . 582 $got->value('Bufsize') 583 if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1; 584 585 my $flags = 0; 586 $flags |= 1 if $got->parsed('Level') ; 587 $flags |= 2 if $got->parsed('Strategy') ; 588 $flags |= 4 if $got->parsed('Bufsize') ; 589 590 $self->_deflateParams($flags, $got->value('Level'), 591 $got->value('Strategy'), $got->value('Bufsize')); 592 593} 594 595 596# Autoload methods go after __END__, and are processed by the autosplit program. 597 5981; 599__END__ 600 601 602=head1 NAME 603 604Compress::Raw::Zlib - Low-Level Interface to zlib compression library 605 606=head1 SYNOPSIS 607 608 use Compress::Raw::Zlib ; 609 610 ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ; 611 $status = $d->deflate($input, $output) ; 612 $status = $d->flush($output [, $flush_type]) ; 613 $d->deflateReset() ; 614 $d->deflateParams(OPTS) ; 615 $d->deflateTune(OPTS) ; 616 $d->dict_adler() ; 617 $d->crc32() ; 618 $d->adler32() ; 619 $d->total_in() ; 620 $d->total_out() ; 621 $d->msg() ; 622 $d->get_Strategy(); 623 $d->get_Level(); 624 $d->get_BufSize(); 625 626 ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ; 627 $status = $i->inflate($input, $output [, $eof]) ; 628 $status = $i->inflateSync($input) ; 629 $i->inflateReset() ; 630 $i->dict_adler() ; 631 $d->crc32() ; 632 $d->adler32() ; 633 $i->total_in() ; 634 $i->total_out() ; 635 $i->msg() ; 636 $d->get_BufSize(); 637 638 $crc = adler32($buffer [,$crc]) ; 639 $crc = crc32($buffer [,$crc]) ; 640 641 $crc = adler32_combine($crc1, $crc2, $len2)l 642 $crc = crc32_combine($adler1, $adler2, $len2) 643 644 my $version = Compress::Raw::Zlib::zlib_version(); 645 my $flags = Compress::Raw::Zlib::zlibCompileFlags(); 646 647=head1 DESCRIPTION 648 649The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib> 650compression library (see L</AUTHOR> for details about where to get 651I<zlib>). 652 653=head1 Compress::Raw::Zlib::Deflate 654 655This section defines an interface that allows in-memory compression using 656the I<deflate> interface provided by zlib. 657 658Here is a definition of the interface available: 659 660=head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) > 661 662Initialises a deflation object. 663 664If you are familiar with the I<zlib> library, it combines the 665features of the I<zlib> functions C<deflateInit>, C<deflateInit2> 666and C<deflateSetDictionary>. 667 668If successful, it will return the initialised deflation object, C<$d> 669and a C<$status> of C<Z_OK> in a list context. In scalar context it 670returns the deflation object, C<$d>, only. 671 672If not successful, the returned deflation object, C<$d>, will be 673I<undef> and C<$status> will hold the a I<zlib> error code. 674 675The function optionally takes a number of named options specified as 676C<< Name => value >> pairs. This allows individual options to be 677tailored without having to specify them all in the parameter list. 678 679For backward compatibility, it is also possible to pass the parameters 680as a reference to a hash containing the name=>value pairs. 681 682Below is a list of the valid options: 683 684=over 5 685 686=item B<-Level> 687 688Defines the compression level. Valid values are 0 through 9, 689C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 690C<Z_DEFAULT_COMPRESSION>. 691 692The default is C<Z_DEFAULT_COMPRESSION>. 693 694=item B<-Method> 695 696Defines the compression method. The only valid value at present (and 697the default) is C<Z_DEFLATED>. 698 699=item B<-WindowBits> 700 701To compress an RFC 1950 data stream, set C<WindowBits> to a positive 702number between 8 and 15. 703 704To compress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 705 706To compress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to 707C<WANT_GZIP>. 708 709For a definition of the meaning and valid values for C<WindowBits> 710refer to the I<zlib> documentation for I<deflateInit2>. 711 712Defaults to C<MAX_WBITS>. 713 714=item B<-MemLevel> 715 716For a definition of the meaning and valid values for C<MemLevel> 717refer to the I<zlib> documentation for I<deflateInit2>. 718 719Defaults to MAX_MEM_LEVEL. 720 721=item B<-Strategy> 722 723Defines the strategy used to tune the compression. The valid values are 724C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and 725C<Z_HUFFMAN_ONLY>. 726 727The default is C<Z_DEFAULT_STRATEGY>. 728 729=item B<-Dictionary> 730 731When a dictionary is specified I<Compress::Raw::Zlib> will automatically 732call C<deflateSetDictionary> directly after calling C<deflateInit>. The 733Adler32 value for the dictionary can be obtained by calling the method 734C<$d-E<gt>dict_adler()>. 735 736The default is no dictionary. 737 738=item B<-Bufsize> 739 740Sets the initial size for the output buffer used by the C<$d-E<gt>deflate> 741and C<$d-E<gt>flush> methods. If the buffer has to be 742reallocated to increase the size, it will grow in increments of 743C<Bufsize>. 744 745The default buffer size is 4096. 746 747=item B<-AppendOutput> 748 749This option controls how data is written to the output buffer by the 750C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods. 751 752If the C<AppendOutput> option is set to false, the output buffers in the 753C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before 754uncompressed data is written to them. 755 756If the option is set to true, uncompressed data will be appended to the 757output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods. 758 759This option defaults to false. 760 761=item B<-CRC32> 762 763If set to true, a crc32 checksum of the uncompressed data will be 764calculated. Use the C<$d-E<gt>crc32> method to retrieve this value. 765 766This option defaults to false. 767 768=item B<-ADLER32> 769 770If set to true, an adler32 checksum of the uncompressed data will be 771calculated. Use the C<$d-E<gt>adler32> method to retrieve this value. 772 773This option defaults to false. 774 775=back 776 777Here is an example of using the C<Compress::Raw::Zlib::Deflate> optional 778parameter list to override the default buffer size and compression 779level. All other options will take their default values. 780 781 my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300, 782 -Level => Z_BEST_SPEED ) ; 783 784=head2 B<$status = $d-E<gt>deflate($input, $output)> 785 786Deflates the contents of C<$input> and writes the compressed data to 787C<$output>. 788 789The C<$input> and C<$output> parameters can be either scalars or scalar 790references. 791 792When finished, C<$input> will be completely processed (assuming there 793were no errors). If the deflation was successful it writes the deflated 794data to C<$output> and returns a status value of C<Z_OK>. 795 796On error, it returns a I<zlib> error code. 797 798If the C<AppendOutput> option is set to true in the constructor for 799the C<$d> object, the compressed data will be appended to C<$output>. If 800it is false, C<$output> will be truncated before any compressed data is 801written to it. 802 803B<Note>: This method will not necessarily write compressed data to 804C<$output> every time it is called. So do not assume that there has been 805an error if the contents of C<$output> is empty on returning from 806this method. As long as the return code from the method is C<Z_OK>, 807the deflate has succeeded. 808 809=head2 B<$status = $d-E<gt>flush($output [, $flush_type]) > 810 811Typically used to finish the deflation. Any pending output will be 812written to C<$output>. 813 814Returns C<Z_OK> if successful. 815 816Note that flushing can seriously degrade the compression ratio, so it 817should only be used to terminate a decompression (using C<Z_FINISH>) or 818when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>). 819 820By default the C<flush_type> used is C<Z_FINISH>. Other valid values 821for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH> 822and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the 823C<flush_type> parameter if you fully understand the implications of 824what it does. See the C<zlib> documentation for details. 825 826If the C<AppendOutput> option is set to true in the constructor for 827the C<$d> object, the compressed data will be appended to C<$output>. If 828it is false, C<$output> will be truncated before any compressed data is 829written to it. 830 831=head2 B<$status = $d-E<gt>deflateReset() > 832 833This method will reset the deflation object C<$d>. It can be used when you 834are compressing multiple data streams and want to use the same object to 835compress each of them. It should only be used once the previous data stream 836has been flushed successfully, i.e. a call to C<< $d->flush(Z_FINISH) >> has 837returned C<Z_OK>. 838 839Returns C<Z_OK> if successful. 840 841=head2 B<$status = $d-E<gt>deflateParams([OPT])> 842 843Change settings for the deflate object C<$d>. 844 845The list of the valid options is shown below. Options not specified 846will remain unchanged. 847 848=over 5 849 850=item B<-Level> 851 852Defines the compression level. Valid values are 0 through 9, 853C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 854C<Z_DEFAULT_COMPRESSION>. 855 856=item B<-Strategy> 857 858Defines the strategy used to tune the compression. The valid values are 859C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 860 861=item B<-BufSize> 862 863Sets the initial size for the output buffer used by the C<$d-E<gt>deflate> 864and C<$d-E<gt>flush> methods. If the buffer has to be 865reallocated to increase the size, it will grow in increments of 866C<Bufsize>. 867 868=back 869 870=head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)> 871 872Tune the internal settings for the deflate object C<$d>. This option is 873only available if you are running zlib 1.2.2.3 or better. 874 875Refer to the documentation in zlib.h for instructions on how to fly 876C<deflateTune>. 877 878=head2 B<$d-E<gt>dict_adler()> 879 880Returns the adler32 value for the dictionary. 881 882=head2 B<$d-E<gt>crc32()> 883 884Returns the crc32 value for the uncompressed data to date. 885 886If the C<CRC32> option is not enabled in the constructor for this object, 887this method will always return 0; 888 889=head2 B<$d-E<gt>adler32()> 890 891Returns the adler32 value for the uncompressed data to date. 892 893=head2 B<$d-E<gt>msg()> 894 895Returns the last error message generated by zlib. 896 897=head2 B<$d-E<gt>total_in()> 898 899Returns the total number of bytes uncompressed bytes input to deflate. 900 901=head2 B<$d-E<gt>total_out()> 902 903Returns the total number of compressed bytes output from deflate. 904 905=head2 B<$d-E<gt>get_Strategy()> 906 907Returns the deflation strategy currently used. Valid values are 908C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 909 910=head2 B<$d-E<gt>get_Level()> 911 912Returns the compression level being used. 913 914=head2 B<$d-E<gt>get_BufSize()> 915 916Returns the buffer size used to carry out the compression. 917 918=head2 Example 919 920Here is a trivial example of using C<deflate>. It simply reads standard 921input, deflates it and writes it to standard output. 922 923 use strict ; 924 use warnings ; 925 926 use Compress::Raw::Zlib ; 927 928 binmode STDIN; 929 binmode STDOUT; 930 my $x = new Compress::Raw::Zlib::Deflate 931 or die "Cannot create a deflation stream\n" ; 932 933 my ($output, $status) ; 934 while (<>) 935 { 936 $status = $x->deflate($_, $output) ; 937 938 $status == Z_OK 939 or die "deflation failed\n" ; 940 941 print $output ; 942 } 943 944 $status = $x->flush($output) ; 945 946 $status == Z_OK 947 or die "deflation failed\n" ; 948 949 print $output ; 950 951=head1 Compress::Raw::Zlib::Inflate 952 953This section defines an interface that allows in-memory uncompression using 954the I<inflate> interface provided by zlib. 955 956Here is a definition of the interface: 957 958=head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) > 959 960Initialises an inflation object. 961 962In a list context it returns the inflation object, C<$i>, and the 963I<zlib> status code (C<$status>). In a scalar context it returns the 964inflation object only. 965 966If successful, C<$i> will hold the inflation object and C<$status> will 967be C<Z_OK>. 968 969If not successful, C<$i> will be I<undef> and C<$status> will hold the 970I<zlib> error code. 971 972The function optionally takes a number of named options specified as 973C<< -Name => value >> pairs. This allows individual options to be 974tailored without having to specify them all in the parameter list. 975 976For backward compatibility, it is also possible to pass the parameters 977as a reference to a hash containing the C<< name=>value >> pairs. 978 979Here is a list of the valid options: 980 981=over 5 982 983=item B<-WindowBits> 984 985To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive 986number between 8 and 15. 987 988To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 989 990To uncompress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to 991C<WANT_GZIP>. 992 993To auto-detect and uncompress an RFC 1950 or RFC 1952 data stream (i.e. 994gzip), set C<WindowBits> to C<WANT_GZIP_OR_ZLIB>. 995 996For a full definition of the meaning and valid values for C<WindowBits> 997refer to the I<zlib> documentation for I<inflateInit2>. 998 999Defaults to C<MAX_WBITS>. 1000 1001=item B<-Bufsize> 1002 1003Sets the initial size for the output buffer used by the C<$i-E<gt>inflate> 1004method. If the output buffer in this method has to be reallocated to 1005increase the size, it will grow in increments of C<Bufsize>. 1006 1007Default is 4096. 1008 1009=item B<-Dictionary> 1010 1011The default is no dictionary. 1012 1013=item B<-AppendOutput> 1014 1015This option controls how data is written to the output buffer by the 1016C<$i-E<gt>inflate> method. 1017 1018If the option is set to false, the output buffer in the C<$i-E<gt>inflate> 1019method will be truncated before uncompressed data is written to it. 1020 1021If the option is set to true, uncompressed data will be appended to the 1022output buffer by the C<$i-E<gt>inflate> method. 1023 1024This option defaults to false. 1025 1026=item B<-CRC32> 1027 1028If set to true, a crc32 checksum of the uncompressed data will be 1029calculated. Use the C<$i-E<gt>crc32> method to retrieve this value. 1030 1031This option defaults to false. 1032 1033=item B<-ADLER32> 1034 1035If set to true, an adler32 checksum of the uncompressed data will be 1036calculated. Use the C<$i-E<gt>adler32> method to retrieve this value. 1037 1038This option defaults to false. 1039 1040=item B<-ConsumeInput> 1041 1042If set to true, this option will remove compressed data from the input 1043buffer of the C<< $i->inflate >> method as the inflate progresses. 1044 1045This option can be useful when you are processing compressed data that is 1046embedded in another file/buffer. In this case the data that immediately 1047follows the compressed stream will be left in the input buffer. 1048 1049This option defaults to true. 1050 1051=item B<-LimitOutput> 1052 1053The C<LimitOutput> option changes the behavior of the C<< $i->inflate >> 1054method so that the amount of memory used by the output buffer can be 1055limited. 1056 1057When C<LimitOutput> is used the size of the output buffer used will either 1058be the value of the C<Bufsize> option or the amount of memory already 1059allocated to C<$output>, whichever is larger. Predicting the output size 1060available is tricky, so don't rely on getting an exact output buffer size. 1061 1062When C<LimitOutout> is not specified C<< $i->inflate >> will use as much 1063memory as it takes to write all the uncompressed data it creates by 1064uncompressing the input buffer. 1065 1066If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be 1067enabled. 1068 1069This option defaults to false. 1070 1071See L</The LimitOutput option> for a discussion on why C<LimitOutput> is 1072needed and how to use it. 1073 1074=back 1075 1076Here is an example of using an optional parameter to override the default 1077buffer size. 1078 1079 my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ; 1080 1081=head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) > 1082 1083Inflates the complete contents of C<$input> and writes the uncompressed 1084data to C<$output>. The C<$input> and C<$output> parameters can either be 1085scalars or scalar references. 1086 1087Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the 1088compressed data has been successfully reached. 1089 1090If not successful C<$status> will hold the I<zlib> error code. 1091 1092If the C<ConsumeInput> option has been set to true when the 1093C<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter 1094is modified by C<inflate>. On completion it will contain what remains 1095of the input buffer after inflation. In practice, this means that when 1096the return status is C<Z_OK> the C<$input> parameter will contain an 1097empty string, and when the return status is C<Z_STREAM_END> the C<$input> 1098parameter will contains what (if anything) was stored in the input buffer 1099after the deflated data stream. 1100 1101This feature is useful when processing a file format that encapsulates 1102a compressed data stream (e.g. gzip, zip) and there is useful data 1103immediately after the deflation stream. 1104 1105If the C<AppendOutput> option is set to true in the constructor for 1106this object, the uncompressed data will be appended to C<$output>. If 1107it is false, C<$output> will be truncated before any uncompressed data 1108is written to it. 1109 1110The C<$eof> parameter needs a bit of explanation. 1111 1112Prior to version 1.2.0, zlib assumed that there was at least one trailing 1113byte immediately after the compressed data stream when it was carrying out 1114decompression. This normally isn't a problem because the majority of zlib 1115applications guarantee that there will be data directly after the 1116compressed data stream. For example, both gzip (RFC 1950) and zip both 1117define trailing data that follows the compressed data stream. 1118 1119The C<$eof> parameter only needs to be used if B<all> of the following 1120conditions apply 1121 1122=over 5 1123 1124=item 1 1125 1126You are either using a copy of zlib that is older than version 1.2.0 or you 1127want your application code to be able to run with as many different 1128versions of zlib as possible. 1129 1130=item 2 1131 1132You have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor 1133for this object, i.e. you are uncompressing a raw deflated data stream 1134(RFC 1951). 1135 1136=item 3 1137 1138There is no data immediately after the compressed data stream. 1139 1140=back 1141 1142If B<all> of these are the case, then you need to set the C<$eof> parameter 1143to true on the final call (and only the final call) to C<$i-E<gt>inflate>. 1144 1145If you have built this module with zlib >= 1.2.0, the C<$eof> parameter is 1146ignored. You can still set it if you want, but it won't be used behind the 1147scenes. 1148 1149=head2 B<$status = $i-E<gt>inflateSync($input)> 1150 1151This method can be used to attempt to recover good data from a compressed 1152data stream that is partially corrupt. 1153It scans C<$input> until it reaches either a I<full flush point> or the 1154end of the buffer. 1155 1156If a I<full flush point> is found, C<Z_OK> is returned and C<$input> 1157will be have all data up to the flush point removed. This data can then be 1158passed to the C<$i-E<gt>inflate> method to be uncompressed. 1159 1160Any other return code means that a flush point was not found. If more 1161data is available, C<inflateSync> can be called repeatedly with more 1162compressed data until the flush point is found. 1163 1164Note I<full flush points> are not present by default in compressed 1165data streams. They must have been added explicitly when the data stream 1166was created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>. 1167 1168=head2 B<$status = $i-E<gt>inflateReset() > 1169 1170This method will reset the inflation object C<$i>. It can be used when you 1171are uncompressing multiple data streams and want to use the same object to 1172uncompress each of them. 1173 1174Returns C<Z_OK> if successful. 1175 1176=head2 B<$i-E<gt>dict_adler()> 1177 1178Returns the adler32 value for the dictionary. 1179 1180=head2 B<$i-E<gt>crc32()> 1181 1182Returns the crc32 value for the uncompressed data to date. 1183 1184If the C<CRC32> option is not enabled in the constructor for this object, 1185this method will always return 0; 1186 1187=head2 B<$i-E<gt>adler32()> 1188 1189Returns the adler32 value for the uncompressed data to date. 1190 1191If the C<ADLER32> option is not enabled in the constructor for this object, 1192this method will always return 0; 1193 1194=head2 B<$i-E<gt>msg()> 1195 1196Returns the last error message generated by zlib. 1197 1198=head2 B<$i-E<gt>total_in()> 1199 1200Returns the total number of bytes compressed bytes input to inflate. 1201 1202=head2 B<$i-E<gt>total_out()> 1203 1204Returns the total number of uncompressed bytes output from inflate. 1205 1206=head2 B<$d-E<gt>get_BufSize()> 1207 1208Returns the buffer size used to carry out the decompression. 1209 1210=head2 Examples 1211 1212Here is an example of using C<inflate>. 1213 1214 use strict ; 1215 use warnings ; 1216 1217 use Compress::Raw::Zlib; 1218 1219 my $x = new Compress::Raw::Zlib::Inflate() 1220 or die "Cannot create a inflation stream\n" ; 1221 1222 my $input = '' ; 1223 binmode STDIN; 1224 binmode STDOUT; 1225 1226 my ($output, $status) ; 1227 while (read(STDIN, $input, 4096)) 1228 { 1229 $status = $x->inflate($input, $output) ; 1230 1231 print $output ; 1232 1233 last if $status != Z_OK ; 1234 } 1235 1236 die "inflation failed\n" 1237 unless $status == Z_STREAM_END ; 1238 1239The next example show how to use the C<LimitOutput> option. Notice the use 1240of two nested loops in this case. The outer loop reads the data from the 1241input source - STDIN and the inner loop repeatedly calls C<inflate> until 1242C<$input> is exhausted, we get an error, or the end of the stream is 1243reached. One point worth remembering is by using the C<LimitOutput> option 1244you also get C<ConsumeInput> set as well - this makes the code below much 1245simpler. 1246 1247 use strict ; 1248 use warnings ; 1249 1250 use Compress::Raw::Zlib; 1251 1252 my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1) 1253 or die "Cannot create a inflation stream\n" ; 1254 1255 my $input = '' ; 1256 binmode STDIN; 1257 binmode STDOUT; 1258 1259 my ($output, $status) ; 1260 1261 OUTER: 1262 while (read(STDIN, $input, 4096)) 1263 { 1264 do 1265 { 1266 $status = $x->inflate($input, $output) ; 1267 1268 print $output ; 1269 1270 last OUTER 1271 unless $status == Z_OK || $status == Z_BUF_ERROR ; 1272 } 1273 while ($status == Z_OK && length $input); 1274 } 1275 1276 die "inflation failed\n" 1277 unless $status == Z_STREAM_END ; 1278 1279=head1 CHECKSUM FUNCTIONS 1280 1281Two functions are provided by I<zlib> to calculate checksums. For the 1282Perl interface, the order of the two parameters in both functions has 1283been reversed. This allows both running checksums and one off 1284calculations to be done. 1285 1286 $crc = adler32($buffer [,$crc]) ; 1287 $crc = crc32($buffer [,$crc]) ; 1288 1289The buffer parameters can either be a scalar or a scalar reference. 1290 1291If the $crc parameters is C<undef>, the crc value will be reset. 1292 1293If you have built this module with zlib 1.2.3 or better, two more 1294CRC-related functions are available. 1295 1296 $crc = adler32_combine($crc1, $crc2, $len2)l 1297 $crc = crc32_combine($adler1, $adler2, $len2) 1298 1299These functions allow checksums to be merged. 1300 1301=head1 Misc 1302 1303=head2 my $version = Compress::Raw::Zlib::zlib_version(); 1304 1305Returns the version of the zlib library. 1306 1307=head2 my $flags = Compress::Raw::Zlib::zlibCompileFlags(); 1308 1309Returns the flags indicating compile-time options that were used to build 1310the zlib library. See the zlib documentation for a description of the flags 1311returned by C<zlibCompileFlags>. 1312 1313Note that when the zlib sources are built along with this module the 1314C<sprintf> flags (bits 24, 25 and 26) should be ignored. 1315 1316If you are using zlib 1.2.0 or older, C<zlibCompileFlags> will return 0. 1317 1318=head1 The LimitOutput option. 1319 1320By default C<< $i->inflate($input, $output) >> will uncompress I<all> data 1321in C<$input> and write I<all> of the uncompressed data it has generated to 1322C<$output>. This makes the interface to C<inflate> much simpler - if the 1323method has uncompressed C<$input> successfully I<all> compressed data in 1324C<$input> will have been dealt with. So if you are reading from an input 1325source and uncompressing as you go the code will look something like this 1326 1327 use strict ; 1328 use warnings ; 1329 1330 use Compress::Raw::Zlib; 1331 1332 my $x = new Compress::Raw::Zlib::Inflate() 1333 or die "Cannot create a inflation stream\n" ; 1334 1335 my $input = '' ; 1336 1337 my ($output, $status) ; 1338 while (read(STDIN, $input, 4096)) 1339 { 1340 $status = $x->inflate($input, $output) ; 1341 1342 print $output ; 1343 1344 last if $status != Z_OK ; 1345 } 1346 1347 die "inflation failed\n" 1348 unless $status == Z_STREAM_END ; 1349 1350The points to note are 1351 1352=over 5 1353 1354=item * 1355 1356The main processing loop in the code handles reading of compressed data 1357from STDIN. 1358 1359=item * 1360 1361The status code returned from C<inflate> will only trigger termination of 1362the main processing loop if it isn't C<Z_OK>. When C<LimitOutput> has not 1363been used the C<Z_OK> status means means that the end of the compressed 1364data stream has been reached or there has been an error in uncompression. 1365 1366=item * 1367 1368After the call to C<inflate> I<all> of the uncompressed data in C<$input> 1369will have been processed. This means the subsequent call to C<read> can 1370overwrite it's contents without any problem. 1371 1372=back 1373 1374For most use-cases the behavior described above is acceptable (this module 1375and it's predecessor, C<Compress::Zlib>, have used it for over 10 years 1376without an issue), but in a few very specific use-cases the amount of 1377memory required for C<$output> can prohibitively large. For example, if the 1378compressed data stream contains the same pattern repeated thousands of 1379times, a relatively small compressed data stream can uncompress into 1380hundreds of megabytes. Remember C<inflate> will keep allocating memory 1381until I<all> the uncompressed data has been written to the output buffer - 1382the size of C<$output> is unbounded. 1383 1384The C<LimitOutput> option is designed to help with this use-case. 1385 1386The main difference in your code when using C<LimitOutput> is having to 1387deal with cases where the C<$input> parameter still contains some 1388uncompressed data that C<inflate> hasn't processed yet. The status code 1389returned from C<inflate> will be C<Z_OK> if uncompression took place and 1390C<Z_BUF_ERROR> if the output buffer is full. 1391 1392Below is typical code that shows how to use C<LimitOutput>. 1393 1394 use strict ; 1395 use warnings ; 1396 1397 use Compress::Raw::Zlib; 1398 1399 my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1) 1400 or die "Cannot create a inflation stream\n" ; 1401 1402 my $input = '' ; 1403 binmode STDIN; 1404 binmode STDOUT; 1405 1406 my ($output, $status) ; 1407 1408 OUTER: 1409 while (read(STDIN, $input, 4096)) 1410 { 1411 do 1412 { 1413 $status = $x->inflate($input, $output) ; 1414 1415 print $output ; 1416 1417 last OUTER 1418 unless $status == Z_OK || $status == Z_BUF_ERROR ; 1419 } 1420 while ($status == Z_OK && length $input); 1421 } 1422 1423 die "inflation failed\n" 1424 unless $status == Z_STREAM_END ; 1425 1426Points to note this time: 1427 1428=over 5 1429 1430=item * 1431 1432There are now two nested loops in the code: the outer loop for reading the 1433compressed data from STDIN, as before; and the inner loop to carry out the 1434uncompression. 1435 1436=item * 1437 1438There are two exit points from the inner uncompression loop. 1439 1440Firstly when C<inflate> has returned a status other than C<Z_OK> or 1441C<Z_BUF_ERROR>. This means that either the end of the compressed data 1442stream has been reached (C<Z_STREAM_END>) or there is an error in the 1443compressed data. In either of these cases there is no point in continuing 1444with reading the compressed data, so both loops are terminated. 1445 1446The second exit point tests if there is any data left in the input buffer, 1447C<$input> - remember that the C<ConsumeInput> option is automatically 1448enabled when C<LimitOutput> is used. When the input buffer has been 1449exhausted, the outer loop can run again and overwrite a now empty 1450C<$input>. 1451 1452=back 1453 1454=head1 ACCESSING ZIP FILES 1455 1456Although it is possible (with some effort on your part) to use this module 1457to access .zip files, there are other perl modules available that will do 1458all the hard work for you. Check out C<Archive::Zip>, 1459C<Archive::Zip::SimpleZip>, C<IO::Compress::Zip> and 1460C<IO::Uncompress::Unzip>. 1461 1462=head1 FAQ 1463 1464=head2 Compatibility with Unix compress/uncompress. 1465 1466This module is not compatible with Unix C<compress>. 1467 1468If you have the C<uncompress> program available, you can use this to read 1469compressed files 1470 1471 open F, "uncompress -c $filename |"; 1472 while (<F>) 1473 { 1474 ... 1475 1476Alternatively, if you have the C<gunzip> program available, you can use 1477this to read compressed files 1478 1479 open F, "gunzip -c $filename |"; 1480 while (<F>) 1481 { 1482 ... 1483 1484and this to write compress files, if you have the C<compress> program 1485available 1486 1487 open F, "| compress -c $filename "; 1488 print F "data"; 1489 ... 1490 close F ; 1491 1492=head2 Accessing .tar.Z files 1493 1494See previous FAQ item. 1495 1496If the C<Archive::Tar> module is installed and either the C<uncompress> or 1497C<gunzip> programs are available, you can use one of these workarounds to 1498read C<.tar.Z> files. 1499 1500Firstly with C<uncompress> 1501 1502 use strict; 1503 use warnings; 1504 use Archive::Tar; 1505 1506 open F, "uncompress -c $filename |"; 1507 my $tar = Archive::Tar->new(*F); 1508 ... 1509 1510and this with C<gunzip> 1511 1512 use strict; 1513 use warnings; 1514 use Archive::Tar; 1515 1516 open F, "gunzip -c $filename |"; 1517 my $tar = Archive::Tar->new(*F); 1518 ... 1519 1520Similarly, if the C<compress> program is available, you can use this to 1521write a C<.tar.Z> file 1522 1523 use strict; 1524 use warnings; 1525 use Archive::Tar; 1526 use IO::File; 1527 1528 my $fh = new IO::File "| compress -c >$filename"; 1529 my $tar = Archive::Tar->new(); 1530 ... 1531 $tar->write($fh); 1532 $fh->close ; 1533 1534=head2 Zlib Library Version Support 1535 1536By default C<Compress::Raw::Zlib> will build with a private copy of version 15371.2.5 of the zlib library. (See the F<README> file for details of 1538how to override this behaviour) 1539 1540If you decide to use a different version of the zlib library, you need to be 1541aware of the following issues 1542 1543=over 5 1544 1545=item * 1546 1547First off, you must have zlib 1.0.5 or better. 1548 1549=item * 1550 1551You need to have zlib 1.2.1 or better if you want to use the C<-Merge> 1552option with C<IO::Compress::Gzip>, C<IO::Compress::Deflate> and 1553C<IO::Compress::RawDeflate>. 1554 1555=back 1556 1557=head1 CONSTANTS 1558 1559All the I<zlib> constants are automatically imported when you make use 1560of I<Compress::Raw::Zlib>. 1561 1562=head1 SEE ALSO 1563 1564L<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::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> 1565 1566L<IO::Compress::FAQ|IO::Compress::FAQ> 1567 1568L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1569L<Archive::Tar|Archive::Tar>, 1570L<IO::Zlib|IO::Zlib> 1571 1572For RFC 1950, 1951 and 1952 see 1573F<http://www.faqs.org/rfcs/rfc1950.html>, 1574F<http://www.faqs.org/rfcs/rfc1951.html> and 1575F<http://www.faqs.org/rfcs/rfc1952.html> 1576 1577The I<zlib> compression library was written by Jean-loup Gailly 1578F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. 1579 1580The primary site for the I<zlib> compression library is 1581F<http://www.zlib.org>. 1582 1583The primary site for gzip is F<http://www.gzip.org>. 1584 1585=head1 AUTHOR 1586 1587This module was written by Paul Marquess, F<pmqs@cpan.org>. 1588 1589=head1 MODIFICATION HISTORY 1590 1591See the Changes file. 1592 1593=head1 COPYRIGHT AND LICENSE 1594 1595Copyright (c) 2005-2013 Paul Marquess. All rights reserved. 1596 1597This program is free software; you can redistribute it and/or 1598modify it under the same terms as Perl itself. 1599 1600