java中Gzip压缩与解压技术

时间:2021-06-20 19:59:50

java中Gzip压缩与解压技术

java中自带有Gzip压缩与解压的类,GZIPOutputStream类是用于压缩的类,里面传递的参数是字节流对象。GZIPIntputStream类是用于解压的类,传递的参数也是字节流。Gzip压缩需要把文件压缩成xxx.gz文件(不过我试了以.g或.z及其他格式,也是可以解压出来的,不过打开可能乱码)。

GZIPOutputStream在写完数据后,需要调用finish()方法。

以下是代码实现:

GZipUtils

package com.zhang;


/**
* Created by Administrator on 2016/7/18.
*/

public class GzipUtils {

private static final int BUFFER = 1024;

public static void compress(File orginalFile, File compressFile) throws IOException {
if (orginalFile != null && orginalFile.isFile() && compressFile != null && compressFile.isFile()) {
FileInputStream is = new FileInputStream(orginalFile);
FileOutputStream os = new FileOutputStream(compressFile);
compress(is, os);
is.close();
os.close();
}
}

public static void compress(String orginalFile, String compressFile) throws IOException {
if (orginalFile != null && compressFile != null) {
FileInputStream is = new FileInputStream(orginalFile);
FileOutputStream os = new FileOutputStream(compressFile);
compress(is, os);
is.close();
os.close();
}
}

public static byte[] compress(byte[] bytes) throws IOException {
if (bytes != null) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compress(bais, baos);
bytes = baos.toByteArray();
bais.close();
baos.close();
}
return bytes;
}

public static void compress(InputStream is, OutputStream os) throws IOException {
GZIPOutputStream gos = new GZIPOutputStream(os);
int len = 0;
byte[] buffer = new byte[BUFFER];
while ((len = is.read(buffer)) != -1) {
gos.write(buffer, 0, len);
}
gos.finish();
gos.flush();
gos.close();
}

public static void deCompress(File compressFile, File orginalFile) throws IOException {
if (orginalFile != null && orginalFile.isFile() && compressFile != null && compressFile.isFile()) {
FileInputStream is = new FileInputStream(compressFile);
FileOutputStream os = new FileOutputStream(orginalFile);
deCompress(is, os);
is.close();
os.close();
}
}

public static void deCompress(String compressFile, String orginalFile) throws IOException {
if (orginalFile != null && compressFile != null) {
FileInputStream is = new FileInputStream(compressFile);
FileOutputStream os = new FileOutputStream(orginalFile);
deCompress(is, os);
is.close();
os.close();
}
}

public static byte[] deCompress(byte[] bytes) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
deCompress(bais, baos);
bytes = baos.toByteArray();
bais.close();
baos.close();
return bytes;
}


public static void deCompress(InputStream is, OutputStream os) throws IOException {
GZIPInputStream gis = new GZIPInputStream(is);
int len = 0;
byte[] buffer = new byte[BUFFER];
while ((len = gis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
gis.close();
}


}

程序入口

public class Test {
public static void main(String[] args) throws IOException {
stringTest();
fileTest();
byteTest();
}

public static void fileTest() throws IOException {
String orginal = "C:\\MyPrivate\\1.txt";
String compress = "C:\\MyPrivate\\1.gz";
String decompress = "C:\\MyPrivate\\1(1).txt";

File orginalFile = new File(orginal);
File compressFile = new File(compress);
File decompressFile = new File(decompress);
if (!compressFile.exists()) {
compressFile.createNewFile();
}
if (!decompressFile.exists()) {
decompressFile.createNewFile();
}
GzipUtils.compress(orginalFile, compressFile);
GzipUtils.deCompress(compressFile, decompressFile);
}

public static void stringTest() throws IOException {
String orginal = "C:\\MyPrivate\\2.png";
String compress = "C:\\MyPrivate\\2.gz";
String decompress = "C:\\MyPrivate\\2(2).png";

GzipUtils.compress(orginal, compress);

GzipUtils.deCompress(compress, decompress);
}

public static void byteTest() throws IOException {
String test = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.orgzlex@zlex.org,snowolf@zlex.org," +
"zlex.snowolf@zlex.orgzlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";

System.out.println("compress before:" + test.getBytes().length);
byte[] bytes = GzipUtils.compress(test.getBytes());
System.out.println("compress after:" + bytes.length);
bytes = GzipUtils.deCompress(bytes);
System.out.println("decompress after:" + bytes.length);
}
}

控制台打印结果(文件测试也是可以的,就不显示了):

compress before:156
compress after:48
decompress after:156