Open Source C API for Compression File Formats
C Library for Reading/Writing streaming archives in TAR, ZIP, 7-ZIP, CPIO, PAX & ISO formats.
Libarchive is an open source pure C library that enables software developers to read and write streaming archives in different formats, such as TAR, ZIP, 7-ZIP, CPIO, PAX, and ISO9660 images. The great thing about Libarchive is that it is highly modular. From the beginning, it was designed in such a manner that adding new archive formats is easy as compare to other libraries.
Libarchive supports Reading and writing several popular compressing file formats. It reads several popular formats, such as TAR, ZIP, 7-ZIP, CPIO, PAX, RAR, XAR, LHA, AR, CAB, MTREE, and ISO images. It also provides writing support for popular formats like TAR, ZIP, ISO, XAR, PAX, CPIO, AR, MTREE and SHAR archives.
At A Glance
An overview of Libarchive features.
- Create ZIP
- GZIP Compression
- BZIP2 compression
- Create TAR
- Create GZIP
- LZ4 compression
- Unzip Files
- 7-Zip Archives
- Extract BZIP2 files
- XAR Archives
- SHAR Archives
- ZIPX Archives
Libarchive supports popular compression file formats listed below.
Libarchive only requires C run-time
Getting Started with Libarchive
The libarchive distribution contains a standard Autoconf-generated "configure" script that can be used to install libarchive, bsdtar, and bsdcpio on almost any POSIX-like system.
A typical installation code is given below
$ tar xzf libarchive-2.7.0.tar.gz $ cd libarchive-2.7.0 $ ./configure $ make $ make check $ make install
You can also use the"cmake" utility to generate configuration files for a variety of IDEs, such as Visual Studio on Windows and XCode on Mac OS. Please remember that it can be used with libarchive 2.6.990a or later. You will first need to obtain and install the appropriate version of the cmake tool for your platform
Use Automatic Format Detector
Libarchive uses an automatic format detector when reading archives. It uses an internal “bidding” process that examines the incoming data using multiple modules. The detector automatically tackles archives that are compressed with Tar, Gzip, Bzip2, and many other popular compression algorithms.
Writing Files to Gzipped TAR Archive
Libarchive enables software developers to write a group of files into a gzipped tar archive. The writing feature is more complex as compared to the reading feature. The common procedure is to first create the struct archive object, set any preferred options, initialize the archive, append entries and in the end, don’t forget to close the archive and release all resources.
C API for Reading File Contents
Libarchive allows computer programmers to read and extract the contents of a file. For file extracting from the archive you first iterate through it until you find one with the filename you want. After that, you can read data and writes it to a file on disk. Compression and format are automatically detected in reading.
How to Extract Archives to Disk via C
Libarchive API gives computer programmers the capability to extract most archives to a disk. To extract archives you need to read headers from the input archive and writes them to disk. To complete the task you need to pull data from a reading archive and write it to a write handler.