Java GZIP Example compress and decompress a file using Java
In this tutorial we demonstrate how to use GZIP to compress and decompress a file. Gzip is a file format and a software application used for file compression and decompression. GZIP is used to reduce the bytes of a file/text/stream. You’re able to compress a single file in GZIP format but you cannot compress and archive a directory like ZIP files using GZIP.
Project Structure
Compress and Decompress GZIP file using Java
- Compress GZIP file: We use the GZIPOutputStream to write the compressed GZIP file.
- Decompress GZIP file: We use the GZIPInputStream to read the compressed GZIP file.
package com.memorynotfound.resource; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GzipJava { private GzipJava() {} public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))){ try (FileInputStream in = new FileInputStream(input)){ byte[] buffer = new byte[1024]; int len; while((len=in.read(buffer)) != -1){ out.write(buffer, 0, len); } } } } public static void decompressGzip(File input, File output) throws IOException { try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(input))){ try (FileOutputStream out = new FileOutputStream(output)){ byte[] buffer = new byte[1024]; int len; while((len = in.read(buffer)) != -1){ out.write(buffer, 0, len); } } } } }
Compress and Decompress GZIP file using Apache
You can also use org.apache.commons:commons-compress dependency to compress and decompress to and from GZIP formats.
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.14</version> </dependency>
We can use the IOUtils.copy(in, out) to copy an InputSteam to the OutputStream.
- Compress file to GZIP format: We use the GzipCompressorOutputStream to compress the specified file.
- Decompress GZIP file: We use the GzipCompressorInputStream to decompress the compressed GZIP file.
package com.memorynotfound.resource; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class GzipApache { private GzipApache() {} public static void compressGZIP(File input, File output) throws IOException { try (GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(output))){ IOUtils.copy(new FileInputStream(input), out); } } public static void decompressGZIP(File input, File output) throws IOException { try (GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(input))){ IOUtils.copy(in, new FileOutputStream(output)); } } }
Java GZIP Program
package com.memorynotfound.resource; import java.io.File; import java.io.IOException; public class GzipProgram { public static void main(String... args) throws IOException { // class for resource classloading Class clazz = GzipApache.class; // create files to read and write File example = new File(clazz.getResource("/example.xml").getFile()); File output = new File("/tmp/example.xml.gz"); File decompressed = new File("/tmp/decompressed.xml"); // Java GZIP example compression decompression GzipJava.compressGZIP(example, output); GzipJava.decompressGzip(output, decompressed); // Apache GZIP example compression decompression GzipApache.compressGZIP(example, output); GzipApache.decompressGZIP(output, decompressed); } }
Example Output
You can see that the application created two files. The example.xml.gz is the compressed file in GZIP format. You can see that this file is smaller than the original file. The decompressed.xml file is the result of the decompression.
Download
From:一号门
COMMENTS