Java 解析嵌套zip文件解压缩

摩西科里 2017-12-29 11:33:12
遇到一个问题,需要解析zip文件,而且是嵌套的zip文件,比如,a.zip里面包含b.zip,c.zip,o.zip或者其他文件,,b.zip文件里包含有c.json,c.zip文件包含有cc.txt等等。现在想要解析出a.zip下面的b.zip下面的c.json文件夹里面的内容,该如何解析呢?简单的zip文件解压缩度娘可以出来,嵌套的zip解压缩感觉有点难度,求大神指教,求思路,求代码。
...全文
1452 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
摩西科里 2018-03-14
  • 打赏
  • 举报
回复
public static Map<String, String> readMultipartFileFile(MultipartFile file) { Map<String, String> resultMap = new HashMap<String, String>(); File f = null; File temp = null;//嵌套压缩文件 try { f = File.createTempFile("tmp",null);//在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称 file.transferTo(f); ZipFile zf = new ZipFile(f); InputStream in = new BufferedInputStream(new FileInputStream(f)); ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; StringBuffer json = new StringBuffer(); //临时文件的父文件路径 String parentZipParent = f.getParent(); while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { }else{ if (ze.getName().equals("xxx.zip")) { temp = new File(parentZipParent + "\\" + ze.getName()); if (!temp.getParentFile().exists()) temp.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(temp); //通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流 InputStream is = zf.getInputStream(ze); int len = 0; while ((len = is.read()) !=-1) { os.write(len); } ZipFile zipFile = new ZipFile(temp); InputStream inputStream = new BufferedInputStream(new FileInputStream(temp)); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry = null; while ((zipEntry = zipInputStream.getNextEntry()) != null) { if (zipEntry.isDirectory()) { }else { if (zipEntry.getName().equals("xxx.json")) { if (zipEntry.getSize() > 0){ BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry), "utf-8")); String line = null; while ((line = br.readLine()) != null) { json.append(line.trim()); } } } } //解析获取的json文件,读取想要的值xxx,存于resultMap返回 //略。。。 os.close(); is.close(); //关闭文件流 zipInputStream.close(); inputStream.close(); zipFile.close(); } } } zin.close(); } } catch (Exception e) { e.printStackTrace(); }finally { f.delete(); temp.delete(); } return resultMap; }
丶丶路遥 2018-03-13
  • 打赏
  • 举报
回复
核心思想是递归,这个地方,可以用两个函数,a()掉b(),b()调用a(),a()负责解压,b()通过递归读取文件,如果是zip,那么再调用a()就行了,这时候a()又会解压之后调用b();
摩西科里 2018-01-01
  • 打赏
  • 举报
回复
引用 3 楼 qingzhongren 的回复:
[quote=引用 1 楼 tianfang 的回复:]
别害怕,解压后的文件再判断一下是不是zip文件,是的话就再解压就是了



ZipFile zf = new ZipFile(file);
InputStream in = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
// System.out.print("directory - " + ze.getName() + " : "
// + ze.getSize() + " bytes");
// System.out.println();
} else {
System.err.println("file - " + ze.getName() + " : "
+ ze.getSize() + " bytes");

long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}


解压后,可以获取到解压后的文件名称(ze.getName()),判断这个文件名称是否是zip文件,但是如何继续解压呢?求关键代码,谢谢!

[/quote]






线上求解。
摩西科里 2018-01-01
  • 打赏
  • 举报
回复
引用 2 楼 lingwu1997 的回复:
用方法递归嘛,判断是不起zip文件就好了,是就递归执行。
嗯,递归可以的,求关键代码提示,谢谢。
摩西科里 2018-01-01
  • 打赏
  • 举报
回复
引用 1 楼 tianfang 的回复:
别害怕,解压后的文件再判断一下是不是zip文件,是的话就再解压就是了
ZipFile zf = new ZipFile(file); InputStream in = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { if (ze.isDirectory()) { // System.out.print("directory - " + ze.getName() + " : " // + ze.getSize() + " bytes"); // System.out.println(); } else { System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes"); long size = ze.getSize(); if (size > 0) { BufferedReader br = new BufferedReader( new InputStreamReader(zf.getInputStream(ze))); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } 解压后,可以获取到解压后的文件名称(ze.getName()),判断这个文件名称是否是zip文件,但是如何继续解压呢?求关键代码,谢谢!
领悟97 2017-12-30
  • 打赏
  • 举报
回复
用方法递归嘛,判断是不起zip文件就好了,是就递归执行。
tianfang 2017-12-30
  • 打赏
  • 举报
回复
别害怕,解压后的文件再判断一下是不是zip文件,是的话就再解压就是了

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