下文笔者讲述使用ZipOutputStream对zip压缩文件解压的方法分享,如下所示
ZipOutputStream解压zip文件的思路分享
1.根据文件路径生成FileInputStream
2.使用fileInputStream流放入ZipInputStream
生成ZipInputStream流
3.然后获取zipEntry条目
遍历输出文件
例:解压zip文件到指定目录
/**
* 解压zip文件到指定目录
* @param fileZip
* @param path_to_dest 解压文件的目录
* @throws IOException
*/
public static void readZip(String fileZip,String path_to_dest) throws IOException {
try (FileInputStream fis = new FileInputStream(fileZip);
ZipInputStream zis =
new ZipInputStream(new BufferedInputStream(fis))) {
ZipEntry entry;
// 从ZipInputStream读取每个条目,直到没有
// 发现更多条目,返回值为空
// getNextEntry()方法。
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
int size;
byte[] buffer = new byte[2048];
File fileOut = new File(path_to_dest+"\\"+entry.getName());
try (FileOutputStream fos =
new FileOutputStream(fileOut);
BufferedOutputStream bos =
new BufferedOutputStream(fos, buffer.length)) {
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


