In order to compress files and folders as a tar.gz file in Java we can make use of the Apache Commons Compress dependency.
Maven Dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.23.0</version>
</dependency>
Gradle Dependency:
implementation group: 'org.apache.commons', name: 'commons-compress', version: '1.23.0'
Code Example:
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import java.io.*;
import java.util.zip.Deflater;
public class TarGzCreatorWithJava {
public static void main(String[] args) {
String sourceDirPath = "/data";
String tarGzFilePath = "/data/data.tar.gz";
try {
var fileOutputStream = new FileOutputStream(tarGzFilePath);
var gzipCompressorOutputStream = new GzipCompressorOutputStream(fileOutputStream);
var tarArchiveOutputStream = new TarArchiveOutputStream(gzipCompressorOutputStream);
tarArchiveOutputStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
File sourceDir = new File(sourceDirPath);
addFilesToTar(sourceDir, tarOut, "");
tarOut.finish();
tarOut.close();
System.out.println("File created.. : " + tarGzFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void addFilesToTar(File source, TarArchiveOutputStream tarOut, String parentDir) throws IOException {
File[] files = source.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
addFilesToTar(file, tarOut, parentDir + file.getName() + "/");
} else {
TarArchiveEntry entry = new TarArchiveEntry(parentDir + file.getName());
entry.setSize(file.length());
tarOut.putArchiveEntry(entry);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[1024];
int read;
while ((read = fis.read(buffer)) != -1) {
tarOut.write(buffer, 0, read);
}
}
tarOut.closeArchiveEntry();
}
}
}
}
}

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to change bash terminal prompt string and color - Linux
- Connect Azure AD (Active Directory) for PowerShell - Powershell
- Install Bash Completion on macOS - Bash
- How to add multiple spaces between html page text - Html
- Steps to Kill a Running Process in Ubuntu Linux - Ubuntu
- Android RatingBar Example - Android
- How to take screenshot on Android - Android
- Spring Boot: JdbcTemplate Update Query With Parameters Example - Java