zip.html
来自「perl教程」· HTML 代码 · 共 1,222 行 · 第 1/5 页
HTML
1,222 行
<p>Extract the given member, or match its name and extract it.
Does not use path information (extracts into the current
directory). Returns undef if member doesn't exist in this
Zip.
If optional second arg is given, use it as the name of the
extracted member (its paths will be deleted too). Otherwise,
the internal filename of the member (minus paths) is used as
the name of the extracted file or directory. Returns <a href="#item_az_ok"><code>AZ_OK</code></a>
on success.</p>
</dd>
</li>
<dt><strong><a name="item_addmember">addMember( $member )</a></strong>
<dd>
<p>Append a member (possibly from another zip file) to the zip
file. Returns the new member. Generally, you will use
addFile(), addDirectory(), addFileOrDirectory(), addString(),
or <a href="#item_read"><code>read()</code></a> to add members.</p>
</dd>
<dd>
<pre>
<span class="comment"># Move member named 'abc' to end of zip:</span>
<span class="keyword">my</span> <span class="variable">$member</span> <span class="operator">=</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">removeMember</span><span class="operator">(</span> <span class="string">'abc'</span> <span class="operator">);</span>
<span class="variable">$zip</span><span class="operator">-></span><span class="variable">addMember</span><span class="operator">(</span> <span class="variable">$member</span> <span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_updatemember">updateMember( $memberOrName, $fileName )</a></strong>
<dd>
<p>Update a single member from the file or directory named <code>$fileName</code>.
Returns the (possibly added or updated) member, if any; <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on
errors.
The comparison is based on <a href="#item_lastmodtime"><code>lastModTime()</code></a> and (in the case of a
non-directory) the size of the file.</p>
</dd>
</li>
<dt><strong><a name="item_addfile">addFile( $fileName [, $newName ] )</a></strong>
<dd>
<p>Append a member whose data comes from an external file,
returning the member or undef. The member will have its file
name set to the name of the external file, and its
desiredCompressionMethod set to COMPRESSION_DEFLATED. The
file attributes and last modification time will be set from
the file.
If the name given does not represent a readable plain file or
symbolic link, undef will be returned. <code>$fileName</code> must be
in the format required for the local file system.
The optional <code>$newName</code> argument sets the internal file name
to something different than the given $fileName. <code>$newName</code>,
if given, must be in Zip name format (i.e. Unix).
The text mode bit will be set if the contents appears to be
text (as returned by the <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> perl operator).</p>
</dd>
<dd>
<p><em>NOTE</em> that you shouldn't (generally) use absolute path names
in zip member names, as this will cause problems with some zip
tools as well as introduce a security hole and make the zip
harder to use.</p>
</dd>
</li>
<dt><strong><a name="item_adddirectory">addDirectory( $directoryName [, $fileName ] )</a></strong>
<dd>
<p>Append a member created from the given directory name. The
directory name does not have to name an existing directory.
If the named directory exists, the file modification time and
permissions are set from the existing directory, otherwise
they are set to now and permissive default permissions.
<code>$directoryName</code> must be in local file system format.
The optional second argument sets the name of the archive
member (which defaults to <code>$directoryName</code>). If given, it
must be in Zip (Unix) format.
Returns the new member.</p>
</dd>
</li>
<dt><strong><a name="item_addfileordirectory">addFileOrDirectory( $name [, $newName ] )</a></strong>
<dd>
<p>Append a member from the file or directory named $name. If
$newName is given, use it for the name of the new member.
Will add or remove trailing slashes from $newName as needed.
<code>$name</code> must be in local file system format.
The optional second argument sets the name of the archive
member (which defaults to <code>$name</code>). If given, it must be in
Zip (Unix) format.</p>
</dd>
</li>
<dt><strong><a name="item_addstring">addString( $stringOrStringRef, $name )</a></strong>
<dd>
<p>Append a member created from the given string or string
reference. The name is given by the second argument.
Returns the new member. The last modification time will be
set to now, and the file attributes will be set to permissive
defaults.</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$member</span> <span class="operator">=</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">addString</span><span class="operator">(</span> <span class="string">'This is a test'</span><span class="operator">,</span> <span class="string">'test.txt'</span> <span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_contents">contents( $memberOrMemberName [, $newContents ] )</a></strong>
<dd>
<p>Returns the uncompressed data for a particular member, or
undef.</p>
</dd>
<dd>
<pre>
<span class="keyword">print</span> <span class="string">"xyz.txt contains "</span> <span class="operator">.</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">contents</span><span class="operator">(</span> <span class="string">'xyz.txt'</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Also can change the contents of a member:</p>
</dd>
<dd>
<pre>
<span class="variable">$zip</span><span class="operator">-></span><span class="variable">contents</span><span class="operator">(</span> <span class="string">'xyz.txt'</span><span class="operator">,</span> <span class="string">'This is the new contents'</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>If called expecting an array as the return value, it will include
the status as the second value in the array.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$content</span><span class="operator">,</span> <span class="variable">$status</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">contents</span><span class="operator">(</span> <span class="string">'xyz.txt'</span><span class="operator">);</span>
</pre>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="zip_archive_i_o_operations">Zip Archive I/O operations</a></h2>
<p>A Zip archive can be written to a file or file handle, or read from
one.</p>
<dl>
<dt><strong><a name="item_writetofilenamed">writeToFileNamed( $fileName )</a></strong>
<dd>
<p>Write a zip archive to named file. Returns <a href="#item_az_ok"><code>AZ_OK</code></a> on
success.</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$status</span> <span class="operator">=</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">writeToFileNamed</span><span class="operator">(</span> <span class="string">'xx.zip'</span> <span class="operator">);</span>
<span class="keyword">die</span> <span class="string">"error somewhere"</span> <span class="keyword">if</span> <span class="variable">$status</span> <span class="operator">!=</span> <span class="variable">AZ_OK</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Note that if you use the same name as an existing zip file
that you read in, you will clobber ZipFileMembers. So
instead, write to a different file name, then delete the
original.
If you use the <a href="#item_overwrite"><code>overwrite()</code></a> or <a href="#item_overwriteas"><code>overwriteAs()</code></a> methods, you can
re-write the original zip in this way.
<code>$fileName</code> should be a valid file name on your system.</p>
</dd>
</li>
<dt><strong><a name="item_writetofilehandle">writeToFileHandle( $fileHandle [, $seekable] )</a></strong>
<dd>
<p>Write a zip archive to a file handle. Return AZ_OK on
success. The optional second arg tells whether or not to try
to seek backwards to re-write headers. If not provided, it is
set if the Perl <a href="../../lib/Pod/perlrun.html#item__2df"><code>-f</code></a> test returns true. This could fail on
some operating systems, though.</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">IO::File</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span> <span class="string">'someFile.zip'</span><span class="operator">,</span> <span class="string">'w'</span> <span class="operator">);</span>
<span class="keyword">unless</span> <span class="operator">(</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">writeToFileHandle</span><span class="operator">(</span> <span class="variable">$fh</span> <span class="operator">)</span> <span class="operator">!=</span> <span class="variable">AZ_OK</span> <span class="operator">)</span> <span class="operator">{</span>
<span class="comment"># error handling</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>If you pass a file handle that is not seekable (like if
you're writing to a pipe or a socket), pass a false second
argument:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">IO::File</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span> <span class="string">'| cat > somefile.zip'</span><span class="operator">,</span> <span class="string">'w'</span> <span class="operator">);</span>
<span class="variable">$zip</span><span class="operator">-></span><span class="variable">writeToFileHandle</span><span class="operator">(</span> <span class="variable">$fh</span><span class="operator">,</span> <span class="number">0</span> <span class="operator">);</span> <span class="comment"># fh is not seekable</span>
</pre>
</dd>
<dd>
<p>If this method fails during the write of a member, that
member and all following it will return false from
<a href="#item_waswritten"><code>wasWritten()</code></a>. See <a href="#item_writecentraldirectory"><code>writeCentralDirectory()</code></a> for a way to
deal with this.
If you want, you can write data to the file handle before
passing it to writeToFileHandle(); this could be used (for
instance) for making self-extracting archives. However, this
only works reliably when writing to a real file (as opposed
to STDOUT or some other possible non-file).</p>
</dd>
<dd>
<p>See examples/selfex.pl for how to write a self-extracting
archive.</p>
</dd>
</li>
<dt><strong><a name="item_writecentraldirectory">writeCentralDirectory( $fileHandle [, $offset ] )</a></strong>
<dd>
<p>Writes the central directory structure to the given file
handle.</p>
</dd>
<dd>
<p>Returns AZ_OK on success. If given an $offset, will
seek to that point before writing. This can be used for
recovery in cases where writeToFileHandle or writeToFileNamed
returns an IO error because of running out of space on the
destination file.</p>
</dd>
<dd>
<p>You can truncate the zip by seeking backwards and then writing the
directory:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">IO::File</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span> <span class="string">'someFile.zip'</span><span class="operator">,</span> <span class="string">'w'</span> <span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$retval</span> <span class="operator">=</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">writeToFileHandle</span><span class="operator">(</span> <span class="variable">$fh</span> <span class="operator">);</span>
<span class="keyword">if</span> <span class="operator">(</span> <span class="variable">$retval</span> <span class="operator">==</span> <span class="variable">AZ_IO_ERROR</span> <span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">@unwritten</span> <span class="operator">=</span> <span class="keyword">grep</span> <span class="operator">{</span> <span class="keyword">not</span> <span class="variable">$_</span><span class="operator">-></span><span class="variable">wasWritten</span><span class="operator">()</span> <span class="operator">}</span> <span class="variable">$zip</span><span class="operator">-></span><span class="variable">members</span><span class="operator">();</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">@unwritten</span><span class="operator">)</span> <span class="operator">{</span>
<span class="variable">$zip</span><span class="operator">-></span><span class="variable">removeMember</span><span class="operator">(</span> <span class="variable">$member</span> <span class="operator">)</span> <span class="keyword">foreach</span> <span class="keyword">my</span> <span class="variable">$member</span> <span class="operator">(</span> <span class="variable">@unwritten</span> <span class="operator">);</span>
<span class="variable">$zip</span><span class="operator">-></span><span class="variable">writeCentralDirectory</span><span class="operator">(</span> <span class="variable">$fh</span><span class="operator">,</span>
<span class="variable">$unwritten</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">-></span><span class="variable">writeLocalHeaderRelativeOffset</span><span class="operator">());</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_overwriteas">overwriteAs( $newName )</a></strong>
<dd>
<p>Write the zip to the specified file, as safely as possible.
This is done by first writing to a temp file, then renaming
the original if it exists, then renaming the temp file, then
deleting the renamed original if it exists. Returns AZ_OK if
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?