📄 deflate.pm
字号:
package IO::Compress::Deflate ;use strict ;use warnings;use bytes;require Exporter ;use IO::Compress::RawDeflate 2.008 ;use Compress::Raw::Zlib 2.008 ;use IO::Compress::Zlib::Constants 2.008 ;use IO::Compress::Base::Common 2.008 qw(createSelfTiedObject);our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $DeflateError);$VERSION = '2.008';$DeflateError = '';@ISA = qw(Exporter IO::Compress::RawDeflate);@EXPORT_OK = qw( $DeflateError deflate ) ;%EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ;push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;Exporter::export_ok_tags('all');sub new{ my $class = shift ; my $obj = createSelfTiedObject($class, \$DeflateError); return $obj->_create(undef, @_);}sub deflate{ my $obj = createSelfTiedObject(undef, \$DeflateError); return $obj->_def(@_);}sub bitmask($$$$){ my $into = shift ; my $value = shift ; my $offset = shift ; my $mask = shift ; return $into | (($value & $mask) << $offset ) ;}sub mkDeflateHdr($$$;$){ my $method = shift ; my $cinfo = shift; my $level = shift; my $fdict_adler = shift ; my $cmf = 0; my $flg = 0; my $fdict = 0; $fdict = 1 if defined $fdict_adler; $cmf = bitmask($cmf, $method, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS); $cmf = bitmask($cmf, $cinfo, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS); $flg = bitmask($flg, $fdict, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS); $flg = bitmask($flg, $level, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS); my $fcheck = 31 - ($cmf * 256 + $flg) % 31 ; $flg = bitmask($flg, $fcheck, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS); my $hdr = pack("CC", $cmf, $flg) ; $hdr .= pack("N", $fdict_adler) if $fdict ; return $hdr;}sub mkHeader { my $self = shift ; my $param = shift ; my $level = $param->value('Level'); my $strategy = $param->value('Strategy'); my $lflag ; $level = 6 if $level == Z_DEFAULT_COMPRESSION ; if (ZLIB_VERNUM >= 0x1210) { if ($strategy >= Z_HUFFMAN_ONLY || $level < 2) { $lflag = ZLIB_FLG_LEVEL_FASTEST } elsif ($level < 6) { $lflag = ZLIB_FLG_LEVEL_FAST } elsif ($level == 6) { $lflag = ZLIB_FLG_LEVEL_DEFAULT } else { $lflag = ZLIB_FLG_LEVEL_SLOWEST } } else { $lflag = ($level - 1) >> 1 ; $lflag = 3 if $lflag > 3 ; } #my $wbits = (MAX_WBITS - 8) << 4 ; my $wbits = 7; mkDeflateHdr(ZLIB_CMF_CM_DEFLATED, $wbits, $lflag);}sub ckParams{ my $self = shift ; my $got = shift; $got->value('ADLER32' => 1); return 1 ;}sub mkTrailer{ my $self = shift ; return pack("N", *$self->{Compress}->adler32()) ;}sub mkFinalTrailer{ return '';}#sub newHeader#{# my $self = shift ;# return *$self->{Header};#}sub getExtraParams{ my $self = shift ; return $self->getZlibParams(),}sub getInverseClass{ return ('IO::Uncompress::Inflate', \$IO::Uncompress::Inflate::InflateError);}sub getFileInfo{ my $self = shift ; my $params = shift; my $file = shift ; }1;__END__=head1 NAMEIO::Compress::Deflate - Write RFC 1950 files/buffers =head1 SYNOPSIS use IO::Compress::Deflate qw(deflate $DeflateError) ; my $status = deflate $input => $output [,OPTS] or die "deflate failed: $DeflateError\n"; my $z = new IO::Compress::Deflate $output [,OPTS] or die "deflate failed: $DeflateError\n"; $z->print($string); $z->printf($format, $string); $z->write($string); $z->syswrite($string [, $length, $offset]); $z->flush(); $z->tell(); $z->eof(); $z->seek($position, $whence); $z->binmode(); $z->fileno(); $z->opened(); $z->autoflush(); $z->input_line_number(); $z->newStream( [OPTS] ); $z->deflateParams(); $z->close() ; $DeflateError ; # IO::File mode print $z $string; printf $z $format, $string; tell $z eof $z seek $z, $position, $whence binmode $z fileno $z close $z ; =head1 DESCRIPTIONThis module provides a Perl interface that allows writing compresseddata to files or buffer as defined in RFC 1950.For reading RFC 1950 files/buffers, see the companion module L<IO::Uncompress::Inflate|IO::Uncompress::Inflate>.=head1 Functional InterfaceA top-level function, C<deflate>, is provided to carry out"one-shot" compression between buffers and/or files. For finercontrol over the compression process, see the L</"OO Interface">section. use IO::Compress::Deflate qw(deflate $DeflateError) ; deflate $input => $output [,OPTS] or die "deflate failed: $DeflateError\n";The functional interface needs Perl5.005 or better.=head2 deflate $input => $output [, OPTS]C<deflate> expects at least two parameters, C<$input> and C<$output>.=head3 The C<$input> parameterThe parameter, C<$input>, is used to define the source ofthe uncompressed data. It can take one of the following forms:=over 5=item A filenameIf the C<$input> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for reading and the input datawill be read from it.=item A filehandleIf the C<$input> parameter is a filehandle, the input data will beread from it.The string '-' can be used as an alias for standard input.=item A scalar reference If C<$input> is a scalar reference, the input data will be readfrom C<$$input>.=item An array reference If C<$input> is an array reference, each element in the array must be afilename.The input data will be read from each file in turn. The complete array will be walked to ensure that it onlycontains valid filenames before any data is compressed.=item An Input FileGlob stringIf C<$input> is a string that is delimited by the characters "<" and ">"C<deflate> will assume that it is an I<input fileglob string>. Theinput is the list of files that match the fileglob.If the fileglob does not match any files ...See L<File::GlobMapper|File::GlobMapper> for more details.=backIf the C<$input> parameter is any other type, C<undef> will be returned.=head3 The C<$output> parameterThe parameter C<$output> is used to control the destination of thecompressed data. This parameter can take one of these forms.=over 5=item A filenameIf the C<$output> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for writing and the compresseddata will be written to it.=item A filehandleIf the C<$output> parameter is a filehandle, the compressed datawill be written to it.The string '-' can be used as an alias for standard output.=item A scalar reference If C<$output> is a scalar reference, the compressed data will bestored in C<$$output>.=item An Array ReferenceIf C<$output> is an array reference, the compressed data will bepushed onto the array.=item An Output FileGlobIf C<$output> is a string that is delimited by the characters "<" and ">"C<deflate> will assume that it is an I<output fileglob string>. Theoutput is the list of files that match the fileglob.When C<$output> is an fileglob string, C<$input> must also be a fileglobstring. Anything else is an error.=backIf the C<$output> parameter is any other type, C<undef> will be returned.=head2 NotesWhen C<$input> maps to multiple files/buffers and C<$output> is a singlefile/buffer the input files/buffers will be storedin C<$output> as a concatenated series of compressed data streams.=head2 Optional ParametersUnless specified below, the optional parameters for C<deflate>,C<OPTS>, are the same as those used with the OO interface defined in theL</"Constructor Options"> section below.=over 5=item C<< AutoClose => 0|1 >>This option applies to any input or output data streams to C<deflate> that are filehandles.If C<AutoClose> is specified, and the value is true, it will result in allinput and/or output filehandles being closed once C<deflate> hascompleted.This parameter defaults to 0.=item C<< BinModeIn => 0|1 >>When reading from a file or filehandle, set C<binmode> before reading.Defaults to 0.=item C<< Append => 0|1 >>TODO=back=head2 ExamplesTo read the contents of the file C<file1.txt> and write the compresseddata to the file C<file1.txt.1950>. use strict ; use warnings ; use IO::Compress::Deflate qw(deflate $DeflateError) ; my $input = "file1.txt"; deflate $input => "$input.1950" or die "deflate failed: $DeflateError\n";To read from an existing Perl filehandle, C<$input>, and write thecompressed data to a buffer, C<$buffer>. use strict ; use warnings ; use IO::Compress::Deflate qw(deflate $DeflateError) ; use IO::File ; my $input = new IO::File "<file1.txt" or die "Cannot open 'file1.txt': $!\n" ; my $buffer ; deflate $input => \$buffer or die "deflate failed: $DeflateError\n";To compress all files in the directory "/my/home" that match "*.txt"and store the compressed data in the same directory use strict ; use warnings ; use IO::Compress::Deflate qw(deflate $DeflateError) ; deflate '</my/home/*.txt>' => '<*.1950>' or die "deflate failed: $DeflateError\n";and if you want to compress each file one at a time, this will do the trick use strict ; use warnings ; use IO::Compress::Deflate qw(deflate $DeflateError) ; for my $input ( glob "/my/home/*.txt" ) { my $output = "$input.1950" ; deflate $input => $output or die "Error compressing '$input': $DeflateError\n"; }=head1 OO Interface=head2 ConstructorThe format of the constructor for C<IO::Compress::Deflate> is shown below my $z = new IO::Compress::Deflate $output [,OPTS] or die "IO::Compress::Deflate failed: $DeflateError\n";It returns an C<IO::Compress::Deflate> object on success and undef on failure. The variable C<$DeflateError> will contain an error message on failure.If you are running Perl 5.005 or better the object, C<$z>, returned from IO::Compress::Deflate can be used exactly like an L<IO::File|IO::File> filehandle. This means that all normal output file operations can be carried out with C<$z>. For example, to write to a compressed file/buffer you can use either of these forms $z->print("hello world\n"); print $z "hello world\n";The mandatory parameter C<$output> is used to control the destinationof the compressed data. This parameter can take one of these forms.=over 5=item A filenameIf the C<$output> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for writing and the compressed datawill be written to it.=item A filehandle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -