package com.ustcinfo.hello;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class StringDemo {
public static void main(String[] args) {
String inputFileName ="D:\\Program Files\\Home Series\\Home Ftp Server\\test\\A_050157_000_20140111_01_0_001.data.gz" ;
doUncompressed(inputFileName);
}
public static void doUncompressed(String inputFileName){
if(!getExtendion(inputFileName).equalsIgnoreCase("gz")){
System.out.println("不是gz的压缩文件");
System.exit(1);
}
String outFileName = obtainOutFileName(inputFileName);
System.out.println("获得解压后的文件名");
GZIPInputStream gis = null;
FileOutputStream fos = null;
byte[] buffer = new byte[1024];
int size = 0;
try {
gis = new GZIPInputStream(new FileInputStream(inputFileName));
fos = new FileOutputStream(outFileName);
while((size=gis.read(buffer))> 0){
fos.write(buffer,0,size);
}
fos.flush();
System.out.println("解压成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.close();
gis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @param inputFileName 输入解压的文件名
* @return 返回解压后的文件名
*/
public static String obtainOutFileName(String inputFileName){
String outFileName = "";
int index = inputFileName.lastIndexOf(".gz");
if(index > 0 && index < inputFileName.length()-1){
outFileName = inputFileName.substring(0, index);
}
return outFileName;
}
/**
*
* @param inputFileName 输入解压的文件名
* @return 获得解压文件的拓展名
*/
public static String getExtendion(String inputFileName){
String extend = "";
int index = inputFileName.lastIndexOf(".");
if(index > 0 && index < inputFileName.length()-1){
extend = inputFileName.substring(index+1);
}
return extend;
}
}