📄 io::compress::zip.3
字号:
\& use IO::Compress::Zip qw(zip $ZipError) ;\&\& my $input = "file1.txt";\& zip $input => "$input.zip"\& or die "zip failed: $ZipError\en";.Ve.PPTo read from an existing Perl filehandle, \f(CW$input\fR, and write thecompressed data to a buffer, \f(CW$buffer\fR..PP.Vb 4\& use strict ;\& use warnings ;\& use IO::Compress::Zip qw(zip $ZipError) ;\& use IO::File ;\&\& my $input = new IO::File "<file1.txt"\& or die "Cannot open \*(Aqfile1.txt\*(Aq: $!\en" ;\& my $buffer ;\& zip $input => \e$buffer \& or die "zip failed: $ZipError\en";.Ve.PPTo compress all files in the directory \*(L"/my/home\*(R" that match \*(L"*.txt\*(R"and store the compressed data in the same directory.PP.Vb 3\& use strict ;\& use warnings ;\& use IO::Compress::Zip qw(zip $ZipError) ;\&\& zip \*(Aq</my/home/*.txt>\*(Aq => \*(Aq<*.zip>\*(Aq\& or die "zip failed: $ZipError\en";.Ve.PPand if you want to compress each file one at a time, this will do the trick.PP.Vb 3\& use strict ;\& use warnings ;\& use IO::Compress::Zip qw(zip $ZipError) ;\&\& for my $input ( glob "/my/home/*.txt" )\& {\& my $output = "$input.zip" ;\& zip $input => $output \& or die "Error compressing \*(Aq$input\*(Aq: $ZipError\en";\& }.Ve.SH "OO Interface".IX Header "OO Interface".Sh "Constructor".IX Subsection "Constructor"The format of the constructor for \f(CW\*(C`IO::Compress::Zip\*(C'\fR is shown below.PP.Vb 2\& my $z = new IO::Compress::Zip $output [,OPTS]\& or die "IO::Compress::Zip failed: $ZipError\en";.Ve.PPIt returns an \f(CW\*(C`IO::Compress::Zip\*(C'\fR object on success and undef on failure. The variable \f(CW$ZipError\fR will contain an error message on failure..PPIf you are running Perl 5.005 or better the object, \f(CW$z\fR, returned from IO::Compress::Zip can be used exactly like an IO::File filehandle. This means that all normal output file operations can be carried out with \f(CW$z\fR. For example, to write to a compressed file/buffer you can use either of these forms.PP.Vb 2\& $z\->print("hello world\en");\& print $z "hello world\en";.Ve.PPThe mandatory parameter \f(CW$output\fR is used to control the destinationof the compressed data. This parameter can take one of these forms..IP "A filename" 5.IX Item "A filename"If the \f(CW$output\fR 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..IP "A filehandle" 5.IX Item "A filehandle"If the \f(CW$output\fR parameter is a filehandle, the compressed data will bewritten to it.The string '\-' can be used as an alias for standard output..IP "A scalar reference" 5.IX Item "A scalar reference"If \f(CW$output\fR is a scalar reference, the compressed data will be storedin \f(CW$$output\fR..PPIf the \f(CW$output\fR parameter is any other type, \f(CW\*(C`IO::Compress::Zip\*(C'\fR::new willreturn undef..Sh "Constructor Options".IX Subsection "Constructor Options"\&\f(CW\*(C`OPTS\*(C'\fR is any combination of the following options:.ie n .IP """AutoClose => 0|1""" 5.el .IP "\f(CWAutoClose => 0|1\fR" 5.IX Item "AutoClose => 0|1"This option is only valid when the \f(CW$output\fR parameter is a filehandle. Ifspecified, and the value is true, it will result in the \f(CW$output\fR beingclosed once either the \f(CW\*(C`close\*(C'\fR method is called or the \f(CW\*(C`IO::Compress::Zip\*(C'\fRobject is destroyed..SpThis parameter defaults to 0..ie n .IP """Append => 0|1""" 5.el .IP "\f(CWAppend => 0|1\fR" 5.IX Item "Append => 0|1"Opens \f(CW$output\fR in append mode..SpThe behaviour of this option is dependent on the type of \f(CW$output\fR..RS 5.IP "\(bu" 5A Buffer.SpIf \f(CW$output\fR is a buffer and \f(CW\*(C`Append\*(C'\fR is enabled, all compressed datawill be append to the end if \f(CW$output\fR. Otherwise \f(CW$output\fR will becleared before any data is written to it..IP "\(bu" 5A Filename.SpIf \f(CW$output\fR is a filename and \f(CW\*(C`Append\*(C'\fR is enabled, the file will beopened in append mode. Otherwise the contents of the file, if any, will betruncated before any compressed data is written to it..IP "\(bu" 5A Filehandle.SpIf \f(CW$output\fR is a filehandle, the file pointer will be positioned to theend of the file via a call to \f(CW\*(C`seek\*(C'\fR before any compressed data is writtento it. Otherwise the file pointer will not be moved..RE.RS 5.SpThis parameter defaults to 0..RE.ie n .IP """Name => $string""" 5.el .IP "\f(CWName => $string\fR" 5.IX Item "Name => $string"Stores the contents of \f(CW$string\fR in the zip filename header field. If\&\f(CW\*(C`Name\*(C'\fR is not specified, no zip filename field will be created..ie n .IP """Time => $number""" 5.el .IP "\f(CWTime => $number\fR" 5.IX Item "Time => $number"Sets the last modified time field in the zip header to \f(CW$number\fR..SpThis field defaults to the time the \f(CW\*(C`IO::Compress::Zip\*(C'\fR object was createdif this option is not specified..ie n .IP """ExtAttr => $attr""" 5.el .IP "\f(CWExtAttr => $attr\fR" 5.IX Item "ExtAttr => $attr"This option controls the \*(L"external file attributes\*(R" field in the centralheader of the zip file. This is a 4 byte field..SpThis option defaults to 0..ie n .IP """exTime => [$atime, $mtime, $ctime]""" 5.el .IP "\f(CWexTime => [$atime, $mtime, $ctime]\fR" 5.IX Item "exTime => [$atime, $mtime, $ctime]"This option expects an array reference with exactly three elements:\&\f(CW$atime\fR, \f(CW\*(C`mtime\*(C'\fR and \f(CW$ctime\fR. These correspond to the last accesstime, last modification time and creation time respectively..SpIt uses these values to set the extended timestamp field in the local zipheader to the three values, \f(CW$atime\fR, \f(CW$mtime\fR, \f(CW$ctime\fR and sets the extendedtimestamp field in the central zip header to \f(CW$mtime\fR..SpIf any of the three values is \f(CW\*(C`undef\*(C'\fR that time value will not be used.So, for example, to set only the \f(CW$mtime\fR you would use this.Sp.Vb 1\& exTime => [undef, $mtime, undef].Ve.SpIf the \f(CW\*(C`Minimal\*(C'\fR option is set to true, this option will be ignored..SpBy default no extended time field is created..ie n .IP """Comment => $comment""" 5.el .IP "\f(CWComment => $comment\fR" 5.IX Item "Comment => $comment"Stores the contents of \f(CW$comment\fR in the Central File Header ofthe zip file..SpBy default, no comment field is written to the zip file..ie n .IP """ZipComment => $comment""" 5.el .IP "\f(CWZipComment => $comment\fR" 5.IX Item "ZipComment => $comment"Stores the contents of \f(CW$comment\fR in the End of Central Directory recordof the zip file..SpBy default, no comment field is written to the zip file..ie n .IP """Method => $method""" 5.el .IP "\f(CWMethod => $method\fR" 5.IX Item "Method => $method"Controls which compression method is used. At present three compressionmethods are supported, namely Store (no compression at all), Deflate andBzip2..SpThe symbols, \s-1ZIP_CM_STORE\s0, \s-1ZIP_CM_DEFLATE\s0 and \s-1ZIP_CM_BZIP2\s0 are used toselect the compression method..SpThese constants are not imported by \f(CW\*(C`IO::Compress::Zip\*(C'\fR by default..Sp.Vb 3\& use IO::Compress::Zip qw(:zip_method);\& use IO::Compress::Zip qw(:constants);\& use IO::Compress::Zip qw(:all);.Ve.SpNote that to create Bzip2 content, the module \f(CW\*(C`IO::Compress::Bzip2\*(C'\fR mustbe installed. A fatal error will be thrown if you attempt to create Bzip2content when \f(CW\*(C`IO::Compress::Bzip2\*(C'\fR is not available..SpThe default method is \s-1ZIP_CM_DEFLATE\s0..ie n .IP """Stream => 0|1""" 5.el .IP "\f(CWStream => 0|1\fR" 5.IX Item "Stream => 0|1"This option controls whether the zip file/buffer output is created instreaming mode..SpNote that when outputting to a file with streaming mode disabled (\f(CW\*(C`Stream\*(C'\fRis 0), the output file must be seekable..SpThe default is 1..ie n .IP """Zip64 => 0|1""" 5.el .IP "\f(CWZip64 => 0|1\fR" 5.IX Item "Zip64 => 0|1"Create a Zip64 zip file/buffer. This option should only be used if you wantto store files larger than 4 Gig..SpIf you intend to manipulate the Zip64 zip files created with this moduleusing an external zip/unzip make sure that it supports streaming Zip64..SpIn particular, if you are using Info-Zip you need to have zip version 3.xor better to update a Zip64 archive and unzip version 6.x to read a zip64archive. At the time of writing both are beta status..SpWhen the \f(CW\*(C`Zip64\*(C'\fR option is enabled, the \f(CW\*(C`Stream\*(C'\fR option \fImust\fR beenabled as well..SpThe default is 0..ie n .IP """TextFlag => 0|1""" 5.el .IP "\f(CWTextFlag => 0|1\fR" 5.IX Item "TextFlag => 0|1"This parameter controls the setting of a bit in the zip central header. Itis used to signal that the data stored in the zip file/buffer is probablytext..SpThe default is 0..ie n .IP """ExtraFieldLocal => $data""\fR =item \f(CW""ExtraFieldCentral => $data""" 5.el .IP "\f(CWExtraFieldLocal => $data\fR =item \f(CWExtraFieldCentral => $data\fR" 5.IX Item "ExtraFieldLocal => $data =item ExtraFieldCentral => $data"These options allows additional metadata to be stored in the local andcentral headers in the zip file/buffer..SpAn extra field consists of zero or more subfields. Each subfield consistsof a two byte header followed by the subfield data..SpThe list of subfields can be supplied in any of the following formats.Sp.Vb 4\& ExtraFieldLocal => [$id1, $data1,\& $id2, $data2,\& ...\& ]\&\& ExtraFieldLocal => [ [$id1 => $data1],\& [$id2 => $data2],\& ...\& ]\&\& ExtraFieldLocal => { $id1 => $data1,\& $id2 => $data2,\& ...\& }.Ve.SpWhere \f(CW$id1\fR, \f(CW$id2\fR are two byte subfield \s-1ID\s0's..SpIf you use the hash syntax, you have no control over the order in whichthe ExtraSubFields are stored, plus you cannot have SubFields withduplicate \s-1ID\s0..SpAlternatively the list of subfields can by supplied as a scalar, thus.Sp.Vb 1\& ExtraField => $rawdata.Ve.SpThe Extended Time field, set using the \f(CW\*(C`exTime\*(C'\fR option, is an example ofan extended field..SpIf the \f(CW\*(C`Minimal\*(C'\fR option is set to true, this option will be ignored..SpThe maximum size of an extra field 65535 bytes..ie n .IP """Minimal => 1|0""" 5.el .IP "\f(CWMinimal => 1|0\fR" 5.IX Item "Minimal => 1|0"If specified, this option will disable the creation of all extended fieldsin the zip local and central headers. So the \f(CW\*(C`exTime\*(C'\fR, \f(CW\*(C`ExtraFieldLocal\*(C'\fRand \f(CW\*(C`ExtraFieldCentral\*(C'\fR options will be ignored..SpThis parameter defaults to 0..ie n .IP """BlockSize100K => number""" 5.el .IP "\f(CWBlockSize100K => number\fR" 5.IX Item "BlockSize100K => number"Specify the number of 100K blocks bzip2 uses during compression..SpValid values are from 1 to 9, where 9 is best compression..SpThis option is only valid if the \f(CW\*(C`Method\*(C'\fR is \s-1ZIP_CM_BZIP2\s0. It is ignoredotherwise..SpThe default is 1..ie n .IP """WorkFactor => number""" 5.el .IP "\f(CWWorkFactor => number\fR" 5.IX Item "WorkFactor => number"Specifies how much effort bzip2 should take before resorting to a slowerfallback compression algorithm..SpValid values range from 0 to 250, where 0 means use the default value 30..SpThis option is only valid if the \f(CW\*(C`Method\*(C'\fR is \s-1ZIP_CM_BZIP2\s0. It is ignoredotherwise..SpThe default is 0..IP "\-Level" 5.IX Item "-Level"Defines the compression level used by zlib. The value should either bea number between 0 and 9 (0 means no compression and 9 is maximumcompression), or one of the symbolic constants defined below..Sp.Vb 4\& Z_NO_COMPRESSION\& Z_BEST_SPEED\& Z_BEST_COMPRESSION\& Z_DEFAULT_COMPRESSION.Ve.SpThe default is Z_DEFAULT_COMPRESSION..SpNote, these constants are not imported by \f(CW\*(C`IO::Compress::Zip\*(C'\fR by default..Sp.Vb 3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -