Archive::Tar(3pm)

Archive::Tar(3pm) ๐Ÿ’…

From the Perl Programmers Reference Guide โ€” manual name folded in near the top, cute and cozy. ๐ŸŽ€

๐Ÿ’– NAME ๐Ÿ’–

Archive::Tar โ€” module for manipulations of tar archives. That's the whole assignment bestie: tar archives, handled by Perl, with object-oriented main character energy. โœจ

โœจ SYNOPSIS โœจ

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

๐ŸŒธ 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. Iconic. ๐Ÿ’…

๐Ÿฆ‹ Object Methods ๐Ÿฆ‹

๐ŸŽ€ Archive::Tar->new( [$file, $compressed] ) ๐ŸŽ€

Returns a new Tar object. If given any arguments, new() calls the read() method automatically, passing on the arguments provided to the read() method. ๐Ÿซถ

If new() is invoked with arguments and the read() method fails for any reason, new() returns undef. Sad times, but that's the tea. ๐Ÿ˜ญ

๐Ÿ“‚ $tar->read ( $filename|$handle, [$compressed, {opt => 'val'}] ) ๐Ÿ“‚

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 "read" will replace any previous content in $tar! So like, don't say I didn't warn you. ๐Ÿ’€

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. ๐Ÿšจ

  • limit โ€” Do not read more than "limit" files. This is useful if you have very big archives, and are only interested in the first few files. ๐Ÿ“
  • filter โ€” Can be set to a regular expression. Only files with names that match the expression will be read. ๐Ÿ”
  • md5 โ€” Set to 1 and the md5sum of files will be returned (instead of file data). Like this:
my $iter = Archive::Tar->iter( $file,  1, {md5 => 1} );
while( my $f = $iter->() ) {
    print $f->data . "\t" . $f->full_path . $/;
}
  • extract โ€” If set to true, immediately extract entries when reading them. This gives you the same memory break as the "extract_archive" function. Note however that entries will not be read into memory, but written straight to disk. This means no "Archive::Tar::File" objects are created for you to inspect. ๐Ÿซถ

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. ๐Ÿ’–

๐ŸŒˆ $tar->contains_file( $filename ) ๐ŸŒˆ

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 "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. No cap, it's strict like that. ๐Ÿ’…

๐Ÿงš $tar->extract( [@filenames] ) ๐Ÿงš

Write files whose names are equivalent to any of the names in @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 "extract" is called without a list of file names, the entire contents of the archive are extracted. ๐ŸŽ€

Returns a list of filenames extracted. โœจ

๐ŸŽ $tar->extract_file( $file, [$extract_path] ) ๐ŸŽ

Write an entry, whose name is equivalent to the file name provided to disk. Optionally takes a second parameter, which is the full native path (including filename) the entry will be written to. ๐Ÿซถ

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. ๐Ÿ˜ญ

๐Ÿฆ‹ $tar->list_files( [\@properties] ) ๐Ÿฆ‹

Returns a list of the names of all the files in the archive. ๐Ÿ“

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. ๐Ÿ’…

๐ŸŒธ $tar->get_files( [@filenames] ) ๐ŸŒธ

Returns the "Archive::Tar::File" objects matching the filenames provided. If no filename list was passed, all "Archive::Tar::File" objects in the current Tar object are returned. ๐Ÿซถ

Please refer to the "Archive::Tar::File" documentation on how to handle these objects. ๐Ÿ“š

๐Ÿ’œ $tar->get_content( $file ) ๐Ÿ’œ

Return the content of the named file. Simple, clean, iconic. โœจ

๐Ÿ“ $tar->replace_content( $file, $content ) ๐Ÿ“

Make the string $content be the content for the file named $file. Glow-up for one file's contents. ๐Ÿ’…

๐ŸŽ€ $tar->rename( $file, $new_name ) ๐ŸŽ€

Rename the file of the in-memory archive to $new_name. โœจ

