📄 tar.pm
字号:
C<IO::Zlib> filehandle instead.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 beread are silently ignored.If the archive creation fails for any reason, C<create_archive> willreturn false. Please use the C<error> method to find the cause of thefailure.Note that this method does not write C<on the fly> as it were; itstill reads all the files into memory before writing out the archive.Consult the FAQ below if this is a problem.=cutsub create_archive { my $class = shift; my $file = shift; return unless defined $file; my $gzip = shift || 0; my @files = @_; unless( @files ) { return $class->_error( qq[Cowardly refusing to create empty archive!] ); } my $tar = $class->new; $tar->add_files( @files ); return $tar->write( $file, $gzip );}=head2 Archive::Tar->list_archive ($file, $compressed, [\@properties])Returns a list of the names of all the files in the archive. Thefirst argument can either be the name of the tar file to list or areference to an open file handle (e.g. a GLOB reference).If C<list_archive()> is passed an array reference as its thirdargument it returns a list of hash references containing the requestedproperties of each file. The following list of properties issupported: full_path, name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix.See C<Archive::Tar::File> for details about supported properties.Passing an array reference containing only one element, 'name', isspecial cased to return a list of names rather than a list of hashreferences.=cutsub list_archive { my $class = shift; my $file = shift; return unless defined $file; my $gzip = shift || 0; my $tar = $class->new($file, $gzip); return unless $tar; return $tar->list_files( @_ );}=head2 Archive::Tar->extract_archive ($file, $gzip)Extracts the contents of the tar file. The first argument can eitherbe the name of the tar file to create or a reference to an open filehandle (e.g. a GLOB reference). All relative paths in the tar file willbe created underneath the current working directory.C<extract_archive> will return a list of files it extracted.If the archive extraction fails for any reason, C<extract_archive>will return false. Please use the C<error> method to find the causeof the failure.=cutsub extract_archive { my $class = shift; my $file = shift; return unless defined $file; my $gzip = shift || 0; my $tar = $class->new( ) or return; return $tar->read( $file, $gzip, { extract => 1 } );}=head2 Archive::Tar->can_handle_compressed_filesA simple checking routine, which will return true if C<Archive::Tar>is able to uncompress compressed archives on the fly with C<IO::Zlib>,or false if C<IO::Zlib> is not installed.You can use this as a shortcut to determine whether C<Archive::Tar>will do what you think before passing compressed archives to itsC<read> method.=cutsub can_handle_compressed_files { return ZLIB ? 1 : 0 }sub no_string_support { croak("You have to install IO::String to support writing archives to strings");}1;__END__=head1 GLOBAL VARIABLES=head2 $Archive::Tar::FOLLOW_SYMLINKSet this variable to C<1> to make C<Archive::Tar> effectively make acopy of the file when extracting. Default is C<0>, whichmeans the symlink stays intact. Of course, you will have to pack thefile linked to as well.This option is checked when you write out the tarfile using C<write>or C<create_archive>.This works just like C</bin/tar>'s C<-h> option.=head2 $Archive::Tar::CHOWNBy default, C<Archive::Tar> will try to C<chown> your files if it isable to. In some cases, this may not be desired. In that case, setthis variable to C<0> to disable C<chown>-ing, even if it werepossible.The default is C<1>.=head2 $Archive::Tar::CHMODBy default, C<Archive::Tar> will try to C<chmod> your files towhatever mode was specified for the particular file in the archive.In some cases, this may not be desired. In that case, set thisvariable to C<0> to disable C<chmod>-ing.The default is C<1>.=head2 $Archive::Tar::DO_NOT_USE_PREFIXBy default, C<Archive::Tar> will try to put paths that are over 100 characters in the C<prefix> field of your tar header, asdefined 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 C<$DO_NOT_USE_PREFIX> variable to a true value, and C<Archive::Tar> will use an alternate way of dealing with paths over 100 characters by using the C<GNU Extended Header> feature.Note that clients who do not support the C<GNU Extended Header>feature will not be able to read these archives. Such clients includetars on C<Solaris>, C<Irix> and C<AIX>.The default is C<0>.=head2 $Archive::Tar::DEBUGSet this variable to C<1> to always get the C<Carp::longmess> outputof the warnings, instead of the regular C<carp>. This is the samemessage you would get by doing: $tar->error(1);Defaults to C<0>.=head2 $Archive::Tar::WARNSet this variable to C<0> if you do not want any warnings printed.Personally I recommend against doing this, but people asked for theoption. Also, be advised that this is of course not threadsafe.Defaults to C<1>.=head2 $Archive::Tar::errorHolds the last reported error. Kept for historical reasons, but itsuse is very much discouraged. Use the C<error()> method instead: warn $tar->error unless $tar->extract;=head2 $Archive::Tar::INSECURE_EXTRACT_MODEThis variable indicates whether C<Archive::Tar> should allowfiles to be extracted outside their current working directory.Allowing this could have security implications, as a malicioustar archive could alter or replace any file the extracting userhas permissions to. Therefor, the default is to not allow insecure extractions. If you trust the archive, or have other reasons to allow the archive to write files outside your current working directory, set this variable to C<true>.Note that this is a backwards incompatible change from versionC<1.36> and before.=head2 $Archive::Tar::HAS_PERLIOThis variable holds a boolean indicating if we currently have C<perlio> support loaded. This will be enabled for any perlgreater than C<5.8> compiled with C<perlio>. If you feel strongly about disabling it, set this variable toC<false>. Note that you will then need C<IO::String> installedto support writing stringified archives.Don't change this variable unless you B<really> know what you'redoing.=head2 $Archive::Tar::HAS_IO_STRINGThis variable holds a boolean indicating if we currently have C<IO::String> support loaded. This will be enabled for any perlthat has a loadable C<IO::String> module.If you feel strongly about disabling it, set this variable toC<false>. Note that you will then need C<perlio> support fromyour perl to be able to write stringified archives.Don't change this variable unless you B<really> know what you'redoing.=head1 FAQ=over 4=item What's the minimum perl version required to run Archive::Tar?You will need perl version 5.005_03 or newer.=item Isn't Archive::Tar slow?Yes it is. It's pure perl, so it's a lot slower then your C</bin/tar>However, it's very portable. If speed is an issue, consider usingC</bin/tar> instead.=item Isn't Archive::Tar heavier on memory than /bin/tar?Yes it is, see previous answer. Since C<Compress::Zlib> and thereforeC<IO::Zlib> doesn't support C<seek> on their filehandles, there is littlechoice but to read the archive into memory.This is ok if you want to do in-memory manipulation of the archive.If you just want to extract, use the C<extract_archive> class methodinstead. It will optimize and write to disk immediately.=item Can't you lazy-load data instead?No, not easily. See previous question.=item How much memory will an X kb tar file need?Probably more than X kb, since it will all be read into memory. Ifthis is a problem, and you don't need to do in memory manipulationof the archive, consider using C</bin/tar> instead.=item What do you do with unsupported filetypes in an archive?C<Unix> has a few filetypes that aren't supported on other platforms,like C<Win32>. If we encounter a C<hardlink> or C<symlink> we'll justtry to make a copy of the original file, rather than throwing an error.This does require you to read the entire archive in to memory first,since otherwise we wouldn't know what data to fill the copy with.(This means that you cannot use the class methods on archives thathave incompatible filetypes and still expect things to work).For other filetypes, like C<chardevs> and C<blockdevs> we'll warn thatthe extraction of this particular item didn't work.=item I'm using WinZip, or some other non-POSIX client, and files are not being extracted properly!By default, C<Archive::Tar> is in a completely POSIX-compatiblemode, which uses the POSIX-specification of C<tar> to store files.For paths greather than 100 characters, this is done using theC<POSIX header prefix>. Non-POSIX-compatible clients may not supportthis part of the specification, and may only support the C<GNU ExtendedHeader> functionality. To facilitate those clients, you can set theC<$Archive::Tar::DO_NOT_USE_PREFIX> variable to C<true>. See the C<GLOBAL VARIABLES> section for details on this variable.Note that GNU tar earlier than version 1.14 does not cope well withthe C<POSIX header prefix>. If you use such a version, consider settingthe C<$Archive::Tar::DO_NOT_USE_PREFIX> variable to C<true>.=item How do I extract only files that have property X from an archive?Sometimes, you might not wish to extract a complete archive, justthe files that are relevant to you, based on some criteria.You can do this by filtering a list of C<Archive::Tar::File> objectsbased on your criteria. For example, to extract only files that havethe string C<foo> in their title, you would use: $tar->extract( grep { $_->full_path =~ /foo/ } $tar->get_files ); This way, you can filter on any attribute of the files in the archive.Consult the C<Archive::Tar::File> documentation on how to use theseobjects.=item How do I access .tar.Z files?The C<Archive::Tar> module can optionally use C<Compress::Zlib> (viathe C<IO::Zlib> module) to access tar files that have been compressedwith C<gzip>. Unfortunately tar files compressed with the Unix C<compress>utility cannot be read by C<Compress::Zlib> and so cannot be directlyaccesses by C<Archive::Tar>.If the C<uncompress> or C<gunzip> programs are available, you can useone of these workarounds to read C<.tar.Z> files from C<Archive::Tar>Firstly with C<uncompress> use Archive::Tar; open F, "uncompress -c $filename |"; my $tar = Archive::Tar->new(*F); ...and this with C<gunzip> use Archive::Tar; open F, "gunzip -c $filename |"; my $tar = Archive::Tar->new(*F); ...Similarly, if the C<compress> program is available, you can use this towrite a C<.tar.Z> file use Archive::Tar; use IO::File; my $fh = new IO::File "| compress -c >$filename"; my $tar = Archive::Tar->new(); ... $tar->write($fh); $fh->close ;=item How do I handle Unicode strings?C<Archive::Tar> uses byte semantics for any files it reads from or writesto disk. This is not a problem if you only deal with files and neverlook at their content or work solely with byte strings. But if you useUnicode strings with character semantics, some additional steps needto be taken.For example, if you add a Unicode string like # Problem $tar->add_data('file.txt', "Euro: \x{20AC}");then there will be a problem later when the tarfile gets written outto disk via C<$tar->write()>: Wide character in print at .../Archive/Tar.pm line 1014.The data was added as a Unicode string and when writing it out to disk,the C<:utf8> line discipline wasn't set by C<Archive::Tar>, so Perltried to convert the string to ISO-8859 and failed. The written filenow contains garbage.For this reason, Unicode strings need to be converted to UTF-8-encodedbytestrings before they are handed off to C<add_data()>: use Encode; my $data = "Accented character: \x{20AC}"; $data = encode('utf8', $data); $tar->add_data('file.txt', $data);A opposite problem occurs if you extract a UTF8-encoded file from a tarball. Using C<get_content()> on the C<Archive::Tar::File> objectwill return its content as a bytestring, not as a Unicode string.If you want it to be a Unicode string (because you want charactersemantics with operations like regular expression matching), you needto decode the UTF8-encoded content and have Perl convert it into a Unicode string: use Encode; my $data = $tar->get_content(); # Make it a Unicode string $data = decode('utf8', $data);There is no easy way to provide this functionality in C<Archive::Tar>, because a tarball can contain many files, and each of which could beencoded in a different way.=back=head1 TODO=over 4=item Check if passed in handles are open for read/writeCurrently I don't know of any portable pure perl way to do this.Suggestions welcome.=item Allow archives to be passed in as stringCurrently, we only allow opened filehandles or filenames, butnot strings. The internals would need some reworking to facilitatestringified archives.=item Facilitate processing an opened filehandle of a compressed archiveCurrently, we only support this if the filehandle is an IO::Zlib object.Environments, like apache, will present you with an opened filehandleto an uploaded file, which might be a compressed archive.=back=head1 SEE ALSO=over 4=item The GNU tar specificationC<http://www.gnu.org/software/tar/manual/tar.html>=item The PAX format specicationThe specifcation which tar derives from; C< http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html>=item A comparison of GNU and POSIX tar standards; C<http://www.delorie.com/gnu/docs/tar/tar_114.html>=item GNU tar intends to switch to POSIX compatibilityGNU Tar authors have expressed their intention to become completelyPOSIX-compatible; C<http://www.gnu.org/software/tar/manual/html_node/Formats.html>=item A Comparison between various tar implementationsLists known issues and incompatibilities; C<http://gd.tuwien.ac.at/utils/archivers/star/README.otherbugs>=back=head1 AUTHORThis module by Jos Boumans E<lt>kane@cpan.orgE<gt>.Please reports bugs to E<lt>bug-archive-tar@rt.cpan.orgE<gt>.=head1 ACKNOWLEDGEMENTSThanks to Sean Burke, Chris Nandor, Chip Salzenberg, Tim Heaney andespecially Andrew Savige for their help and suggestions.=head1 COPYRIGHTThis module is copyright (c) 2002 - 2007 Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -