faq.html

来自「perl教程」· HTML 代码 · 共 391 行 · 第 1/2 页

HTML
391
字号
Deflate compression.</p>
<p>
</p>
<hr />
<h1><a name="add_a_directory_tree_to_a_zip">Add a directory/tree to a Zip</a></h1>
<p><strong>Q:</strong> How can I add a directory (or tree) full of files to a Zip?</p>
<p><strong>A:</strong> You can use the Archive::Zip::addTree*() methods:</p>
<pre>
   <span class="keyword">use</span> <span class="variable">Archive::Zip</span><span class="operator">;</span>
   <span class="keyword">my</span> <span class="variable">$zip</span> <span class="operator">=</span> <span class="variable">Archive::Zip</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">();</span>
   <span class="comment"># add all readable files and directories below . as xyz/*</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addTree</span><span class="operator">(</span> <span class="string">'.'</span><span class="operator">,</span> <span class="string">'xyz'</span> <span class="operator">);</span> 
   <span class="comment"># add all readable plain files below /abc as def/*</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addTree</span><span class="operator">(</span> <span class="string">'/abc'</span><span class="operator">,</span> <span class="string">'def'</span><span class="operator">,</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">-f</span> <span class="operator">&amp;&amp;</span> <span class="keyword">-r</span> <span class="operator">}</span> <span class="operator">);</span>    
   <span class="comment"># add all .c files below /tmp as stuff/*</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addTreeMatching</span><span class="operator">(</span> <span class="string">'/tmp'</span><span class="operator">,</span> <span class="string">'stuff'</span><span class="operator">,</span> <span class="string">'\.c$'</span> <span class="operator">);</span>
   <span class="comment"># add all .o files below /tmp as stuff/* if they aren't writable</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addTreeMatching</span><span class="operator">(</span> <span class="string">'/tmp'</span><span class="operator">,</span> <span class="string">'stuff'</span><span class="operator">,</span> <span class="string">'\.o$'</span><span class="operator">,</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="operator">!</span> <span class="keyword">-w</span> <span class="operator">}</span> <span class="operator">);</span>
   <span class="comment"># add all .so files below /tmp that are smaller than 200 bytes as stuff/*</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addTreeMatching</span><span class="operator">(</span> <span class="string">'/tmp'</span><span class="operator">,</span> <span class="string">'stuff'</span><span class="operator">,</span> <span class="string">'\.o$'</span><span class="operator">,</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">-s</span> <span class="operator">&lt;</span> <span class="number">200</span> <span class="operator">}</span> <span class="operator">);</span>
   <span class="comment"># and write them into a file</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">writeToFileNamed</span><span class="operator">(</span><span class="string">'xxx.zip'</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="extract_a_directory_tree">Extract a directory/tree</a></h1>
<p><strong>Q:</strong> How can I extract some (or all) files from a Zip into a different
directory?</p>
<p><strong>A:</strong> You can use the Archive::Zip::extractTree() method:
??? ||</p>
<pre>
   <span class="comment"># now extract the same files into /tmpx</span>
   <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">extractTree</span><span class="operator">(</span> <span class="string">'stuff'</span><span class="operator">,</span> <span class="string">'/tmpx'</span> <span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="update_a_directory_tree">Update a directory/tree</a></h1>
