java的方法decompress_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

4fe2cb032780c1d4062d20b6ef8858d7.png

Compress and Decompress GZIP file using JavaCompress 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.

org.apache.commons

commons-compress

1.14

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 Programpackage 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.

a3ddbc6f07afa901108be5b421f223ea.png

Download

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值