use Archive::Tar; my $tar = Archive::Tar->new; $tar->read('origin.tgz'); $tar->extract(); $tar->add_files('file/foo.pl', 'docs/README'); $tar->add_data('file/baz.txt', 'This is the contents now'); $tar->rename('oldname', 'new/file/name'); $tar->chown('/', 'root'); $tar->chown('/', 'root:root'); $tar->chmod('/tmp', '1777'); $tar->write('files.tar'); # plain tar $tar->write('files.tgz', COMPRESS_GZIP); # gzip compressed $tar->write('files.tbz', COMPRESS_BZIP); # bzip2 compressed $tar->write('files.txz', COMPRESS_XZ); # xz compressed
An object of class Archive::Tar represents a .tar(.gz) archive full of files and things.
If "new()" is invoked with arguments and the "read()" method fails for any reason, "new()" returns undef.
The "read" will replace any previous content in $tar!
The second argument may be considered optional, but remains for backwards compatibility. Archive::Tar now looks at the file magic to determine what class should be used to open the file and will transparently Do The Right Thing.
Archive::Tar will warn if you try to pass a bzip2 / xz compressed file and the IO::Uncompress::Bunzip2 / IO::Uncompress::UnXz are not available and simply return.
Note that you can currently not pass a "gzip" compressed filehandle, which is not opened with "IO::Zlib", a "bzip2" compressed filehandle, which is not opened with "IO::Uncompress::Bunzip2", a "xz" compressed filehandle, which is not opened with "IO::Uncompress::UnXz", nor a string containing the full archive information (either compressed or uncompressed). These are worth while features, but not currently implemented. See the "TODO" section.
The third argument can be a hash reference with options. Note that all options are case-sensitive.
All files are stored internally as "Archive::Tar::File" objects. Please consult the Archive::Tar::File documentation for details.
Returns the number of files read in scalar context, and a list of "Archive::Tar::File" objects in list context.
Note however, that this function does an exact match using "eq" on the full path. So it cannot compensate for case-insensitive file- systems or compare 2 paths to see if they would point to the same underlying file.
If "extract" is called without a list of file names, the entire contents of the archive are extracted.
Returns a list of filenames extracted.
For example:
$tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' ); $tar->extract_file( $at_file_object, 'name/i/want/to/give/it' );
Returns true on success, false on failure.
If "list_files()" is passed an array reference as its first argument it returns a list of hash references containing the requested properties of each file. The following list of properties is supported: name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix.
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, making it equivalent to calling "list_files" without arguments.
Please refer to the "Archive::Tar::File" documentation on how to handle these objects.
Note that you must specify a Unix path for $new_name, since per tar standard, all files in the archive must be Unix paths.
Returns true on success and false on failure.
Returns true on success and false on failure.
Returns true on success and false on failure.
The second argument is used to indicate compression. You can compress using "gzip", "bzip2" or "xz". If you pass a digit, it's assumed to be the "gzip" compression level (between 1 and 9), but the use of constants is preferred:
# write a gzip compressed file $tar->write( 'out.tgz', COMPRESS_GZIP ); # write a bzip compressed file $tar->write( 'out.tbz', COMPRESS_BZIP ); # write a xz compressed file $tar->write( 'out.txz', COMPRESS_XZ );
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 "IO::Zlib", "IO::Compress::Bzip2" or "IO::Compress::Xz" filehandle instead.
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'.
If no arguments are given, "write" 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.
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 Archive::Tar can be read both with tar on Unix and applications like suntar or Stuffit Expander on MacOS.
Be aware that the file's type/creator and resource fork will be lost, which is usually what you want in cross-platform archives.
Instead of a filename, you can also pass it an existing "Archive::Tar::File" object from, for example, another archive. The object will be clone, and effectively be a copy of the original, not an alias.
Returns a list of "Archive::Tar::File" objects that were just added.
Will add a file to the in-memory archive, with name $filename and content $data. Specific properties can be set using $opthashref. 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.)
Valid values for the file type are the following constants defined by Archive::Tar::Constant:
Returns the "Archive::Tar::File" object that was just added, or "undef" on failure.
For backwards compatibility, this error is also available as $Archive::Tar::error although it is much recommended you use the method call instead.
Since "Archive::Tar" doesn't change the current directory internally while it is extracting the items in a tarball, all calls to "Cwd::cwd()" can be avoided if we can guarantee that the current directory doesn't get changed externally.
To use this performance boost, set the current directory via
use Cwd; $tar->setcwd( cwd() );
once before calling a function like "extract_file" and "Archive::Tar" will use the current directory setting from then on and won't call "Cwd::cwd()" internally.
To switch back to the default behaviour, use
$tar->setcwd( undef );
and "Archive::Tar" will call "Cwd::cwd()" internally again.
If you're using "Archive::Tar"'s "extract()" method, "setcwd()" will be called for you.
The second argument is used to indicate compression. You can compress using "gzip", "bzip2" or "xz". If you pass a digit, it's assumed to be the "gzip" compression level (between 1 and 9), but the use of constants is preferred:
# write a gzip compressed file Archive::Tar->create_archive( 'out.tgz', COMPRESS_GZIP, @filelist ); # write a bzip compressed file Archive::Tar->create_archive( 'out.tbz', COMPRESS_BZIP, @filelist ); # write a xz compressed file Archive::Tar->create_archive( 'out.txz', COMPRESS_XZ, @filelist );
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 "IO::Zlib", "IO::Compress::Bzip2" or "IO::Compress::Xz" 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 be read are silently ignored.
If the archive creation fails for any reason, "create_archive" will return false. Please use the "error" method to find the cause of the failure.
Note that this method does not write "on the fly" 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.
The second argument can be a hash reference with options, which are identical to the arguments passed to "read()".
Example usage:
my $next = Archive::Tar->iter( "example.tar.gz", 1, {filter => qr/\.pm$/} ); while( my $f = $next->() ) { print $f->name, "\n"; $f->extract or warn "Extraction failed"; # .... }
If "list_archive()" 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, type.
See "Archive::Tar::File" for details about supported properties.
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.
"extract_archive" will return a list of files it extracted. If the archive extraction fails for any reason, "extract_archive" will return false. Please use the "error" method to find the cause of the failure.
Either "IO::String" or "perlio" support is needed to support writing stringified archives. Currently, "perlio" is the preferred method, if available.
See the "GLOBAL VARIABLES" section to see how to change this preference.
This requires "perl-5.8" or higher, compiled with "perlio"
Either "IO::String" or "perlio" support is needed to support writing stringified archives. Currently, "perlio" is the preferred method, if available.
See the "GLOBAL VARIABLES" section to see how to change this preference.
You can use this as a shortcut to determine whether "Archive::Tar" will do what you think before passing compressed archives to its "read" method.
This option is checked when you write out the tarfile using "write" or "create_archive".
This works just like "/bin/tar"'s "-h" option.
The default is 1 for the root user and 0 for normal users.
Note that clients who do not support the "GNU Extended Header" feature will not be able to read these archives. Such clients include tars on "Solaris", "Irix" and "AIX".
$tar->error(1);
warn $tar->error unless $tar->extract;
Note that in older versions of this module, the "error()" method would return an effectively global value even when called an instance method as above. This has since been fixed, and multiple instances of "Archive::Tar" now have separate error strings.
Allowing this could have security implications, as a malicious tar archive could alter or replace any file the extracting user has 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 "true".
Note that this is a backwards incompatible change from version 1.36 and before.
If you feel strongly about disabling it, set this variable to "false". Note that you will then need "IO::String" installed to support writing stringified archives.
Don't change this variable unless you really know what you're doing.
If you feel strongly about disabling it, set this variable to "false". Note that you will then need "perlio" support from your perl to be able to write stringified archives.
Don't change this variable unless you really know what you're doing.
You can tune the behaviour by setting the $Archive::Tar::RESOLVE_SYMLINK variable, or $ENV{PERL5_AT_RESOLVE_SYMLINK} before loading the module Archive::Tar. Values can be one of the following: none Disable this mechanism and failed as it was in previous version (<1.88) speed (default) If you prefer speed this will read again the whole archive using read() so all entries will be available memory If you prefer memory Limitation It won't work for terminal, pipe or sockets or every non seekable source.
If you just want to extract, use the "extract_archive" class method instead. It will optimize and write to disk immediately.
Another option is to use the "iter" class method to iterate over the files in the tarball without reading them all in memory at once.
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, including "iter" on archives that have incompatible filetypes and still expect things to work).
For other filetypes, like "chardevs" and "blockdevs" we'll warn that the extraction of this particular item didn't work.
Note that GNU tar earlier than version 1.14 does not cope well with the "POSIX header prefix". If you use such a version, consider setting the $Archive::Tar::DO_NOT_USE_PREFIX variable to "true".
You can do this by filtering a list of "Archive::Tar::File" objects based on your criteria. For example, to extract only files that have the string "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 "Archive::Tar::File" documentation on how to use these objects.
If the "uncompress" or "gunzip" programs are available, you can use one of these workarounds to read ".tar.Z" files from "Archive::Tar"
Firstly with "uncompress"
use Archive::Tar; open F, "uncompress -c $filename |"; my $tar = Archive::Tar->new(*F); ...
and this with "gunzip"
use Archive::Tar; open F, "gunzip -c $filename |"; my $tar = Archive::Tar->new(*F); ...
Similarly, if the "compress" program is available, you can use this to write a ".tar.Z" file
use Archive::Tar; use IO::File; my $fh = IO::File->new( "| compress -c >$filename" ); my $tar = Archive::Tar->new(); ... $tar->write($fh); $fh->close ;
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 out to disk via "$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 ":utf8" line discipline wasn't set by "Archive::Tar", so Perl tried to convert the string to ISO-8859 and failed. The written file now contains garbage.
For this reason, Unicode strings need to be converted to UTF-8-encoded bytestrings before they are handed off to "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 "get_content()" on the "Archive::Tar::File" object will return its content as a bytestring, not as a Unicode string.
If you want it to be a Unicode string (because you want character semantics with operations like regular expression matching), you need to 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 "Archive::Tar", because a tarball can contain many files, and each of which could be encoded in a different way.
Invalid header block at offset nnn
A fix for that problem is scheduled to be released in the following levels of AIX, all of which should be coming out in the 4th quarter of 2009:
AIX 5.3 TL7 SP10 AIX 5.3 TL8 SP8 AIX 5.3 TL9 SP5 AIX 5.3 TL10 SP2 AIX 6.1 TL0 SP11 AIX 6.1 TL1 SP7 AIX 6.1 TL2 SP6 AIX 6.1 TL3 SP3
The IBM APAR number for this problem is IZ50240 (Reported component ID: 5765G0300 / AIX 5.3). It is possible to get an ifix for that problem. If you need an ifix please contact your local IBM AIX support.
Please reports bugs to <bug-archive-tar@rt.cpan.org>.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.