Archive::Tar(3perl) Perl Programmers Reference Guide Archive::Tar(3perl)
Archive::Tar - module for manipulations of tar archives
| Use Case | Command | Description |
|---|---|---|
| đ§ Create a new tar object | my $tar = Archive::Tar->new('file.tgz') | Reads archive into memory, returns object or undef on failure |
| đ Read an archive | $tar->read('file.tar', COMPRESS_GZIP, {filter=>qr/\.pm$/}) | Load archive with optional compression and filtering |
| đ Extract all files | $tar->extract() | Write all files to disk, creating subdirectories |
| đ Extract specific files | $tar->extract('file1.txt', 'dir/file2.pl') | Extract only named files |
| â Add files to archive | $tar->add_files('file1', 'file2') | Add existing files by name |
| â Add data as file | $tar->add_data('new.txt', 'Hello World') | Add in-memory content as a file entry |
| đž Write archive to disk | $tar->write('out.tgz', COMPRESS_GZIP, 'prefix') | Write with optional compression and prefix directory |
| đ List files | $tar->list_files([qw(name size mtime)]) | Return names or hashrefs of properties |
| đ Iterate without loading all | my $next = Archive::Tar->iter('big.tar'); while (my $f = $next->()) { } | Memory-efficient streaming of archive entries |
| đ Quick extract (class method) | Archive::Tar->extract_archive('file.tgz', COMPRESS_GZIP) | Extract directly to disk, low memory |
| đ Create archive (class method) | Archive::Tar->create_archive('out.tar', COMPRESS_GZIP, @files) | One-step archive creation from file list |
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
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.
Returns a new Tar object. If given any arguments, new() calls the read() method automatically. If new() is invoked with arguments and read() fails, new() returns undef.
Read the given tar file into memory. The first argument can be a filename or a reference to an open filehandle (or an IO::Zlib object if compressed). The read replaces any previous content in $tar.
The second argument is optional; Archive::Tar now looks at file magic to determine compression. The third argument can be a hash reference with options:
limit files.Returns the number of files read (scalar) or a list of Archive::Tar::File objects (list).
Check if the archive contains a certain file (exact match on full path). Returns true/false.
Write files matching names to disk, creating subdirectories as needed. If called without arguments, extract entire archive. Returns list of extracted filenames.
Write a single entry to disk. Optionally specify a different path. Returns true on success.
$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 a list of all file names. If passed an array reference of properties, returns a list of hash references with those properties. Supported properties: name, size, mtime, mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix.
Returns Archive::Tar::File objects matching filenames. If no list, returns all objects.
Return the content of the named file.
Replace the content of a file entry.
Rename a file in the in-memory archive. Must use Unix path. Returns true/false.
Change mode of a file entry. Returns true/false.
Change owner and group. Returns true/false.
Remove entries matching filenames. Returns list of remaining Archive::Tar::File objects.
Clear the in-memory archive, producing a blank object.
Write the in-memory archive to disk. First argument can be filename or GLOB reference. Compression can be COMPRESS_GZIP, COMPRESS_BZIP, COMPRESS_XZ, or a digit (gzip level). The third argument is an optional prefix directory. If no arguments, returns the archive as a string.
# 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 );
Add files to the in-memory archive by name. Unix path conversion is automatic. Returns list of added Archive::Tar::File objects.
Add a file with given name and content. Optional hash reference can set properties: name, size, mtime, mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix, type. Constants for type: FILE, HARDLINK, SYMLINK, CHARDEV, BLOCKDEV, DIR, FIFO, SOCKET. Returns the Archive::Tar::File object or undef on failure.
Returns the current error string. If true argument, returns stacktrace via Carp::longmess. Also available as $Archive::Tar::error (deprecated).
Set the current working directory for extraction to avoid repeated Cwd::cwd() calls. Pass undef to revert to default behavior. The extract() method calls this automatically.
Create a tar file from list of files. First argument can be filename or GLOB. Compression constants as above. Returns false on failure.
# 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 );
Returns an iterator function that reads the tar file without loading all in memory. Each call returns the next Archive::Tar::File object. Options same as read().
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";
}
List files in an archive. Returns names or hash references with properties (same as object method).
Extract contents of a tar file to the current working directory. Returns list of extracted files or false on failure.
Archive::Tar->has_io_string â Returns true if IO::String support is loaded.Archive::Tar->has_perlio â Returns true if perlio support is loaded (perl 5.8+).Archive::Tar->has_zlib_support â Returns true if zlib compressed archives can be extracted.Archive::Tar->has_bzip2_support â Returns true if bzip2 compressed archives can be extracted.Archive::Tar->has_xz_support â Returns true if xz compressed archives can be extracted.Archive::Tar->can_handle_compressed_files â Returns true if all compression backends are available.These variables control the behavior of Archive::Tar. Set them before use.
$Archive::Tar::FOLLOW_SYMLINK â Set to 1 to copy symlink targets (like tar -h). Default: 0.$Archive::Tar::CHOWN â Set to 0 to disable chown-ing. Default: 1.$Archive::Tar::CHMOD â Set to 0 to disable chmod-ing. Default: 1.$Archive::Tar::SAME_PERMISSIONS â When CHMOD is enabled, controls permission filtering. Default: 1 for root, 0 for normal users.$Archive::Tar::DO_NOT_USE_PREFIX â Set to 1 to use GNU Extended Headers instead of POSIX header prefix for long paths. Default: 0.$Archive::Tar::DEBUG â Set to 1 to get verbose error messages. Default: 0.$Archive::Tar::WARN â Set to 0 to suppress warnings. Default: 1.$Archive::Tar::error â Holds last error (deprecated; use $tar->error()).$Archive::Tar::INSECURE_EXTRACT_MODE â Set to true to allow extraction outside current directory. Default: 0 (security).$Archive::Tar::HAS_PERLIO â Boolean; set to false to disable perlio.$Archive::Tar::HAS_IO_STRING â Boolean; set to false to disable IO::String.$Archive::Tar::ZERO_PAD_NUMBERS â Set to 1 to zero-pad size/mtime/checksum (for busybox compatibility). Default: 0.Behavior can be tuned by setting $Archive::Tar::RESOLVE_SYMLINK or environment variable PERL5_AT_RESOLVE_SYMLINK before loading the module.
read() for speed.Limitation: Does not work for non-seekable sources (terminals, pipes, sockets).
/bin/tar if speed is critical.extract_archive or iter for low memory.iter class method.iter or /bin/tar if memory is a concern.$Archive::Tar::DO_NOT_USE_PREFIX to true. GNU tar before 1.14 also has issues.Archive::Tar::File objects:$tar->extract(
grep { $_->full_path =~ /foo/ } $tar->get_files
);
uncompress or gunzip, or compress for writing.use Encode;
my $data = "Euro: \x{20AC}";
$data = encode('utf8', $data);
$tar->add_data('file.txt', $data);
# When extracting:
my $data = $tar->get_content();
$data = decode('utf8', $data);
The AIX tar does not fill unused space with 0x00, causing warnings like "Invalid header block at offset nnn". Fixed in AIX levels listed below (2009Q4). IBM APAR IZ50240.
This module by Jos Boumans <kane AT cpan.org>. Please report bugs to <bug-archive-tar AT rt.org>.
Thanks to Sean Burke, Chris Nandor, Chip Salzenberg, Tim Heaney, Gisle Aas, Rainer Tammer and especially Andrew Savige for their help and suggestions.
This module is copyright (c) 2002 - 2009 Jos Boumans <kane AT cpan.org>. All rights reserved. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
Generated by phpman v4.9.26-1-g511901d Author: Che Dong Under GNU General Public License
2026-08-09 10:13 @2600:1f28:365:80b0:50b3:453e:ff52:20f7
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format