tar.html

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

HTML
814
字号
<code>IO::Zlib</code> filehandle instead.</p>
<p>Specific levels of compression can be chosen by passing the values 2
through 9 as the second parameter.</p>
<p>The third argument is an optional prefix. All files will be tucked
away in the directory you specify as prefix. So if you have files
'a' and 'b' in your archive, and you specify 'foo' as prefix, they
will be written to the archive as 'foo/a' and 'foo/b'.</p>
<p>If no arguments are given, <a href="../../lib/Pod/perlfunc.html#item_write"><code>write</code></a> returns the entire formatted
archive as a string, which could be useful if you'd like to stuff the
archive into a socket or a pipe to gzip or something.</p>
<p>
</p>
<h2><a name="_tar_add_files___filenamelist__">$tar-&gt;add_files( @filenamelist )</a></h2>
<p>Takes a list of filenames and adds them to the in-memory archive.</p>
<p>The path to the file is automatically converted to a Unix like
equivalent for use in the archive, and, if on MacOS, the file's
modification time is converted from the MacOS epoch to the Unix epoch.
So tar archives created on MacOS with <strong>Archive::Tar</strong> can be read
both with <em>tar</em> on Unix and applications like <em>suntar</em> or
<em>Stuffit Expander</em> on MacOS.</p>
<p>Be aware that the file's type/creator and resource fork will be lost,
which is usually what you want in cross-platform archives.</p>
<p>Returns a list of <code>Archive::Tar::File</code> objects that were just added.</p>
<p>
</p>
<h2><a name="_tar_add_data____filename___data____opthashref___">$tar-&gt;add_data ( $filename, $data, [$opthashref] )</a></h2>
<p>Takes a filename, a scalar full of data and optionally a reference to
a hash with specific options.</p>
<p>Will add a file to the in-memory archive, with name <code>$filename</code> and
content <code>$data</code>. Specific properties can be set using <code>$opthashref</code>.
The following list of properties is supported: name, size, mtime
(last modified date), mode, uid, gid, linkname, uname, gname,
devmajor, devminor, prefix, type.  (On MacOS, the file's path and
modification times are converted to Unix equivalents.)</p>
<p>Valid values for the file type are the following constants defined in
Archive::Tar::Constants:</p>
<dl>
<dt><strong><a name="item_file">FILE</a></strong>

<dd>
<p>Regular file.</p>
</dd>
</li>
<dt><strong><a name="item_hardlink">HARDLINK</a></strong>

<dt><strong><a name="item_symlink">SYMLINK</a></strong>

<dd>
<p>Hard and symbolic (&quot;soft&quot;) links; linkname should specify target.</p>
</dd>
</li>
<dt><strong><a name="item_chardev">CHARDEV</a></strong>

<dt><strong><a name="item_blockdev">BLOCKDEV</a></strong>

<dd>
<p>Character and block devices. devmajor and devminor should specify the major
and minor device numbers.</p>
</dd>
</li>
<dt><strong><a name="item_dir">DIR</a></strong>

<dd>
<p>Directory.</p>
</dd>
</li>
<dt><strong><a name="item_fifo">FIFO</a></strong>

<dd>
<p>FIFO (named pipe).</p>
</dd>
</li>
<dt><strong><a name="item_socket">SOCKET</a></strong>

<dd>
<p>Socket.</p>
</dd>
</li>
</dl>
<p>Returns the <code>Archive::Tar::File</code> object that was just added, or
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on failure.</p>
<p>
</p>
<h2><a name="_tar_error____bool___">$tar-&gt;error( [$BOOL] )</a></h2>
<p>Returns the current errorstring (usually, the last error reported).
If a true value was specified, it will give the <code>Carp::longmess</code>
equivalent of the error, in effect giving you a stacktrace.</p>
<p>For backwards compatibility, this error is also available as
<code>$Archive::Tar::error</code> although it is much recommended you use the
method call instead.</p>
<p>
</p>
<h2><a name="_tar_setcwd___cwd___">$tar-&gt;setcwd( $cwd );</a></h2>
<p><code>Archive::Tar</code> needs to know the current directory, and it will run
<code>Cwd::cwd()</code> <em>every</em> time it extracts a <em>relative</em> entry from the 
tarfile and saves it in the file system. (As of version 1.30, however,
<code>Archive::Tar</code> will use the speed optimization described below 
automatically, so it's only relevant if you're using <code>extract_file()</code>).</p>
<p>Since <code>Archive::Tar</code> doesn't change the current directory internally
while it is extracting the items in a tarball, all calls to <code>Cwd::cwd()</code>
can be avoided if we can guarantee that the current directory doesn't
get changed externally.</p>
<p>To use this performance boost, set the current directory via</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Cwd</span><span class="operator">;</span>
    <span class="variable">$tar</span><span class="operator">-&gt;</span><span class="variable">setcwd</span><span class="operator">(</span> <span class="variable">cwd</span><span class="operator">()</span> <span class="operator">);</span>
