In previous article, we show how to compress a file in Gzip format. This article shows how to decompress a Gzip file.
1. Decompress a Gzip file – GZIPInputStream
1.1 This example decompress a Gzip file sitemap.xml.gz back to its original file sitemap.xml. We copy the GZIPInputStream into a FileOutputStream to decompress a Gzip file.
GZipExample.java
package com.favtuts.io.howto.compress;
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
public class GZipExample {
public static void main(String[] args) {
// decompress
Path source = Paths.get("/home/tvt/workspace/favtuts/sitemap.xml.gz");
Path target = Paths.get("/home/tvt/workspace/favtuts/sitemap3.xml");
if (Files.notExists(source)) {
System.err.printf("The path %s doesn't exist!", source);
return;
}
try {
GZipExample.decompressGzip(source, target);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void decompressGzip(Path source, Path target) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(
new FileInputStream(source.toFile()));
FileOutputStream fos = new FileOutputStream(target.toFile())) {
// copy GZIPInputStream to FileOutputStream
byte[] buffer = new byte[1024];
int len;
while ((len = gis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
}
2. Decompress a Gzip file, again.
2.1 This example is similar to example 1; instead, we use NIO File.copy to copy a file.
import java.nio.file.Files;
import java.util.zip.GZIPInputStream;
//...
public static void decompressGzipNio(Path source, Path target) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(
new FileInputStream(source.toFile()))) {
Files.copy(gis, target);
}
}
3. Decompress a Gzip file to byte arrays
3.1 This example decompress a Gzip file into a byte[] directly without saving it into a local file.
GZipExample3.java
package com.favtuts.io.howto.compress;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.zip.*;
public class GZipExample {
public static void main(String[] args) {
// decompress
Path source = Paths.get("/home/tvt/workspace/favtuts/sitemap.xml.gz");
if (Files.notExists(source)) {
System.err.printf("The path %s doesn't exist!", source);
return;
}
try {
byte[] bytes = GZipExample.decompressGzipToBytes(source);
// do task for the byte[]
//convert byte[] to string for display
System.out.println(new String(bytes, StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
// decompress a Gzip file into a byte arrays
public static byte[] decompressGzipToBytes(Path source) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (GZIPInputStream gis = new GZIPInputStream(
new FileInputStream(source.toFile()))) {
// copy GZIPInputStream to ByteArrayOutputStream
byte[] buffer = new byte[1024];
int len;
while ((len = gis.read(buffer)) > 0) {
output.write(buffer, 0, len);
}
}
return output.toByteArray();
}
}
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/howto/compress