Note that you must specify a Unix path for $new_name, since per tar standard, all files in the archive must be Unix paths. No Windows-y shortcuts, bestie. ๐Ÿซ 

Returns true on success and false on failure. ๐Ÿ’–

๐Ÿ”ฅ $tar->chmod( $file, $mode ) ๐Ÿ”ฅ

Change mode of $file to $mode. ๐Ÿ”

Returns true on success and false on failure. โœ…

๐Ÿ‘‘ $tar->chown( $file, $uname [, $gname] ) ๐Ÿ‘‘

Change owner $file to $uname and $gname. ๐Ÿ‘‘

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 "Archive::Tar::File" objects that remain. Bye-bye, entries. ๐Ÿ‘‹

๐ŸŒˆ $tar->clear ๐ŸŒˆ

"clear" clears the current in-memory archive. This effectively gives you a 'blank' object, ready to be filled again. Note that "clear" only has effect on the object, not the underlying tarfile. So the object gets a reset, not the actual tarfile. ๐Ÿซถ

๐Ÿ’พ $tar->write ( [$file, $compressed, $prefix] ) ๐Ÿ’พ

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). ๐Ÿ“

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. ๐Ÿš€

๐Ÿ“ฆ $tar->add_files( @filenamelist ) ๐Ÿ“ฆ

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. ๐Ÿซถ

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. ๐ŸŽ€

๐Ÿช„ $tar->add_data ( $filename, $data, [$opthashref] ) ๐Ÿช„

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 $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:

  • FILE โ€” Regular file. ๐Ÿ“„
  • HARDLINK, SYMLINK โ€” Hard and symbolic ("soft") links; linkname should specify target. ๐Ÿ”—
  • CHARDEV, BLOCKDEV โ€” Character and block devices. devmajor and devminor should specify the major and minor device numbers. ๐Ÿ”Œ
  • DIR โ€” Directory. ๐Ÿ“
  • FIFO โ€” FIFO (named pipe). ๐Ÿšฐ
  • SOCKET โ€” Socket. ๐Ÿงฆ

Returns the "Archive::Tar::File" object that was just added, or "undef" on failure. ๐Ÿ’œ

๐Ÿ’ฌ $tar->error( [$BOOL] )

Returns the current error string โ€” usually the last error that got reported. If you pass a true value, it gives you the Carp::longmess equivalent of the error, which basically means you get a stacktrace. Iconic for debugging, honestly.

For backwards compatibility, this error is also available as $Archive::Tar::error, but please use the method call instead โ€” it's the recommended way, fr fr.

๐Ÿ› ๏ธ $tar->setcwd( $cwd );

Archive::Tar needs to know the current directory, and it will run Cwd::cwd() every single time it extracts a relative entry from the tarfile and saves it in the file system. (As of version 1.30, however, Archive::Tar will use the speed optimization described below automatically, so it's only relevant if you're using extract_file().)

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. Speed boost unlocked ๐Ÿš€

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. No extra work, bestie โœจ


๐Ÿ’… CLASS METHODS ๐Ÿ’…

๐Ÿ“ฆ Archive::Tar->create_archive($file, $compressed, @filelist)

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 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 โ€” no drama, no warning.

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.

โš ๏ธ heads up: 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.

๐Ÿ” Archive::Tar->iter( $filename, [ $compressed, {opt => $val} ] )

Returns an iterator function that reads the tar file without loading it all in memory. Each time the function is called it will return the next file in the tarball. The files are returned as Archive::Tar::File objects. The iterator function returns the empty list once it has exhausted the files contained. Lowkey elegant ๐Ÿฆ‹

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";

    # ....
}

๐Ÿ“‹ Archive::Tar->list_archive($file, $compressed, [\@properties])

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 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. iykyk โœจ

๐Ÿ“‚ Archive::Tar->extract_archive($file, $compressed)

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.

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.

๐Ÿงต $bool = Archive::Tar->has_io_string

