1. Products
  2.   Compression
  3.   Java
  4.   Jarchivelib
 
  

Open Source Java Compression & Archiving Library

Generate, Read, Extract, Stream and Compress/Decompress ZIP, TAR, and GZIP Archives and Other Popular Archive Formats via Open Source Free Java API

In the vast world of software development, efficient data compression is a critical aspect of optimizing storage and accelerating data transfer. One remarkable tool that has emerged to tackle this challenge is the JArchivelib library. With its simplicity, flexibility, and powerful features, JArchivelib has become a go-to choice for software developers seeking reliable compression and decompression capabilities in their Java applications. Whether your Java application is running on Windows, macOS, or Linux, it ensures consistent behavior across different platforms, making it a reliable choice for cross-platform development.

Jarchivelib is an open source Java library that allows developers to create a new archiver to handle zip archives inside their own applications. It is a simple archiving and compression library that automatically generates ZIP, TAR.gz, TAR.bz2, and TAR archived source code.

Jarchivelib is an open source Java library designed to handle various archive formats seamlessly. Whether you're dealing with ZIP, TAR, GZIP, or other popular archive formats, the library simplifies the process of compressing and decompressing files, making it an indispensable asset for developers working on projects that require efficient file management. The library provides options for specifying compression levels, allowing developers to balance between file size and compression speed. It has included support for several important features such as creating ZIP archives using all the available files in a directory, creating password-protected ZIP files, extracting ZIP file contents to a directory, and many more.

JArchivelib boasts an intuitive and straightforward API, making it accessible to developers of all skill levels. Its user-friendly design minimizes the learning curve, allowing software developers to integrate compression and decompression functionalities effortlessly. Its simplicity, versatility, and platform independence make it a go-to choice for developers looking to streamline their archiving workflows. Whether you're packing files for distribution or extracting data from archives, the library proves to be a dependable companion, simplifying tasks that would otherwise be tedious and error-prone.

Previous Next

Getting Started with Jarchivelib

To run your project using Jarchivelib, first of all, you need to have Java 7 & above. You can manually download the repository from GitHub. Use the following command to install it.

Install via Git command

 git clone https://github.com/thrau/jarchivelib.git 

Jarchivelib Maven Dependency

<dependency>
<groupId>org.rauschig</groupId>
<artifactId>jarchivelib</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>

Compress and Decompress Files via Java Library

Jarchivelib provides the functionally for compressing and decompressing files inside Java applications. It allows to compresses of the given input file to the given destination directory or file. It requires the source to be a readable File, and the destination to be either a file or a directory. The API also supports decompressing the given source file to the given destination directory or file.

Archive Extraction using Java Library

The open source JArchivelib library makes is easy for software developers to load and extract files for an existing archive inside Java applications. Using jarchivelib library, developers are not limited to a single archive format. Whether developers are dealing with ZIP, TAR, GZIP, or BZIP2, the library has got them covered. The below code example demonstrates how users can extract files from a ZIP archive inside their own Java applications.

How to Extract Files from a ZIP Archive inside Java Applications?

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class ZipExtractor {

    public static void main(String[] args) {
        String zipFilePath = "example.zip";
        String destinationFolderPath = "extractedFiles";

        try {
            ZipFile zipFile = new ZipFile(zipFilePath);
            zipFile.extractAll(destinationFolderPath);
            System.out.println("Files extracted successfully.");
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

Create a New ZIP Archives using Java

Jarchivelib enables software developers to create a new archiver to handle zip archives inside their own Java applications. If the File has a composite file extension such as ".tar.gz", the created Archiver will also handle ".gz" compression. Developers can omit the filename extension in the archive name, as it will be appended by the archiver automatically if it is missing. Developers can also create a new tar archive with gzip compression that can contain the entire directory.

How to Create a ZIP Archive & Add Files to It via Java API?

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class ZipCreator {

    public static void main(String[] args) {
        String zipFilePath = "example.zip";
        String sourceFolderPath = "sourceFolder";

        try {
            ZipFile zipFile = new ZipFile(zipFilePath);
            zipFile.addFolder(new File(sourceFolderPath));
            System.out.println("ZIP archive created successfully.");
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

Specify Compression Levels via Java

The JArchivelib library provides various types of options for specifying compression levels, allowing software developers to balance between file size and compression speed. This flexibility ensures that the library can be tailored to specific performance needs, making it suitable for both quick, lightweight compression and more resource-intensive tasks. Here's an example demonstrating how to set the compression level while creating a ZIP archive.

How to Set the Compression Level While Creating a ZIP Archive using Java?

import net.lingala.zip4j.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod;

public class ZipWithCompressionLevel {

    public static void main(String[] args) {
        String zipFilePath = "example.zip";
        String sourceFolderPath = "sourceFolder";
        int compressionLevel = 5; // Compression level: 0 (no compression) to 9 (maximum compression)

        try {
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(CompressionMethod.DEFLATE);
            parameters.setCompressionLevel(compressionLevel);

            ZipFile zipFile = new ZipFile(zipFilePath);
            zipFile.addFolder(new File(sourceFolderPath), parameters);
            System.out.println("ZIP archive created with compression level " + compressionLevel);
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}
 English