1. Perl
  2. Module
  3. here

Archive::Tar - Create and extract tar file

You can use Archive::Tar to extract or create a tar file.

# Loading modules and creating objects
use Archive::Tar;
my $tar = Archive::Tar->new;

Use the read method to read the tar file information into memory. To extract the file, use the extract method after loading the tar file. The file will be expanded to the current directory.

# Extract tar file
$tar->read('archive.tar');
$tar->extract;

You can also extract the tar file if it is compressed in gzip format. It will automatically determine if the file is compressed in gzip format.

# Extract tar file compressed in gzip format
$tar->read('archive.tar.gz');
$tar->extract;

To create a tar file, add the file with the add_files method and then write to the tar file with the write method. After changing the current directory, add the file name with add_files with a relative path.

# Create tar file
my $curdir = '/ somedir';
chdir $curdir
  or die qq/Can't change directory "$curdir":$!/;

$tar->add_files('xxx.txt', 'dir/yyy.txt');
$tar->write('archive.tar');

You can also create a tar file compressed in gzip format. When compressing to gzip format, specify a value from 2 to 9 indicating the compression ratio in the second argument. The larger the number, the higher the compression ratio.

# Create tar.gz file
$tar->write('archive.tar.gz', 9);

Archive::Tar's Other Techniques

If you specify the file name in the argument of extract, you can extract only the specified file.

# Extract only specified files
$tar->extract('xxx.txt', 'dir/yyy.txt');

Use the list_files method to find out which files are contained in the tar file.

# Get the list of files contained in the tar file
my @files = $tar->list_files;

Use the remove method to remove the specified file from the tar file.

# Remove the specified file from the tar file
$tar->remove('xxx.txt');

Use the rename method to rename the file contained in the tar file.

# Rename the file contained in the tar file
$tar->rename('from.txt', 'to.txt');

You can use the clear method to clear the information in the tar file in memory.

# Clear tar file information in memory
$tar->clear;

Archive::Tar FAQ

Q. Is it possible to specify a directory and make all the files contained in it a tar file?

A. There is no such method, so you need to devise it. Use the File::Find module, which can search all files recursively. This is an example to create a tar file from all the files (including the top directory itself) contained under the directory named top.

# Specify a directory to create a tar file from all files under it
use File::Find 'find';
use Archive::Tar;

my $curdir = '/ somedir';
chdir $curdir
  or die qq/Can't change directory "$curdir":$!/;

my $tar = Archive::Tar->new;
my $top = 'top';

my @files;
find(sub {
  push @files, $File::Find::name;
}, $top);

$tar->add_files(@files);
$tar->write("$top.tar");

Q. Is it possible to include the symbolic linked file in the tar file?

A. Yes. Set the package variable $Archive::Tar::FOLLOW_SYMLINK to 1.

# Include the symbolic link destination file in the tar file
local $Archive::Tar::FOLLOW_SYMLINK = 1;
$tar->write('archive.tar');

(Reference) local - Temporarily save and restore a package variable

Related Informatrion