Returns true if we currently have IO::String support loaded.

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.

๐ŸŽ€ $bool = Archive::Tar->has_perlio

Returns true if we currently have perlio support loaded.

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.

๐ŸŒธ $bool = Archive::Tar->has_zlib_support

Returns true if Archive::Tar can extract zlib compressed archives.

๐Ÿ“ $bool = Archive::Tar->has_bzip2_support

Returns true if Archive::Tar can extract bzip2 compressed archives.

๐ŸŒˆ $bool = Archive::Tar->has_xz_support

Returns true if Archive::Tar can extract xz compressed archives.

๐Ÿ‘‘ Archive::Tar->can_handle_compressed_files

A simple checking routine, which will return true if Archive::Tar is able to uncompress compressed archives on the fly with IO::Zlib, IO::Compress::Bzip2 and IO::Compress::Xz or false if not both are installed.

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. Vibe check your archives first ๐Ÿซถ

โœจ GLOBAL VARIABLES โœจ

okay bestie, these are the module-wide knobs ๐Ÿ’… โ€” set them and everything shifts. read carefully, some of these are lowkey load-bearing.

  • $Archive::Tar::FOLLOW_SYMLINK โ€” set this bad boy to 1 and Archive::Tar will effectively make a copy of the file when extracting. default is 0, which means the symlink stays intact. and like, obviously you'll have to pack the file that's linked to as well, no cap. ๐Ÿซถ

this one gets checked when you write out the tarfile using write or create_archive.

it works exactly like /bin/tar's -h option. iconic behaviour tbh.

  • $Archive::Tar::CHOWN โ€” by default, Archive::Tar will try to chown your files if it's able to. but sometimes that's not the vibe. in that case, set this variable to 0 to disable the chown-ing, even if it were possible.

the default is 1.

  • $Archive::Tar::CHMOD โ€” by default, Archive::Tar will try to chmod your files to whatever mode was specified for that particular file in the archive. again, sometimes not the vibe! set this variable to 0 to disable the chmod-ing.

the default is 1.

  • $Archive::Tar::SAME_PERMISSIONS โ€” when $Archive::Tar::CHMOD is enabled, this setting controls whether the permissions on files from the archive are used without modification, or whether they get filtered by removing any setid bits and applying the current umask. ๐ŸŽ€

the default is 1 for the root user and 0 for normal users.

  • $Archive::Tar::DO_NOT_USE_PREFIX โ€” by default, Archive::Tar will try to put paths that are over 100 characters in the prefix field of your tar header, as defined per POSIX-standard. sounds great, right? but some (older) tar programs do not implement this spec ๐Ÿ˜ญ. to retain compatibility with these older / non-POSIX-compliant versions, you can set the $DO_NOT_USE_PREFIX variable to a true value, and Archive::Tar will use an alternate way of dealing with paths over 100 characters by using the GNU Extended Header feature.
heads up bestie: clients that don't support the GNU Extended Header feature will not be able to read these archives. that includes tars on Solaris, Irix and AIX. ๐Ÿ’€

the default is 0.

  • $Archive::Tar::DEBUG โ€” set this variable to 1 to always get the Carp::longmess output of the warnings instead of the regular carp. it's the same message you'd get by doing:
$tar->error(1);

defaults to 0.

  • $Archive::Tar::WARN โ€” set this variable to 0 if you do not want any warnings printed. personally i recommend against doing this, but people asked for the option so here we are ๐Ÿ™ˆ. also, be advised that this is of course not threadsafe.

defaults to 1.

  • $Archive::Tar::error โ€” holds the last reported error. kept for historical reasons, but its use is very much discouraged. use the error() method instead:
warn $tar->error unless $tar->extract;
history lesson โœจ: in older versions of this module, the error() method would return an effectively global value even when called as an instance method like above. this has since been fixed, and multiple instances of Archive::Tar now have separate error strings. periodt.
  • $Archive::Tar::INSECURE_EXTRACT_MODE โ€” this variable indicates whether Archive::Tar should allow files to be extracted outside their current working directory.
