Converting compressed archives from ZIP to TAR is a frequent requirement when integrating Java services with Linux‑based pipelines. Conholdate.Total for Java provides a robust SDK that simplifies this task for developers. In this guide we walk through the complete process of ZIP to TAR conversion in Java, from Maven setup to a runnable Spring Boot example. You’ll also learn how to tweak compression options and verify the resulting TAR file.

Prerequisites and Setup

Before you begin, make sure you have the following:

  • Java 17 or newer installed.
  • Maven 3.6+ for dependency management.
  • A Spring Boot project (you can generate one from start.spring.io).
  • Access to the Conholdate.Total for Java SDK (downloadable from the official site).

Add the Conholdate Maven repository and the SDK dependency to your pom.xml:

<repositories>
    <repository>
        <id>conholdate-repo</id>
        <name>Conholdate Maven Repository</name>
        <url>https://repository.conholdate.com/repo/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.conholdate</groupId>
    <artifactId>conholdate-total</artifactId>
    <version>24.9</version>
    <type>pom</type>
</dependency>

You can also download the latest JAR directly from the download page. After the dependency is resolved, you’re ready to write the conversion code.

Building It Step by Step: ZIP to TAR Conversion in Java

Step 1: Load the Source ZIP File

First, create File objects for the input ZIP and the desired output TAR. The code also checks that the source file exists.

File inputFile = new File(INPUT_ZIP);
File outputFile = new File(OUTPUT_TAR);

if (!inputFile.exists()) {
    System.err.println("Input ZIP file not found: " + inputFile.getAbsolutePath());
    return;
}

Step 2: Initialise the ArchiveConversion Object

Create an ArchiveConversion instance with the input path and default load options. This object represents the source archive.

try (ArchiveConversion conversion = new ArchiveConversion(
        inputFile.getAbsolutePath(),
        new ArchiveLoadOptions())) {

For more details, see the official documentation.

Step 3: Configure Save Options and Compression Level

Set the desired compression level before saving. The CompressionLevel.NORMAL option provides a good balance between speed and size.

ArchiveSaveOptions saveOptions = new ArchiveSaveOptions();
saveOptions.setCompressionLevel(CompressionLevel.NORMAL);

Step 4: Perform the Conversion and Measure Time

Call save with the target TAR path. The SDK infers the format from the file extension.

conversion.save(outputFile.getAbsolutePath(), saveOptions);

You can also capture the duration to benchmark performance:

long durationMs = (System.nanoTime() - startTime) / 1_000_000;
System.out.println("Conversion completed in " + durationMs + " ms.");

Step 5: Verify the Output File

After conversion, confirm that the TAR file exists and contains data.

if (outputFile.exists() && Files.size(Path.of(outputFile.getAbsolutePath())) > 0) {
    System.out.println("TAR file created successfully: " + outputFile.getAbsolutePath());
} else {
    System.err.println("Failed to create TAR file.");
}

With the conversion complete, you can now explore additional configuration options.

Full Working Example for ZIP to TAR Conversion in Java Using Conholdate.Total

The following example demonstrates a minimal Spring Boot application that converts a ZIP archive to a TAR file using Conholdate.Total for Java.

package com.example.zip2tar;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.groupdocs.archive.ArchiveConversion;
import com.groupdocs.archive.options.ArchiveLoadOptions;
import com.groupdocs.archive.options.ArchiveSaveOptions;
import com.groupdocs.archive.options.CompressionLevel;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

@SpringBootApplication
public class ZipToTarApplication implements CommandLineRunner {

    private static final String INPUT_ZIP = "sample-input.zip";
    private static final String OUTPUT_TAR = "converted-output.tar";

    public static void main(String[] args) {
        SpringApplication.run(ZipToTarApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        File inputFile = new File(INPUT_ZIP);
        File outputFile = new File(OUTPUT_TAR);

        if (!inputFile.exists()) {
            System.err.println("Input ZIP file not found: " + inputFile.getAbsolutePath());
            return;
        }

        long startTime = System.nanoTime();

        try (ArchiveConversion conversion = new ArchiveConversion(
                inputFile.getAbsolutePath(),
                new ArchiveLoadOptions())) {

            ArchiveSaveOptions saveOptions = new ArchiveSaveOptions();
            saveOptions.setCompressionLevel(CompressionLevel.NORMAL);
            // Target format is inferred from the .tar extension
            conversion.save(outputFile.getAbsolutePath(), saveOptions);
        } catch (Exception e) {
            System.err.println("Error during ZIP to TAR conversion: " + e.getMessage());
            e.printStackTrace();
            return;
        }

        long durationMs = (System.nanoTime() - startTime) / 1_000_000;
        System.out.println("Conversion completed in " + durationMs + " ms.");

        // Verify output
        if (outputFile.exists() && Files.size(Path.of(outputFile.getAbsolutePath())) > 0) {
            System.out.println("TAR file created successfully: " + outputFile.getAbsolutePath());
        } else {
            System.err.println("Failed to create TAR file.");
        }
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Configuration Options: Settings and Tuning

The SDK offers several options you can adjust to fine‑tune the conversion:

  • CompressionLevel - Controls how aggressively the output TAR is compressed. The example uses CompressionLevel.NORMAL. Other values include NONE, FAST, and HIGH.
  • ArchiveLoadOptions - Allows you to specify password protection or custom encoding when loading the source ZIP. You can create an instance and set properties before passing it to ArchiveConversion.
  • ArchiveSaveOptions - Besides compression, you can define whether to preserve file timestamps or include directory entries. Refer to the API reference for the full list of properties.

Here is how you set a different compression level:

ArchiveSaveOptions saveOptions = new ArchiveSaveOptions();
saveOptions.setCompressionLevel(CompressionLevel.HIGH);

Experiment with these settings to achieve the best balance for your specific workload.

Key Takeaways

ZIP to TAR conversion in Java becomes straightforward when you leverage Conholdate.Total for Java. The SDK handles format detection, streaming conversion, and compression configuration with just a few lines of code. By adding the Maven dependency, writing a concise Spring Boot runner, and optionally adjusting CompressionLevel, you obtain a production‑ready solution for any file‑processing pipeline. Remember to obtain a proper license for production use; you can start with a temporary license and upgrade via the pricing page. With this foundation, extending the logic to batch processing or integrating with other services is a natural next step.

FAQs

Can I convert multiple ZIP files to TAR in a single run?

Yes. Place the conversion block inside a loop that iterates over each source ZIP path. The same Conholdate.Total for Java API instance can be reused for each file, ensuring efficient resource handling.

What compression levels are supported for ZIP to TAR conversion in Java?

The SDK provides the CompressionLevel enum with options NONE, FAST, NORMAL, and HIGH. Adjust the level via ArchiveSaveOptions.setCompressionLevel() to match your size‑vs‑speed requirements.

Is a temporary license sufficient for development?

A temporary license from the temporary license page allows you to evaluate the SDK without cost. For production deployments, purchase a full license through the pricing page.

How does ZIP to TAR conversion in Java handle large archives?

The conversion process streams data, keeping memory usage low even for multi‑gigabyte archives. Ensure adequate disk space for the output TAR and monitor the conversion duration if processing very large files.

Read More