Python Tarfile Module

TAR is a popular file format in the Unix and Linux world for archiving files and directories into a single file. It’s often used for creating backups or distributing collections of files. In this blog post, we’ll explore how to create TAR files in Python. In addition, we will also cover how to compress TAR files into tar.gz and tar.xz archives.

Python TAR Library - Tarfile Alternate

Aspose.ZIP for Python is a Python library that allows you to work with ZIP, TAR, and GZIP file formats seamlessly. It provides a simple and efficient way to create, manipulate, and extract files from these archive formats. We will use this library to create TAR files and then compress the TAR archives with different compression algorithms.

You can either download the library or install it from PyPI.

> pip install aspose-zip

Create TAR Files in Python

Aspose.ZIP for Python offers the simplest way of creating TAR archives among all available options. So let’s have a look at the steps required to create a TAR file in Python.

  • First, create an object of the TarArchive class.
  • Then, use TarArchive.create_entry() method to add files to TAR.
  • Finally, save TAR archive using TarArchive.save() method.

The following code sample shows how to create a TAR file in Python.

Compress TAR Files in Python

Although TAR archives bundle multiple files and directories together, they lack compression by themselves. By combining TAR with the GZIP or LZMA compression algorithms (.tar.gz and .tar.xz respectively), the resulting archive significantly reduces the file size, saving disk space and speeding up data transfers.

Let’s have a look at either way of compressing TAR archives in Python.

Compress TAR with GZIP (.tar.gz)

The following are the steps to compress a TAR file with GZIP (.tar.gz) in Python.

  • First, load the TAR file using the TarArchive class.
  • Then, compress TAR file using TarArchive.save_gzipped() method.

The following code sample shows how to compress TAR with GZIP in Python.

Compress TAR with XZ (.tar.xz)

To compress a TAR file with XZ (.tar.xz), follow the steps given below.

  • First, use the TarArchive class to load the TAR file.
  • Then, compress TAR file to XZ using TarArchive.save_xz_compressed() method.

The code sample given below shows how to compress TAR to XZ in Python.

Python TAR Module - Get a Free License

You can get a free temporary license to create or compress TAR archives without evaluation limitations.

Learning Resources

Learn more about our Python TAR library using the resources given below:

Online TAR Tool

We also provide free online tools to create, compress, and extract TAR files, which are powered by Aspose.ZIP for Python.

Conclusion

Creating TAR files in Python is a common task, especially in Unix and Linux environments. Aspose.ZIP for Python simplifies this process and provides a robust set of features for working with TAR, ZIP, and GZIP archives. In this article, you have learned how to create TAR archives in a Python application. In addition, we have demonstrated how to compress TAR archives with GZIP or XZ methods in Python.

See Also