📄 zlib.pm
字号:
return Z_DATA_ERROR() if length($$string) < GZIP_FEXTRA_HEADER_SIZE ; my ($extra_len) = unpack ('v', $$string); $extra_len += GZIP_FEXTRA_HEADER_SIZE; return Z_DATA_ERROR() if length($$string) < $extra_len ; substr($$string, 0, $extra_len) = ''; } # skip orig name if ($flags & GZIP_FLG_FNAME) { my $name_end = index ($$string, GZIP_NULL_BYTE); return Z_DATA_ERROR() if $name_end == -1 ; substr($$string, 0, $name_end + 1) = ''; } # skip comment if ($flags & GZIP_FLG_FCOMMENT) { my $comment_end = index ($$string, GZIP_NULL_BYTE); return Z_DATA_ERROR() if $comment_end == -1 ; substr($$string, 0, $comment_end + 1) = ''; } # skip header crc if ($flags & GZIP_FLG_FHCRC) { return Z_DATA_ERROR() if length ($$string) < GZIP_FHCRC_SIZE ; substr($$string, 0, GZIP_FHCRC_SIZE) = ''; } return Z_OK();}sub memGunzip($){ # if the buffer isn't a reference, make it one my $string = (ref $_[0] ? $_[0] : \$_[0]); $] >= 5.008 and (utf8::downgrade($$string, 1) or croak "Wide character in memGunzip"); _removeGzipHeader($string) == Z_OK() or return undef; my $bufsize = length $$string > 4096 ? length $$string : 4096 ; my $x = new Compress::Raw::Zlib::Inflate({-WindowBits => - MAX_WBITS(), -Bufsize => $bufsize}) or return undef; my $output = "" ; my $status = $x->inflate($string, $output); return undef unless $status == Z_STREAM_END(); if (length $$string >= 8) { my ($crc, $len) = unpack ("VV", substr($$string, 0, 8)); substr($$string, 0, 8) = ''; return undef unless $len == length($output) and $crc == crc32($output); } else { $$string = ''; } return $output; }# Autoload methods go after __END__, and are processed by the autosplit program.1;__END__=head1 NAMECompress::Zlib - Interface to zlib compression library=head1 SYNOPSIS use Compress::Zlib ; ($d, $status) = deflateInit( [OPT] ) ; $status = $d->deflate($input, $output) ; $status = $d->flush($output [, $flush_type]) ; $d->deflateParams(OPTS) ; $d->deflateTune(OPTS) ; $d->dict_adler() ; $d->crc32() ; $d->adler32() ; $d->total_in() ; $d->total_out() ; $d->msg() ; $d->get_Strategy(); $d->get_Level(); $d->get_BufSize(); ($i, $status) = inflateInit( [OPT] ) ; $status = $i->inflate($input, $output [, $eof]) ; $status = $i->inflateSync($input) ; $i->dict_adler() ; $d->crc32() ; $d->adler32() ; $i->total_in() ; $i->total_out() ; $i->msg() ; $d->get_BufSize(); $dest = compress($source) ; $dest = uncompress($source) ; $gz = gzopen($filename or filehandle, $mode) ; $bytesread = $gz->gzread($buffer [,$size]) ; $bytesread = $gz->gzreadline($line) ; $byteswritten = $gz->gzwrite($buffer) ; $status = $gz->gzflush($flush) ; $offset = $gz->gztell() ; $status = $gz->gzseek($offset, $whence) ; $status = $gz->gzclose() ; $status = $gz->gzeof() ; $status = $gz->gzsetparams($level, $strategy) ; $errstring = $gz->gzerror() ; $gzerrno $dest = Compress::Zlib::memGzip($buffer) ; $dest = Compress::Zlib::memGunzip($buffer) ; $crc = adler32($buffer [,$crc]) ; $crc = crc32($buffer [,$crc]) ; $crc = adler32_combine($crc1, $crc2, $len2)l $crc = crc32_combine($adler1, $adler2, $len2) ZLIB_VERSION ZLIB_VERNUM=head1 DESCRIPTIONThe I<Compress::Zlib> module provides a Perl interface to the I<zlib>compression library (see L</AUTHOR> for details about where to getI<zlib>). The C<Compress::Zlib> module can be split into two general areas offunctionality, namely a simple read/write interface to I<gzip> filesand a low-level in-memory compression/decompression interface.Each of these areas will be discussed in the following sections.=head2 Notes for users of Compress::Zlib version 1The main change in C<Compress::Zlib> version 2.x is that it does not nowinterface directly to the zlib library. Instead it uses theC<IO::Compress::Gzip> and C<IO::Uncompress::Gunzip> modules forreading/writing gzip files, and the C<Compress::Raw::Zlib> module for somelow-level zlib access. The interface provided by version 2 of this module should be 100% backwardcompatible with version 1. If you find a difference in the expectedbehaviour please contact the author (See L</AUTHOR>). See L<GZIP INTERFACE> With the creation of the C<IO::Compress> and C<IO::Uncompress> modules nonew features are planned for C<Compress::Zlib> - the new modules doeverything that C<Compress::Zlib> does and then some. Development onC<Compress::Zlib> will be limited to bug fixes only.If you are writing new code, your first port of call should be one of thenew C<IO::Compress> or C<IO::Uncompress> modules.=head1 GZIP INTERFACEA number of functions are supplied in I<zlib> for reading and writingI<gzip> files that conform to RFC 1952. This module provides an interfaceto most of them. If you have previously used C<Compress::Zlib> 1.x, the followingenhancements/changes have been made to the C<gzopen> interface:=over 5=item 1If you want to to open either STDIN or STDOUT with C<gzopen>, you can nowoptionally use the special filename "C<->" as a synonym for C<\*STDIN> andC<\*STDOUT>.=item 2 In C<Compress::Zlib> version 1.x, C<gzopen> used the zlib library to openthe underlying file. This made things especially tricky when a Perlfilehandle was passed to C<gzopen>. Behind the scenes the numeric C filedescriptor had to be extracted from the Perl filehandle and this passed tothe zlib library.Apart from being non-portable to some operating systems, this made itdifficult to use C<gzopen> in situations where you wanted to extract/createa gzip data stream that is embedded in a larger file, without having toresort to opening and closing the file multiple times. It also made it impossible to pass a perl filehandle that wasn't associatedwith a real filesystem file, like, say, an C<IO::String>.In C<Compress::Zlib> version 2.x, the C<gzopen> interface has beencompletely rewritten to use the L<IO::Compress::Gzip|IO::Compress::Gzip>for writing gzip files and L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>for reading gzip files. None of the limitations mentioned above apply.=item 3Addition of C<gzseek> to provide a restricted C<seek> interface.=item 4.Added C<gztell>.=backA more complete and flexible interface for reading/writing gzipfiles/buffers is included with the module C<IO-Compress-Zlib>. SeeL<IO::Compress::Gzip|IO::Compress::Gzip> andL<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for more details.=over 5=item B<$gz = gzopen($filename, $mode)>=item B<$gz = gzopen($filehandle, $mode)>This function opens either the I<gzip> file C<$filename> for reading orwriting or attaches to the opened filehandle, C<$filehandle>. It returns an object on success and C<undef> on failure.When writing a gzip file this interface will I<always> create the smallestpossible gzip header (exactly 10 bytes). If you want greater control overwhat gets stored in the gzip header (like the original filename or acomment) use L<IO::Compress::Gzip|IO::Compress::Gzip> instead. Similarly ifyou want to read the contents of the gzip header useL<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.The second parameter, C<$mode>, is used to specify whether the file isopened for reading or writing and to optionally specify a compressionlevel and compression strategy when writing. The format of the C<$mode>parameter is similar to the mode parameter to the 'C' function C<fopen>,so "rb" is used to open for reading, "wb" for writing and "ab" forappending (writing at the end of the file).To specify a compression level when writing, append a digit between 0and 9 to the mode string -- 0 means no compression and 9 means maximumcompression.If no compression level is specified Z_DEFAULT_COMPRESSION is used.To specify the compression strategy when writing, append 'f' for filtereddata, 'h' for Huffman only compression, or 'R' for run-length encoding.If no strategy is specified Z_DEFAULT_STRATEGY is used.So, for example, "wb9" means open for writing with the maximum compressionusing the default strategy and "wb4R" means open for writing with compressionlevel 4 and run-length encoding.Refer to the I<zlib> documentation for the exact format of the C<$mode>parameter.=item B<$bytesread = $gz-E<gt>gzread($buffer [, $size]) ;>Reads C<$size> bytes from the compressed file into C<$buffer>. IfC<$size> is not specified, it will default to 4096. If the scalarC<$buffer> is not large enough, it will be extended automatically.Returns the number of bytes actually read. On EOF it returns 0 and inthe case of an error, -1.=item B<$bytesread = $gz-E<gt>gzreadline($line) ;>Reads the next line from the compressed file into C<$line>. Returns the number of bytes actually read. On EOF it returns 0 and inthe case of an error, -1.It is legal to intermix calls to C<gzread> and C<gzreadline>.To maintain backward compatibility with version 1.x of this moduleC<gzreadline> ignores the C<$/> variable - it I<always> uses the stringC<"\n"> as the line delimiter. If you want to read a gzip file a line at a time and have it respect theC<$/> variable (or C<$INPUT_RECORD_SEPARATOR>, or C<$RS> when C<English> isin use) see L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>.=item B<$byteswritten = $gz-E<gt>gzwrite($buffer) ;>Writes the contents of C<$buffer> to the compressed file. Returns thenumber of bytes actually written, or 0 on error.=item B<$status = $gz-E<gt>gzflush($flush_type) ;>Flushes all pending output into the compressed file.This method takes an optional parameter, C<$flush_type>, that controlshow the flushing will be carried out. By default the C<$flush_type>used is C<Z_FINISH>. Other valid values for C<$flush_type> areC<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It isstrongly recommended that you only set the C<flush_type> parameter ifyou fully understand the implications of what it does - overuse of C<flush>can seriously degrade the level of compression achieved. See the C<zlib>documentation for details.Returns 0 on success.=item B<$offset = $gz-E<gt>gztell() ;>Returns the uncompressed file offset.=item B<$status = $gz-E<gt>gzseek($offset, $whence) ;>Provides a sub-set of the C<seek> functionality, with the restrictionthat it is only legal to seek forward in the compressed file.It is a fatal error to attempt to seek backward.When opened for writing, empty parts of the file will have NULL (0x00)bytes written to them.The C<$whence> parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END.Returns 1 on success, 0 on failure.=item B<$gz-E<gt>gzclose>Closes the compressed file. Any pending data is flushed to the filebefore it is closed.Returns 0 on success.=item B<$gz-E<gt>gzsetparams($level, $strategy>Change settings for the deflate stream C<$gz>.The list of the valid options is shown below. Options not specifiedwill remain unchanged.Note: This method is only available if you are running zlib 1.0.6 or better.=over 5=item B<$level>Defines the compression level. Valid values are 0 through 9,C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, andC<Z_DEFAULT_COMPRESSION>.=item B<$strategy>Defines the strategy used to tune the compression. The valid values areC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. =back=item B<$gz-E<gt>gzerror>Returns the I<zlib> error message or number for the last operationassociated with C<$gz>. The return value will be the I<zlib> errornumber when used in a numeric context and the I<zlib> error messagewhen used in a string context. The I<zlib> error number constants,shown below, are available for use. Z_OK Z_STREAM_END Z_ERRNO Z_STREAM_ERROR Z_DATA_ERROR Z_MEM_ERROR Z_BUF_ERROR=item B<$gzerrno>The C<$gzerrno> scalar holds the error code associated with the mostrecent I<gzip> routine. Note that unlike C<gzerror()>, the error isI<not> associated with a particular file.As with C<gzerror()> it returns an error number in numeric context andan error message in string context. Unlike C<gzerror()> though, theerror message will correspond to the I<zlib> message when the error isassociated with I<zlib> itself, or the UNIX error message when it isnot (i.e. I<zlib> returned C<Z_ERRORNO>).As there is an overlap between the error numbers used by I<zlib> andUNIX, C<$gzerrno> should only be used to check for the presence ofI<an> error in numeric context. Use C<gzerror()> to check for specificI<zlib> errors. The I<gzcat> example below shows how the variable canbe used safely.=back=head2 ExamplesHere is an example script which uses the interface. It implements aI<gzcat> function. use strict ; use warnings ; use Compress::Zlib ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $file (@ARGV) { my $buffer ; my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; print $buffer while $gz->gzread($buffer) > 0 ; die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; }Below is a script which makes use of C<gzreadline>. It implements avery simple I<grep> like script. use strict ; use warnings ; use Compress::Zlib ; die "Usage: gzgrep pattern [file...]\n" unless @ARGV >= 1; my $pattern = shift ; # use stdin if no files supplied @ARGV = '-' unless @ARGV ; foreach my $file (@ARGV) { my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n" ; while ($gz->gzreadline($_) > 0) { print if /$pattern/ ; } die "Error reading from $file: $gzerrno\n" if $gzerrno != Z_STREAM_END ; $gz->gzclose() ; }This script, I<gzstream>, does the opposite of the I<gzcat> scriptabove. It reads from standard input and writes a gzip data stream tostandard output. use strict ; use warnings ; use Compress::Zlib ; binmode STDOUT; # gzopen only sets it on the fd my $gz = gzopen(\*STDOUT, "wb") or die "Cannot open stdout: $gzerrno\n" ; while (<>) { $gz->gzwrite($_) or die "error writing: $gzerrno\n" ; } $gz->gzclose ;=head2 Compress::Zlib::memGzipThis function is used to create an in-memory gzip file with the minimumpossible gzip header (exactly 10 bytes). $dest = Compress::Zlib::memGzip($buffer) ;If successful, it returns the in-memory gzip file, otherwise it returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -