复制代码代码如下:
paket com.cjonline.foundation.cpe.action;
impor java.io.bytearrayInputStream;
impor java.io.bytearrayoutputStream;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.fileoutputStream;
impor java.io.inputstream;
impor java.io.outputStream;
impor java.util.zip.gzipinputStream;
impor java.util.zip.gzipoutputStream;
kelas abstrak publik gziputils {
Buffer int static final public = 1024;
string final statis publik ext = ".gz";
/**
* 数据压缩
*
* @param data
* @kembali
* @Throws Exception
*/
byte statis publik [] kompres (byte [] data) melempar pengecualian {
BytearrayInputStream Bais = bytearrayInputStream baru (data);
BytearrayoutputStream baos = new bytearrayoutputStream ();
// 压缩
kompres (Bais, Baos);
byte [] output = Baos.tobyteArray ();
baos.flush ();
baos.close ();
bais.close ();
output pengembalian;
}
/**
* 文件压缩
*
* file @param
* @Throws Exception
*/
public static void compress (file file) melempar Exception {
kompres (file, true);
}
/**
* 文件压缩
*
* file @param
* @param hapus
* 是否删除原始文件
* @Throws Exception
*/
public static void compress (file file, boolean delete) melempar pengecualian {
FileInputStream fis = new fileInputStream (file);
FileOutputStream fos = new FileOutputStream (file.getPath () + ext);
kompres (fis, fos);
fis.close ();
fos.flush ();
fos.close ();
if (hapus) {
file.delete ();
}
}
/**
* 数据压缩
*
* @param adalah
* @param OS
* @Throws Exception
*/
Kompres void statis publik (InputStream IS, OutputStream OS)
melempar pengecualian {
GzipoutputStream gos = GzipoutputStream baru (OS);
jumlah int;
data byte [] = byte baru [buffer];
while ((count = is.read (data, 0, buffer))! = -1) {
gos.write (data, 0, hitungan);
}
gos.finish ();
gos.flush ();
gos.close ();
}
/**
* 文件压缩
*
* Jalur @param
* @Throws Exception
*/
public static void compress (jalur string) melempar Exception {
kompres (jalur, benar);
}
/**
* 文件压缩
*
* Jalur @param
* @param hapus
* 是否删除原始文件
* @Throws Exception
*/
public static void compress (jalur string, boolean delete) melempar pengecualian {
File file = file baru (path);
kompres (file, hapus);
}
/**
* 数据解压缩
*
* @param data
* @kembali
* @Throws Exception
*/
byte statis publik [] decompress (byte [] data) melempar Exception {
BytearrayInputStream Bais = bytearrayInputStream baru (data);
BytearrayoutputStream baos = new bytearrayoutputStream ();
// 解压缩
Decompress (Bais, Baos);
data = Baos.tobyteArray ();
baos.flush ();
baos.close ();
bais.close ();
pengembalian data;
}
/**
* 文件解压缩
*
* file @param
* @Throws Exception
*/
public static void decompress (file file) melempar pengecualian {
decompress (file, true);
}
/**
* 文件解压缩
*
* file @param
* @param hapus
* 是否删除原始文件
* @Throws Exception
*/
public static void decompress (file file, boolean delete) melempar pengecualian {
FileInputStream fis = new fileInputStream (file);
FileOutputStream fos = new FileOutputStream (file.getPath (). Ganti (ext,
""));
Decompress (FIS, FOS);
fis.close ();
fos.flush ();
fos.close ();
if (hapus) {
file.delete ();
}
}
/**
* 数据解压缩
*
* @param adalah
* @param OS
* @Throws Exception
*/
Decompress void statis publik (InputStream IS, OutputStream OS)
melempar pengecualian {
GzipInputStream GIS = GzipInputStream baru (IS);
jumlah int;
data byte [] = byte baru [buffer];
while ((count = gis.read (data, 0, buffer))! = -1) {
os.write (data, 0, hitung);
}
gis.close ();
}
/**
* 文件解压缩
*
* Jalur @param
* @Throws Exception
*/
public static void decompress (jalur string) melempar Exception {
decompress (jalan, benar);
}
/**
* 文件解压缩
*
* Jalur @param
* @param hapus
* 是否删除原始文件
* @Throws Exception
*/
public static void decompress (string path, boolean delete) melempar Exception {
File file = file baru (path);
decompress (file, hapus);
}
}