版权声明:自由转载,无需过问 https://blog.csdn.net/Next__One/article/details/78668923
Java中有可以直接解压gzip文件的输入流。
/**
* 获取文件名(去掉.gz后缀)
* @param path
* @return
*/
public static String getPrefix(String path) {
int index = path.lastIndexOf('.');
return path.substring(0, index);
}
public static void unGzip(String srcPath) {
unGzip(new File(srcPath));
}
/**
* 解压Gzip
* @param src 压缩文件
*/
public static void unGzip(File src) {
String path = getPrefix(src.getAbsolutePath());
GZIPInputStream gzs = null;
BufferedOutputStream bos = null;
try {
gzs = new GZIPInputStream(new FileInputStream(src));
bos = new BufferedOutputStream(new FileOutputStream(path));
byte[] buf = new byte[102400];
int len = -1;
while ((len = gzs.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
FileUtil.close(gzs, bos);
}
}
/**
* 关闭流
* @param io
*/
public static void close(Closeable ...io){
for (Closeable temp : io) {
try {
if(temp != null){
temp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}