复制代码代码如下:
package com.cjonline.foundation.cpe.action;
Importer java.io.ByteArrayInputStream;
Importer java.io.ByteArrayOutputStream;
Importer java.io.file;
import java.io.fileInputStream;
Importer java.io.fileOutputStream;
import java.io.inputStream;
import java.io.outputStream;
Importer java.util.zip.gzipinputStream;
Importer java.util.zip.gzipOutputStream;
classe abstraite publique gziputils {
Public static final int tamper = 1024;
String final statique publique ext = ".gz";
/ **
* 数据压缩
*
* Données @param
* @retour
* @throws exception
* /
Public Static octet [] compress (octet [] data) lève une exception {
BytearrayInputStream bais = new bytearrayInputStream (data);
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// 压缩
Compress (BAIS, Baos);
octet [] output = baos.toByTearray ();
baos.flush ();
baos.close ();
bais.close ();
sortie de retour;
}
/ **
* 文件压缩
*
* fichier @param
* @throws exception
* /
public static void compress (fichier de fichier) lance une exception {
compress (fichier, true);
}
/ **
* 文件压缩
*
* fichier @param
* @param supprime
* 是否删除原始文件
* @throws exception
* /
Public Static Void Compress (fichier de fichier, booléen delete) lève une exception {
FileInputStream fis = new FileInputStream (fichier);
FileOutputStream fos = new FileOutputStream (file.getPath () + ext);
Compress (FIS, FOS);
fis.close ();
fos.flush ();
fos.close ();
if (delete) {
file.delete ();
}
}
/ **
* 数据压缩
*
* @param est
* @param os
* @throws exception
* /
Compression publique de void statique (InputStream est, Outporestream OS)
lance l'exception {
GzipOutputStream gos = new gzipOutputStream (OS);
int count;
Données d'octets [] = nouveau octet [tampon];
while ((count = is.read (data, 0, tampon))! = -1) {
gos.write (données, 0, comte);
}
gos.finish ();
gos.flush ();
gos.close ();
}
/ **
* 文件压缩
*
* chemin @param
* @throws exception
* /
Public Static Void Compress (String Path) lève une exception {
compress (chemin, vrai);
}
/ **
* 文件压缩
*
* chemin @param
* @param supprime
* 是否删除原始文件
* @throws exception
* /
Public Static Void Compress (String Path, Boolean Delete) lève une exception {
Fichier fichier = nouveau fichier (chemin);
compress (fichier, supprimer);
}
/ **
* 数据解压缩
*
* Données @param
* @retour
* @throws exception
* /
octet statique public [] décompress (octet [] data) lève une exception {
BytearrayInputStream bais = new bytearrayInputStream (data);
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// 解压缩
décompression (Bais, baos);
data = baos.toByteArray ();
baos.flush ();
baos.close ();
bais.close ();
retourner les données;
}
/ **
* 文件解压缩
*
* fichier @param
* @throws exception
* /
Public static void décompress (fichier fichier) lève une exception {
décompress (fichier, true);
}
/ **
* 文件解压缩
*
* fichier @param
* @param supprime
* 是否删除原始文件
* @throws exception
* /
Public Static Void Decompress (fichier de fichier, booléen supprime) lève une exception {
FileInputStream fis = new FileInputStream (fichier);
FileoutputStream fos = new FileOutputStream (file.getPath (). Remplace (ext,
""));
décompression (FIS, FOS);
fis.close ();
fos.flush ();
fos.close ();
if (delete) {
file.delete ();
}
}
/ **
* 数据解压缩
*
* @param est
* @param os
* @throws exception
* /
Décompression du public statique vide (InputStream est, Outporestream OS)
lance l'exception {
GzipinputStream gis = new gzipinputStream (IS);
int count;
Données d'octets [] = nouveau octet [tampon];
while ((count = gis.read (data, 0, tampon))! = -1) {
OS.Write (données, 0, comte);
}
gis.close ();
}
/ **
* 文件解压缩
*
* chemin @param
* @throws exception
* /
La décompression publique statique du vide (chemin de chaîne) lève une exception {
décompression (chemin, vrai);
}
/ **
* 文件解压缩
*
* chemin @param
* @param supprime
* 是否删除原始文件
* @throws exception
* /
La décompression publique statique du vide (chemin de chaîne, booléen supprime) lève une exception {
Fichier fichier = nouveau fichier (chemin);
décompress (fichier, supprimer);
}
}