解压缩工具类

来源:互联网 发布:mysql没有可视化界面 编辑:程序博客网 时间:2024/06/02 04:41
/**
* 解压缩工具类
* @author Administrator
*
*/
public class GzipUtil {
// 压缩文件
public static void zip(File srcFile, File targetFile) {
InputStream is = null;
GZIPOutputStream gos = null;
try {
is = new FileInputStream(srcFile);
gos = new GZIPOutputStream(new FileOutputStream(targetFile));
byte buff[] = new byte[1024];
int len = 0;
while ((len = is.read(buff)) != -1) {
gos.write(buff, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseQuitely(is);
CloseQuitely(gos);
}
}
/**
* 解压缩
* @param srcFile 源文件 压缩文件
* @param targetFile 目标文件 普通文件
*/
public static void unZip(File srcFile, File targetFile) {
GZIPInputStream gis = null;
OutputStream out = null;
try {
gis = new GZIPInputStream(new FileInputStream(srcFile));
out = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = gis.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseQuitely(gis);
CloseQuitely(out);
}
}
/**
* 关闭流
*/
public static void CloseQuitely(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* 解压缩
* @param in 输入流 压缩包
* @param targetFile 目标文件 普通文件
*/
public static void unZip(InputStream in,File targetFile){
GZIPInputStream gis=null;
FileOutputStream fos=null;
try {
gis=new GZIPInputStream(in);
fos=new FileOutputStream(targetFile);
byte []buffer=new byte[1024];
int len=0;
while((len=gis.read(buffer))!=-1){
fos.write(buffer, 0,len);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
CloseQuitely(gis);
CloseQuitely(fos);
}

}
}
0 0
原创粉丝点击