java使用gzip实现文件解压缩示例
package com.cjonline.foundation.cpe.action;
import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import java.util.zip.gzipinputstream;
import java.util.zip.gzipoutputstream;
public abstract class gziputils {
public static final int buffer = 1024;
public static final string ext = ".gz";
/**
* 数据压缩
*
* @param data
* @return
* @throws exception
*/
public static byte[] compress(byte[] data) throws exception {
bytearrayinputstream bais = new bytearrayinputstream(data);
bytearrayoutputstream baos = new bytearrayoutputstream();
// 压缩
compress(bais, baos);
byte[] output = baos.tobytearray();
baos.flush();
baos.close();
bais.close();
return output;
}
/**
* 文件压缩
*
* @param file
* @throws exception
*/
public static void compress(file file) throws exception {
compress(file, true);
}
/**
* 文件压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws exception
*/
public static void compress(file file, boolean delete) throws exception {
fileinputstream fis = new fileinputstream(file);
fileoutputstream fos = new fileoutputstream(file.getpath() + ext);
compress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 数据压缩
*
* @param is
* @param os
* @throws exception
*/
public static void compress(inputstream is, outputstream os)
throws exception {
gzipoutputstream gos = new gzipoutputstream(os);
int count;
byte data[] = new byte[buffer];
while ((count = is.read(data, 0, buffer)) != -1) {
gos.write(data, 0, count);
}
gos.finish();
gos.flush();
gos.close();
}
/**
* 文件压缩
*
* @param path
* @throws exception
*/
public static void compress(string path) throws exception {
compress(path, true);
}
/**
* 文件压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws exception
*/
public static void compress(string path, boolean delete) throws exception {
file file = new file(path);
compress(file, delete);
}
/**
* 数据解压缩
*
* @param data
* @return
* @throws exception
*/
public static byte[] decompress(byte[] data) throws exception {
bytearrayinputstream bais = new bytearrayinputstream(data);
bytearrayoutputstream baos = new bytearrayoutputstream();
// 解压缩
decompress(bais, baos);
data = baos.tobytearray();
baos.flush();
baos.close();
bais.close();
return data;
}
/**
* 文件解压缩
*
* @param file
* @throws exception
*/
public static void decompress(file file) throws exception {
decompress(file, true);
}
/**
* 文件解压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws exception
*/
public static void decompress(file file, boolean delete) throws exception {
fileinputstream fis = new fileinputstream(file);
fileoutputstream fos = new fileoutputstream(file.getpath().replace(ext,
""));
decompress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 数据解压缩
*
* @param is
* @param os
* @throws exception
*/
public static void decompress(inputstream is, outputstream os)
throws exception {
gzipinputstream gis = new gzipinputstream(is);
int count;
byte data[] = new byte[buffer];
while ((count = gis.read(data, 0, buffer)) != -1) {
os.write(data, 0, count);
}
gis.close();
}
/**
* 文件解压缩
*
* @param path
* @throws exception
*/
public static void decompress(string path) throws exception {
decompress(path, true);
}
/**
* 文件解压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws exception
*/
public static void decompress(string path, boolean delete) throws exception {
file file = new file(path);
decompress(file, delete);
}
}