Java – Compress a file in Gzip format
This article shows how to use Java to compress a file in Gzip format.
1. What is Gzip?
GZip is a standard file compression tool on Unix or Linux system, and generally has the suffix .gz
.
2. Why we need to compress a file in Gzip?
Answer: Smaller file size, save disk space.
3. How about tar.gz?
Gzip
compress a single file, and the Tar
is collecting files into one archive file, visit this article to Create tar.gz in Java
1. Compress a file in Gzip – GZIPOutputStream
We copy the FileInputStream
into a GZIPOutputStream to compress a file in Gzip format.
1.1 The below example compresses a file sitemap.xml
into a Gzip file sitemap.xml.gz
.
package com.mkyong.io.howto.compress;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.GZIPOutputStream;
public class GZipExample {
public static void main(String[] args) {
// compress a file
Path source = Paths.get("/home/mkyong/test/sitemap.xml");
Path target = Paths.get("/home/mkyong/test/sitemap.xml.gz");
if (Files.notExists(source)) {
System.err.printf("The path %s doesn't exist!", source);
return;
}
try {
GZipExample.compressGzip(source, target);
} catch (IOException e) {
e.printStackTrace();
}
}
// copy file (FileInputStream) to GZIPOutputStream
public static void compressGzip(Path source, Path target) throws IOException {
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(target.toFile()));
FileInputStream fis = new FileInputStream(source.toFile())) {
// copy file
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
gos.write(buffer, 0, len);
}
}
}
}
Output, the XML file is compressed in Gzip format, and the file size drops from 388k to 40k.
$ ls -lsah sitemap.*
388K -rw-rw-r-- 1 mkyong mkyong 388K Ogos 12 14:02 sitemap.xml
40K -rw-rw-r-- 1 mkyong mkyong 40K Ogos 12 14:06 sitemap.xml.gz
$ gzip -l sitemap.xml.gz
compressed uncompressed ratio uncompressed_name
40154 396719 89.9% sitemap.xml
1.2 This example is similar to example 1.1. Instead, we use NIO Files.copy
to copy a Path
to GZIPOutputStream
directly.
public static void compressGzipNio(Path source, Path target) throws IOException {
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(target.toFile()))) {
Files.copy(source, gos);
}
}
2. Compress a String to Gzip
2.1 This example compresses a String into a Gzip file.
public static void compressStringToGzip(String data, Path target) throws IOException {
try (GZIPOutputStream gos = new GZIPOutputStream(
new FileOutputStream(target.toFile()))) {
gos.write(data.getBytes(StandardCharsets.UTF_8));
}
}
Further Reading
Java – How to decompress a Gzip file
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-io
Great article, helped a lot! now how to do reverse in java(“Compress a file in Gzip – GZIPOutputStream”) i mean unzip ?