public class GzipUtil {
/**
* 解压gz文件
* @param inFileName
* @throws IOException
*/
public static String unGzipFile(String sourcedir) {
String ouputfile = "";
try {
//建立gzip压缩文件输入流
FileInputStream fin = new FileInputStream(sourcedir);
//建立gzip解压工作流
GZIPInputStream gzin = new GZIPInputStream(fin);
//建立解压文件输出流
ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
FileOutputStream fout = new FileOutputStream(ouputfile);
int num;
byte[] buf=new byte[1024];
while ((num = gzin.read(buf,0,buf.length)) != -1)
{
fout.write(buf,0,num);
}
gzin.close();
fout.close();
fin.close();
} catch (Exception ex){
System.err.println(ex.toString());
}
return ouputfile;
}
/**
* 将文件压缩成gz根式
* @param inFileName
* @throws IOException
*/
public static String GzipFile(String inFileName) {
String outFileName = inFileName + ".gz";
FileInputStream in = null;
try {
in = new FileInputStream(new File(inFileName));
}catch (FileNotFoundException e) {
System.out.println("Could not find the inFile..."+inFileName);
}
GZIPOutputStream out = null;
try {
out = new GZIPOutputStream(new FileOutputStream(outFileName));
}catch (IOException e) {
System.out.println("Could not find the outFile..."+outFileName);
}
byte[] buf = new byte[10240];
int len = 0;
try {
while (((in.available()>10240)&& (in.read(buf)) > 0)) {
out.write(buf);
}
len = in.available();
in.read(buf, 0, len);
out.write(buf, 0, len);
in.close();
System.out.println("Completing the GZIP file..."+outFileName);
out.flush();
out.close();
}catch (IOException e) {
e.printStackTrace();
}
return outFileName;
}
}