alllllowing this could have security implications, because a malicious tar archive could alter or replace any file the extracting user has permissions to. therefore, 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. iykyk.

  • $Archive::Tar::EXTRACT_HARDLINK โ€” prior to version 3.06, Archive::Tar would honour hardlinks in archives. version 3.06 and onwards will skip hardlinks unless this flag is set. exercise caution when enabling this ๐Ÿ”ฅ.
  • $Archive::Tar::HAS_PERLIO โ€” this variable holds a boolean indicating if we currently have perlio support loaded. this will be enabled for any perl greater than 5.8 compiled with perlio.

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 ๐Ÿ‘‘.

  • $Archive::Tar::HAS_IO_STRING โ€” this variable holds a boolean indicating if we currently have IO::String support loaded. this will be enabled for any perl that has a loadable IO::String module.

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 ๐Ÿ‘‘.

  • $Archive::Tar::ZERO_PAD_NUMBERS โ€” this variable holds a boolean indicating if we will create zero padded numbers for size, mtime and checksum. the default is 0, indicating that we will create space padded numbers. added for compatibility with busybox implementations ๐ŸŒธ.

so you wanna vibe-check the symlink resolution? you can tune the behaviour by setting the $Archive::Tar::RESOLVE_SYMLINK variable, or $ENV{PERL5_AT_RESOLVE_SYMLINK} โ€” but note: before loading the module Archive::Tar โญ.

values can be one of the following:

  • none โ€” disable this mechanism and fail 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. ๐Ÿ’€

  • $Archive::Tar::EXTRACT_BLOCK_SIZE โ€” this variable holds an integer with the block size that should be used when writing files during extraction. it defaults to 1 GiB. pls note that this cannot be arbitrarily large, since some operating systems limit the number of bytes that can be written in one call to write(2), so if this is too large, extraction may fail with an error ๐Ÿ˜ญ.
  • $Archive::Tar::MAX_FILE_SIZE โ€” this variable holds an upper bound on the per-entry declared size that Archive::Tar will accept when reading an archive. entries whose header claims a larger size are refused with an error before any read allocation (protective king behaviour ๐Ÿ‘‘). defaults to 1 GiB. set to 0 to disable the cap.

๐Ÿ’– FAQ ๐Ÿ’–

What's the minimum perl version required to run Archive::Tar?

You'll need perl version 5.005_03 or newer. That's the floor, bestie โ€” nothing older is getting in the club. ๐Ÿšช

Isn't Archive::Tar slow?

Yeah. Yeah it is. ๐Ÿ˜ญ It's pure perl, so it's a lot slower than your /bin/tar.

But here's the thing โ€” it's very portable. That's the whole trade. If speed is an issue for you, consider using /bin/tar instead.

Isn't Archive::Tar heavier on memory than /bin/tar?

Yes, it is โ€” see the previous answer, same energy. ๐Ÿ’€

Since Compress::Zlib and therefore IO::Zlib doesn't support seek on their filehandles, there's honestly little choice but to read the archive into memory. That's totally ok if what you actually want is in-memory manipulation of the archive.

If you just want to extract, use the extract_archive class method instead โ€” it'll optimize and write to disk immediately. โœจ

Another option is the iter class method, which lets you iterate over the files in the tarball without reading them all into memory at once. Iconic behaviour, no notes. ๐Ÿ‘‘

Can you lazy-load data instead?

In some cases, yes! You can use the iter class method to iterate over the files in the tarball without reading them all in memory at once. ๐Ÿฆ‹

How much memory will an X kb tar file need?

Probably more than X kb, since it will all be read into memory. Not gonna lie to you. ๐Ÿ™ˆ

If that's a problem, and you don't need to do in memory manipulation of the archive, consider using the iter class method, or /bin/tar instead.

What do you do with unsupported filetypes in an archive?

