man Archive::Tar () - module for manipulations of tar archives
NAME
Archive::Tar - module for manipulations of tar archives
SYNOPSIS
use Archive::Tar; my $tar = Archive::Tar->new;
$tar->read('origin.tgz',1); $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->write('files.tar');
DESCRIPTION
Archive::Tar provides an object oriented mechanism for handling tar files. It provides class methods for quick and easy files handling while also allowing for the creation of tar file objects for custom manipulation. If you have the IO::Zlib module installed, Archive::Tar will also support compressed or gzipped tar files.
An object of class Archive::Tar represents a .tar(.gz) archive full of files and things.
Object Methods
Returns a new Tar object. If given any arguments, CWnew() calls the CWread() method automatically, passing on the arguments provided to the CWread() method.
If CWnew() is invoked with arguments and the CWread() method fails for any reason, CWnew() returns undef. Read the given tar file into memory. The first argument can either be the name of a file or a reference to an already open filehandle (or an IO::Zlib object if it's compressed) The second argument indicates whether the file referenced by the first argument is compressed.
The CWread will replace any previous content in CW$tar!
The second argument may be considered optional if IO::Zlib is installed, since it will transparently Do The Right Thing. Archive::Tar will warn if you try to pass a compressed file if IO::Zlib is not available and simply return.
The third argument can be a hash reference with options. Note that all options are case-sensitive.
- limit
- Do not read more than CWlimit files. This is useful if you have very big archives, and are only interested in the first few files.
- extract
- If set to true, immediately extract entries when reading them. This gives you the same memory break as the CWextract_archive function. Note however that entries will not be read into memory, but written straight to disk.
All files are stored internally as CWArchive::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 CWArchive::Tar::File objects in list context. Check if the archive contains a certain file. It will return true if the file is in the archive, false otherwise.
Note however, that this function does an exact match using CWeq 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.
$tar->extract( [@filenames] )
Write files whose names are equivalent to any of the names in CW@filenames to disk, creating subdirectories as necessary. This might not work too well under VMS. Under MacPerl, the file's modification time will be converted to the MacOS zero of time, and appropriate conversions will be done to the path. However, the length of each element of the path is not inspected to see whether it's longer than MacOS currently allows (32 characters).
If CWextract is called without a list of file names, the entire contents of the archive are extracted.
Returns a list of filenames extracted. Write an entry, whose name is equivalent to the file name provided to disk. Optionally takes a second parameter, which is the full (unix) path (including filename) the entry will be written to.
For example:
$tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' );
Returns true on success, false on failure.
$tar->list_files( [\@properties] )
Returns a list of the names of all the files in the archive.
If CWlist_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 CWlist_files without arguments.
$tar->get_files( [@filenames] )
Returns the CWArchive::Tar::File objects matching the filenames provided. If no filename list was passed, all CWArchive::Tar::File objects in the current Tar object are returned.
Please refer to the CWArchive::Tar::File documentation on how to handle these objects. Return the content of the named file. Make the string CW$content be the content for the file named CW$file. Rename the file of the in-memory archive to CW$new_name.
Note that you must specify a Unix path for CW$new_name, since per tar standard, all files in the archive must be Unix paths.
Returns true on success and false on failure.
$tar->remove (@filenamelist)
Removes any entries with names matching any of the given filenames from the in-memory archive. Returns a list of CWArchive::Tar::File objects that remain.
$tar->clear
CWclear clears the current in-memory archive. This effectively gives you a 'blank' object, ready to be filled again. Note that CWclear only has effect on the object, not the underlying tarfile. Write the in-memory archive to disk. The first argument can either be the name of a file or a reference to an already open filehandle (a GLOB reference). If the second argument is true, the module will use IO::Zlib to write the file in a compressed format. If IO::Zlib is not available, the CWwrite method will fail and return.
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 CWIO::Zlib filehandle instead.
Specific levels of compression can be chosen by passing the values 2 through 9 as the second parameter.
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, CWwrite 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. Takes a list of filenames and adds them to the in-memory archive.
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.
Returns a list of CWArchive::Tar::File objects that were just added. Takes a filename, a scalar full of data and optionally a reference to a hash with specific options.
Will add a file to the in-memory archive, with name CW$filename and content CW$data. Specific properties can be set using CW$opthashref. The following list of properties is supported: name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix. (On MacOS, the file's path and modification times are converted to Unix equivalents.)
Returns the CWArchive::Tar::File object that was just added, or CWundef on failure.
$tar->error( [$BOOL] )
Returns the current errorstring (usually, the last error reported). If a true value was specified, it will give the CWCarp::longmess equivalent of the error, in effect giving you a stacktrace.
For backwards compatibility, this error is also available as CW$Archive::Tar::error although it is much recommended you use the method call instead. Returns true if we currently have CWIO::String support loaded.
Either CWIO::String or CWperlio support is needed to support writing stringified archives. Currently, CWperlio is the preffered method, if available.
See the CWGLOBAL VARIABLES section to see how to change this preference. Returns true if we currently have CWperlio support loaded.
This requires CWperl-5.8 or higher, compiled with CWperlio
Either CWIO::String or CWperlio support is needed to support writing stringified archives. Currently, CWperlio is the preffered method, if available.
See the CWGLOBAL VARIABLES section to see how to change this preference.
Class Methods
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).
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.
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 CWIO::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 be read are silently ignored.
If the archive creation fails for any reason, CWcreate_archive will return false. Please use the CWerror method to find the cause of the failure.
Note that this method does not write CWon 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. 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).
If CWlist_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: 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. 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.
CWextract_archive will return a list of files it extracted. If the archive extraction fails for any reason, CWextract_archive will return false. Please use the CWerror method to find the cause of the failure.
Archive::Tar->can_handle_compressed_files
A simple checking routine, which will return true if CWArchive::Tar is able to uncompress compressed archives on the fly with CWIO::Zlib, or false if CWIO::Zlib is not installed.
You can use this as a shortcut to determine whether CWArchive::Tar will do what you think before passing compressed archives to its CWread method.
GLOBAL VARIABLES
$Archive::Tar::FOLLOW_SYMLINK
Set this variable to CW1 to make CWArchive::Tar effectively make a copy of the file when extracting. Default is CW0, which means the symlink stays intact. Of course, you will have to pack the file linked to as well.
This option is checked when you write out the tarfile using CWwrite or CWcreate_archive.
This works just like CW/bin/tar's CW-h option.
$Archive::Tar::CHOWN
By default, CWArchive::Tar will try to CWchown your files if it is able to. In some cases, this may not be desired. In that case, set this variable to CW0 to disable CWchown-ing, even if it were possible.
The default is CW1.
$Archive::Tar::CHMOD
By default, CWArchive::Tar will try to CWchmod 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 CW0 to disable CWchmod-ing.
The default is CW1.
$Archive::Tar::DO_NOT_USE_PREFIX
By default, CWArchive::Tar will try to put paths that are over 100 characters in the CWprefix field of your tar header. However, some older tar programs do not implement this spec. To retain compatibility with these older versions, you can set the CW$DO_NOT_USE_PREFIX variable to a true value, and CWArchive::Tar will use an alternate way of dealing with paths over 100 characters by using the CWGNU Extended Header feature.
The default is CW0.
$Archive::Tar::DEBUG
Set this variable to CW1 to always get the CWCarp::longmess output of the warnings, instead of the regular CWcarp. This is the same message you would get by doing:
$tar->error(1);
Defaults to CW0.
$Archive::Tar::WARN
Set this variable to CW0 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.
Defaults to CW1.
$Archive::Tar::error
Holds the last reported error. Kept for historical reasons, but its use is very much discouraged. Use the CWerror() method instead:
warn $tar->error unless $tar->extract;
$Archive::Tar::HAS_PERLIO
This variable holds a boolean indicating if we currently have CWperlio support loaded. This will be enabled for any perl greater than CW5.8 compiled with CWperlio.
If you feel strongly about disabling it, set this variable to CWfalse. Note that you will then need CWIO::String installed to support writing stringified archives.
Don't change this variable unless you really know what you're doing.
$Archive::Tar::HAS_IO_STRING
This variable holds a boolean indicating if we currently have CWIO::String support loaded. This will be enabled for any perl that has a loadable CWIO::String module.
If you feel strongly about disabling it, set this variable to CWfalse. Note that you will then need CWperlio support from your perl to be able to write stringified archives.
Don't change this variable unless you really know what you're doing.
FAQ
- What's the minimum perl version required to run Archive::Tar?
- You will need perl version 5.005_03 or newer.
- Isn't Archive::Tar slow?
- Yes it is. It's pure perl, so it's a lot slower then your CW/bin/tar However, it's very portable. If speed is an issue, consider using CW/bin/tar instead.
- Isn't Archive::Tar heavier on memory than /bin/tar?
- Yes it is, see previous answer. Since CWCompress::Zlib and therefore CWIO::Zlib doesn't support CWseek on their filehandles, there is little choice 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 CWextract_archive class method instead. It will optimize and write to disk immediately.
- Can't you lazy-load data instead?
- No, not easily. See previous question.
- How much memory will an X kb tar file need?
- Probably more than X kb, since it will all be read into memory. If this is a problem, and you don't need to do in memory manipulation of the archive, consider using CW/bin/tar instead.
- What do you do with unsupported filetypes in an archive?
- CWUnix has a few filetypes that aren't supported on other platforms, like CWWin32. If we encounter a CWhardlink or CWsymlink we'll just try 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 that have incompatible filetypes and still expect things to work). For other filetypes, like CWchardevs and CWblockdevs we'll warn that the extraction of this particular item didn't work.
- How do I access .tar.Z files?
-
The CWArchive::Tar module can optionally use CWCompress::Zlib (via
the CWIO::Zlib module) to access tar files that have been compressed
with CWgzip. Unfortunately tar files compressed with the Unix CWcompress
utility cannot be read by CWCompress::Zlib and so cannot be directly
accesses by CWArchive::Tar.
If the CWuncompress or CWgunzip programs are available, you can use
one of these workarounds to read CW.tar.Z files from CWArchive::Tar
Firstly with CWuncompress
use Archive::Tar;
open F, "uncompress -c $filename |"; my $tar = Archive::Tar->new(*F); ...
and this with CWgunzipuse Archive::Tar;
open F, "gunzip -c $filename |"; my $tar = Archive::Tar->new(*F); ...
Similarly, if the CWcompress program is available, you can use this to write a CW.tar.Z fileuse Archive::Tar; use IO::File;
my $fh = new IO::File "| compress -c >$filename"; my $tar = Archive::Tar->new(); ... $tar->write($fh); $fh->close ;
TODO
- Check if passed in handles are open for read/write
- Currently I don't know of any portable pure perl way to do this. Suggestions welcome.
AUTHOR
This module by Jos Boumans <kane@cpan.org>.
ACKNOWLEDGEMENTS
Thanks to Sean Burke, Chris Nandor, Chip Salzenberg, Tim Heaney and especially Andrew Savige for their help and suggestions.
COPYRIGHT
This module is copyright (c) 2002 Jos Boumans <kane@cpan.org>. All rights reserved.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.