<p><strong>Q:</strong> How can I update a Zip from a directory tree, adding or replacing only
the newer files?</p>
<p><strong>A:</strong> You can use the Archive::Zip::updateTree() method that was added in version 1.09.</p>
<p>
</p>
<hr />
<h1><a name="zip_times_might_be_off_by_1_second">Zip times might be off by 1 second</a></h1>
<p><strong>Q:</strong> It bothers me greatly that my file times are wrong by one second about half
the time. Why don't you do something about it?</p>
<p><strong>A:</strong> Get over it. This is a result of the Zip format storing times in DOS
format, which has a resolution of only two seconds.</p>
<p>
</p>
<hr />
<h1><a name="zip_times_don_t_include_time_zone_information">Zip times don't include time zone information</a></h1>
<p><strong>Q:</strong> My file times don't respect time zones. What gives?</p>
<p><strong>A:</strong> If this is important to you, please submit patches to read the various
Extra Fields that encode times with time zones. I'm just using the DOS
Date/Time, which doesn't have a time zone.</p>
<p>
</p>
<hr />
<h1><a name="how_do_i_make_a_selfextracting_zip">How do I make a self-extracting Zip</a></h1>
<p><strong>Q:</strong> I want to make a self-extracting Zip file. Can I do this?</p>
<p><strong>A:</strong> Yes. You can write a self-extracting archive stub (that is, a version of
unzip) to the output filehandle that you pass to writeToFileHandle(). See
examples/selfex.pl for how to write a self-extracting archive.</p>
<p>However, you should understand that this will only work on one kind of
platform (the one for which the stub was compiled).</p>
<p>
</p>
<hr />
<h1><a name="how_can_i_deal_with_zips_with_prepended_garbage__i_e__from_sircam_">How can I deal with Zips with prepended garbage (i.e. from Sircam)</a></h1>
<p><strong>Q:</strong> How can I tell if a Zip has been damaged by adding garbage to the
beginning or inside the file?</p>
<p><strong>A:</strong> I added code for this for the Amavis virus scanner. You can query archives
for their 'eocdOffset' property, which should be 0:</p>
<pre>
  <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">eocdOffset</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">)</span>
    <span class="operator">{</span> <span class="keyword">warn</span><span class="operator">(</span><span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">eocdOffset</span> <span class="operator">.</span> <span class="string">" bytes of garbage at beginning or within Zip"</span><span class="operator">)</span> <span class="operator">}</span>
</pre>
<p>When members are extracted, this offset will be used to adjust the start of
the member if necessary.</p>
<p>
</p>
<hr />
<h1><a name="can_t_extract_shrunk_files">Can't extract Shrunk files</a></h1>
<p><strong>Q:</strong> I'm trying to extract a file out of a Zip produced by PKZIP, and keep
getting this error message:</p>
<pre>
  error: Unsupported compression combination: read 6, write 0</pre>
<p><strong>A:</strong> You can't uncompress this archive member. Archive::Zip only supports uncompressed
members, and compressed members that are compressed using the compression
supported by Compress::Zlib. That means only Deflated and Stored members.</p>
<p>Your file is compressed using the Shrink format, which isn't supported by
Compress::Zlib.</p>
<p>You could, perhaps, use a command-line UnZip program (like the Info-Zip
one) to extract this.</p>
<p>
</p>
<hr />
<h1><a name="can_t_do_decryption">Can't do decryption</a></h1>
<p><strong>Q:</strong> How do I decrypt encrypted Zip members?</p>
<p><strong>A:</strong> With some other program or library. Archive::Zip doesn't support decryption,
and probably never will (unless <em>you</em> write it).</p>
<p>
</p>
<hr />
<h1><a name="how_to_test_file_integrity">How to test file integrity?</a></h1>
<p><strong>Q:</strong> How can Archive::Zip can test the validity of a Zip file?</p>
<p><strong>A:</strong> If you try to decompress the file, the gzip streams will report errors 
if you have garbage. Most of the time.</p>
<p>If you try to open the file and a central directory structure can't be 
found, an error will be reported.</p>
<p>When a file is being read, if we can't find a proper PK.. signature in 
the right places we report a format error.</p>
<p>If there is added garbage at the beginning of a Zip file (as inserted 
by some viruses), you can find out about it, but Archive::Zip will ignore it, 
and you can still use the archive. When it gets written back out the 
added stuff will be gone.</p>
<p>There are two ready-to-use utilities in the examples directory that can
be used to test file integrity, or that you can use as examples
for your own code:</p>
<dl>
<dt><strong><a name="item_examples_2fzipcheck_2epl_shows_how_to_use_an_attem">examples/zipcheck.pl shows how to use an attempted extraction to test a file.</a></strong>

<dt><strong><a name="item_examples_2fziptest_2epl_shows_how_to_test_crcs_in_">examples/ziptest.pl shows how to test CRCs in a file.</a></strong>

</dl>
<p>
</p>
<hr />
<h1><a name="duplicate_files_in_zip">Duplicate files in Zip?</a></h1>
<p><strong>Q:</strong> Archive::Zip let me put the same file in my Zip twice! Why don't you prevent this?</p>
<p><strong>A:</strong> As far as I can tell, this is not disallowed by the Zip spec. If you
think it's a bad idea, check for it yourself:</p>
<pre>
  <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">addFile</span><span class="operator">(</span><span class="variable">$someFile</span><span class="operator">,</span> <span class="variable">$someName</span><span class="operator">)</span> <span class="keyword">unless</span> <span class="variable">$zip</span><span class="operator">-&gt;</span><span class="variable">memberNamed</span><span class="operator">(</span><span class="variable">$someName</span><span class="operator">);</span>
</pre>
<p>I can even imagine cases where this might be useful (for instance, multiple
versions of files).</p>
<p>
</p>
<hr />
<h1><a name="file_ownership_permissions_acls_etc">File ownership/permissions/ACLS/etc</a></h1>
<p><strong>Q:</strong> Why doesn't Archive::Zip deal with file ownership, ACLs, etc.?</p>
<p><strong>A:</strong> There is no standard way to represent these in the Zip file format. If
you want to send me code to properly handle the various extra fields that
have been used to represent these through the years, I'll look at it.</p>
<p>
</p>
<hr />
<h1><a name="i_can_t_compile_but_activestate_only_has_an_old_version_of_archive__zip">I can't compile but ActiveState only has an old version of Archive::Zip</a></h1>
<p><strong>Q:</strong> I've only installed modules using ActiveState's PPM program and
repository. But they have a much older version of Archive::Zip than is in CPAN. Will
you send me a newer PPM?</p>
<p><strong>A:</strong> Probably not, unless I get lots of extra time. But there's no reason you
can't install the version from CPAN. Archive::Zip is pure Perl, so all you need is
NMAKE, which you can get for free from Microsoft (see the FAQ in the
ActiveState documentation for details on how to install CPAN modules).</p>
<p>
</p>
<hr />
<h1><a name="my_jpegs__or_mp3_s__don_t_compress_when_i_put_them_into_zips_">My JPEGs (or MP3's) don't compress when I put them into Zips!</a></h1>
<p><strong>Q:</strong> How come my JPEGs and MP3's don't compress much when I put them into Zips?</p>
<p><strong>A:</strong> Because they're already compressed.</p>
<p>
</p>
<hr />
<h1><a name="under_windows__things_lock_up_get_damaged">Under Windows, things lock up/get damaged</a></h1>
<p><strong>Q:</strong> I'm using Windows. When I try to use Archive::Zip, my machine locks up/makes
funny sounds/displays a BSOD/corrupts data. How can I fix this?</p>
<p><strong>A:</strong> First, try the newest version of Compress::Zlib. I know of
Windows-related problems prior to v1.14 of that library.</p>
<p>If that doesn't get rid of the problem, fix your computer or get rid of
Windows.</p>
<p>
</p>
<hr />
<h1><a name="zip_contents_in_a_scalar">Zip contents in a scalar</a></h1>
<p><strong>Q:</strong> I want to read a Zip file from (or write one to) a scalar variable instead
of a file. How can I do this?</p>
<p><strong>A:</strong> Use <code>IO::Scalar</code> and the <code>readFromFileHandle()</code> and
<code>writeToFileHandle()</code> methods.
See <code>examples/readScalar.pl</code> and <code>examples/writeScalar.pl</code>.</p>
<p>
</p>
<hr />
<h1><a name="reading_from_streams">Reading from streams</a></h1>
<p><strong>Q:</strong> How do I read from a stream (like for the Info-Zip <code>funzip</code> program)?</p>
<p><strong>A:</strong>	This isn't currently supported, though writing to a stream is.</p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?