Unix has a few filetypes that just aren't supported on other platforms, like Win32. If we encounter a hardlink or symlink we'll just try to make a copy of the original file, rather than throwing an error. Very "make it work" of us. ๐Ÿ’…

This does require you to read the entire archive into memory first, since otherwise we wouldn't know what data to fill the copy with. (Which means: you cannot use the class methods โ€” including iter โ€” on archives that have incompatible filetypes and still expect things to work. That's the fine print, bestie. โš ๏ธ)

For other filetypes, like chardevs and blockdevs, we'll warn that the extraction of this particular item didn't work.

I'm using WinZip, or some other non-POSIX client, and files are not being extracted properly!

By default, Archive::Tar is in a completely POSIX-compatible mode, which uses the POSIX-specification of tar to store files. For paths greater than 100 characters, this is done using the POSIX header prefix.

Non-POSIX-compatible clients may not support this part of the specification, and may only support the GNU Extended Header functionality. To facilitate those clients, you can set the $Archive::Tar::DO_NOT_USE_PREFIX variable to "true". See the GLOBAL VARIABLES section for details on this variable.

Heads up! 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".

How do I extract only files that have property X from an archive?

Sometimes you might not wish to extract a complete archive โ€” just the files that are relevant to you, based on some criteria. Mood. ๐ŸŽฏ

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. ๐Ÿ“š

How do I access .tar.Z files?

The Archive::Tar module can optionally use Compress::Zlib (via the IO::Zlib module) to access tar files that have been compressed with gzip. Unfortunately, tar files compressed with the Unix compress utility cannot be read by Compress::Zlib and so cannot be directly accesses by Archive::Tar. ๐Ÿ˜”

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 one 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 ;

How do I handle Unicode strings?

Archive::Tar uses byte semantics for any files it reads from or writes to disk. This is not a problem if you only deal with files and never look at their content, or work solely with byte strings. But omg โ€” if you use Unicode strings with character semantics, some additional steps need to 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 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);

And 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. It's giving "chaos, but make it portable". ๐Ÿ˜ฎโ€๐Ÿ’จ


๐Ÿ’€ CAVEATS ๐Ÿ’€

The AIX tar does not fill all unused space in the tar archive with 0x00. This sometimes leads to warning messages from Archive::Tar:

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. ๐Ÿ“ž


๐ŸŒˆ 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! ๐Ÿ™

Allow archives to be passed in as string

Currently, we only allow opened filehandles or filenames, but not strings. The internals would need some reworking to facilitate stringified archives.

Facilitate processing an opened filehandle of a compressed archive

Currently, we only support this if the filehandle is an IO::Zlib object. Environments, like apache, will present you with an opened filehandle to an uploaded file, which might be a compressed archive.


๐ŸŽ€ SEE ALSO ๐ŸŽ€

The GNU tar specification

<https://www.gnu.org/software/tar/manual/tar.html>

The PAX format specification

The specification which tar derives from; <https://pubs.opengroup.org/onlinepubs/007904975/utilities/pax.html>


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

This module by Jos Boumans <kane@cpan.org>.

Please reports bugs to <bug-archive-tar@rt.cpan.org>. ๐Ÿ›


๐Ÿซถ ACKNOWLEDGEMENTS ๐Ÿซถ

Thanks to Sean Burke, Chris Nandor, Chip Salzenberg, Tim Heaney, Gisle Aas, Rainer Tammer and especially Andrew Savige for their help and suggestions. Besties fr fr. ๐Ÿ’œ


This module is copyright (c) 2002 - 2009 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. โœจ


And that's the whole page, bestie โ€” you made it to the very end! ๐ŸŽ‰ This beauty is perl v5.42.3, dated 2026-11-03, and it lives at Archive::Tar(3pm). Go forth and tar responsibly. ๐Ÿ’…โœจ๐Ÿฆ‹๐ŸŒธ๐Ÿ’–

๐Ÿ“ฆ where this page comes from