Copy Code code as follows:
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";
/**
* Data compression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Compress (byte[] data) throws Exception {
Bytearrayinputstream Bais = new Bytearrayinputstream (data);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Compression
Compress (Bais, BAOs);
byte[] output = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Bais.close ();
return output;
}
/**
* File compression
*
* @param file
* @throws Exception
*/
public static void Compress (file file) throws Exception {
Compress (file, true);
}
/**
* File compression
*
* @param file
* @param Delete
* Whether to delete the original file
* @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 ();
}
}
/**
* Data compression
*
* @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 ();
}
/**
* File compression
*
* @param path
* @throws Exception
*/
public static void Compress (String path) throws Exception {
Compress (path, true);
}
/**
* File compression
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Compress (String path, Boolean delete) throws Exception {
File File = new file (path);
Compress (file, delete);
}
/**
* Data Decompression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Decompress (byte[] data) throws Exception {
Bytearrayinputstream Bais = new Bytearrayinputstream (data);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Decompression
Decompress (Bais, BAOs);
data = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Bais.close ();
return data;
}
/**
* File Uncompressed
*
* @param file
* @throws Exception
*/
public static void decompress (file file) throws Exception {
Decompress (file, true);
}
/**
* File Uncompressed
*
* @param file
* @param Delete
* Whether to delete the original file
* @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 ();
}
}
/**
* Data Decompression
*
* @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 ();
}
/**
* File Uncompressed
*
* @param path
* @throws Exception
*/
public static void Decompress (String path) throws Exception {
Decompress (path, true);
}
/**
* File Uncompressed
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Decompress (String path, Boolean delete) throws Exception {
File File = new file (path);
Decompress (file, delete);
}
}