</pre>
<p>once before calling a function like <code>extract_file</code> and
<code>Archive::Tar</code> will use the current directory setting from then on
and won't call <code>Cwd::cwd()</code> internally.</p>
<p>To switch back to the default behaviour, use</p>
<pre>
    <span class="variable">$tar</span><span class="operator">-&gt;</span><span class="variable">setcwd</span><span class="operator">(</span> <span class="keyword">undef</span> <span class="operator">);</span>
</pre>
<p>and <code>Archive::Tar</code> will call <code>Cwd::cwd()</code> internally again.</p>
<p>If you're using <code>Archive::Tar</code>'s <code>exract()</code> method, <code>setcwd()</code> will
be called for you.</p>
<p>
</p>
<h2><a name="_bool____tar_has_io_string">$bool = $tar-&gt;has_io_string</a></h2>
<p>Returns true if we currently have <code>IO::String</code> support loaded.</p>
<p>Either <code>IO::String</code> or <code>perlio</code> support is needed to support writing 
stringified archives. Currently, <code>perlio</code> is the preferred method, if
available.</p>
<p>See the <code>GLOBAL VARIABLES</code> section to see how to change this preference.</p>
<p>
</p>
<h2><a name="_bool____tar_has_perlio">$bool = $tar-&gt;has_perlio</a></h2>
<p>Returns true if we currently have <code>perlio</code> support loaded.</p>
<p>This requires <code>perl-5.8</code> or higher, compiled with <code>perlio</code></p>
<p>Either <code>IO::String</code> or <code>perlio</code> support is needed to support writing 
stringified archives. Currently, <code>perlio</code> is the preferred method, if
available.</p>
<p>See the <code>GLOBAL VARIABLES</code> section to see how to change this preference.</p>
<p>
</p>
<hr />
<h1><a name="class_methods">Class Methods</a></h1>
<p>
</p>
<h2><a name="archive__tar_create_archive__file___compression___filelist_">Archive::Tar-&gt;create_archive($file, $compression, @filelist)</a></h2>
<p>Creates a tar file from the list of files provided.  The first
argument can either be the name of the tar file to create or a
reference to an open file handle (e.g. a GLOB reference).</p>
<p>The second argument specifies the level of compression to be used, if
any.  Compression of tar files requires the installation of the
IO::Zlib module.  Specific levels of compression may be
requested by passing a value between 2 and 9 as the second argument.
Any other value evaluating as true will result in the default
compression level being used.</p>
<p>Note that when you pass in a filehandle, the compression argument
is ignored, as all files are printed verbatim to your filehandle.
If you wish to enable compression with filehandles, use an
<code>IO::Zlib</code> filehandle instead.</p>
<p>The remaining arguments list the files to be included in the tar file.
These files must all exist. Any files which don't exist or can't be
read are silently ignored.</p>
<p>If the archive creation fails for any reason, <code>create_archive</code> will
return false. Please use the <code>error</code> method to find the cause of the
failure.</p>
<p>Note that this method does not write <code>on the fly</code> as it were; it
still reads all the files into memory before writing out the archive.
Consult the FAQ below if this is a problem.</p>
<p>
</p>
<h2><a name="archive__tar_list_archive___file___compressed_____properties__">Archive::Tar-&gt;list_archive ($file, $compressed, [\@properties])</a></h2>
<p>Returns a list of the names of all the files in the archive.  The
first argument can either be the name of the tar file to list or a
reference to an open file handle (e.g. a GLOB reference).</p>
<p>If <code>list_archive()</code> is passed an array reference as its third
argument it returns a list of hash references containing the requested
properties of each file.  The following list of properties is
supported: full_path, name, size, mtime (last modified date), mode, 
uid, gid, linkname, uname, gname, devmajor, devminor, prefix.</p>
<p>See <code>Archive::Tar::File</code> for details about supported properties.</p>
<p>Passing an array reference containing only one element, 'name', is
special cased to return a list of names rather than a list of hash
references.</p>
<p>
</p>
<h2><a name="archive__tar_extract_archive___file___gzip_">Archive::Tar-&gt;extract_archive ($file, $gzip)</a></h2>
<p>Extracts the contents of the tar file.  The first argument can either
be the name of the tar file to create or a reference to an open file
handle (e.g. a GLOB reference).  All relative paths in the tar file will
be created underneath the current working directory.</p>
<p><code>extract_archive</code> will return a list of files it extracted.
If the archive extraction fails for any reason, <code>extract_archive</code>
will return false.  Please use the <code>error</code> method to find the cause
of the failure.</p>
<p>
</p>
<h2><a name="archive__tar_can_handle_compressed_files">Archive::Tar-&gt;can_handle_compressed_files</a></h2>
<p>A simple checking routine, which will return true if <code>Archive::Tar</code>
is able to uncompress compressed archives on the fly with <code>IO::Zlib</code>,
or false if <code>IO::Zlib</code> is not installed.</p>
<p>You can use this as a shortcut to determine whether <code>Archive::Tar</code>
will do what you think before passing compressed archives to its
<a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a> method.</p>
<p>
</p>
<hr />
<h1><a name="global_variables">GLOBAL VARIABLES</a></h1>
<p>
</p>
<h2><a name="_archive__tar__follow_symlink">$Archive::Tar::FOLLOW_SYMLINK</a></h2>
<p>Set this variable to <code>1</code> to make <code>Archive::Tar</code> effectively make a
copy of the file when extracting. Default is <code>0</code>, which
means the symlink stays intact. Of course, you will have to pack the
file linked to as well.</p>
<p>This option is checked when you write out the tarfile using <a href="../../lib/Pod/perlfunc.html#item_write"><code>write</code></a>
or <code>create_archive</code>.</p>
<p>This works just like <code>/bin/tar</code>'s <a href="../../lib/Pod/perlrun.html#item__2dh"><code>-h</code></a> option.</p>
<p>
</p>
<h2><a name="_archive__tar__chown">$Archive::Tar::CHOWN</a></h2>
<p>By default, <code>Archive::Tar</code> will try to <a href="../../lib/Pod/perlfunc.html#item_chown"><code>chown</code></a> your files if it is
able to. In some cases, this may not be desired. In that case, set
this variable to <code>0</code> to disable <a href="../../lib/Pod/perlfunc.html#item_chown"><code>chown</code></a>-ing, even if it were
possible.</p>
<p>The default is <code>1</code>.</p>
<p>
</p>
<h2><a name="_archive__tar__chmod">$Archive::Tar::CHMOD</a></h2>
<p>By default, <code>Archive::Tar</code> will try to <a href="../../lib/Pod/perlfunc.html#item_chmod"><code>chmod</code></a> your files to
whatever mode was specified for the particular file in the archive.
In some cases, this may not be desired. In that case, set this
variable to <code>0</code> to disable <a href="../../lib/Pod/perlfunc.html#item_chmod"><code>chmod</code></a>-ing.</p>
<p>The default is <code>1</code>.</p>
<p>
</p>
<h2><a name="_archive__tar__do_not_use_prefix">$Archive::Tar::DO_NOT_USE_PREFIX</a></h2>
<p>By default, <code>Archive::Tar</code> will try to put paths that are over 
100 characters in the <code>prefix</code> field of your tar header, as
defined per POSIX-standard. However, some (older) tar programs 
do not implement this spec. To retain compatibility with these older 
or non-POSIX compliant versions, you can set the <code>$DO_NOT_USE_PREFIX</code> 
variable to a true value, and <code>Archive::Tar</code> will use an alternate 
way of dealing with paths over 100 characters by using the 
<code>GNU Extended Header</code> feature.</p>
<p>Note that clients who do not support the <code>GNU Extended Header</code>
feature will not be able to read these archives. Such clients include
tars on <code>Solaris</code>, <code>Irix</code> and <code>AIX</code>.</p>
<p>The default is <code>0</code>.</p>
<p>
</p>
<h2><a name="_archive__tar__debug">$Archive::Tar::DEBUG</a></h2>
<p>Set this variable to <code>1</code> to always get the <code>Carp::longmess</code> output
of the warnings, instead of the regular <code>carp</code>. This is the same
message you would get by doing:</p>
<pre>
    <span class="variable">$tar</span><span class="operator">-&gt;</span><span class="variable">error</span><span class="operator">(</span><span class="number">1</span><span class="operator">);</span>
</pre>
<p>Defaults to <code>0</code>.</p>
<p>
</p>
<h2><a name="_archive__tar__warn">$Archive::Tar::WARN</a></h2>
<p>Set this variable to <code>0</code> if you do not want any warnings printed.
Personally I recommend against doing this, but people asked for the
option. Also, be advised that this is of course not threadsafe.</p>
<p>Defaults to <code>1</code>.</p>
<p>
</p>
<h2><a name="_archive__tar__error">$Archive::Tar::error</a></h2>
<p>Holds the last reported error. Kept for historical reasons, but its
use is very much discouraged. Use the <code>error()</code> method instead:</p>
<pre>
    <span class="keyword">warn</span> <span class="variable">$tar</span><span class="operator">-&gt;</span><span class="variable">error</span> <span class="keyword">unless</span> <span class="variable">$tar</span><span class="operator">-&gt;</span><span class="variable">extract</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="_archive__tar__has_perlio">$Archive::Tar::HAS_PERLIO</a></h2>
<p>This variable holds a boolean indicating if we currently have 

⌨️ 快捷键说明

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