首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取zip文件中的zip文件

读取zip文件中的zip文件
EN

Stack Overflow用户
提问于 2012-07-02 11:29:08
回答 4查看 12.7K关注 0票数 10

我有一个压缩文件,这是在一个压缩文件的文件夹内,请建议我如何使用压缩输入流读取它。

例如:

代码语言:javascript
运行
复制
abc.zip
    |
      documents/bcd.zip

如何读取zip文件里面的zip文件?

EN

回答 4

Stack Overflow用户

发布于 2016-11-11 15:30:39

如果您想要递归地查看zip文件中zip文件,

代码语言:javascript
运行
复制
    public void lookupSomethingInZip(InputStream fileInputStream) throws IOException {
        ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
        String entryName = "";
        ZipEntry entry = zipInputStream.getNextEntry();
        while (entry!=null) {
            entryName = entry.getName();
            if (entryName.endsWith("zip")) {
                //recur if the entry is a zip file
                lookupSomethingInZip(zipInputStream);
            }
            //do other operation with the entries..

            entry=zipInputStream.getNextEntry();
        }
    }

使用从文件派生的文件输入流调用该方法-

代码语言:javascript
运行
复制
File file = new File(name);
lookupSomethingInZip(new FileInputStream(file));
票数 7
EN

Stack Overflow用户

发布于 2012-07-02 12:25:49

下面的代码片段列出了一个ZIP文件在另一个ZIP文件中的条目。使其适应您的需求。ZipFile在幕后使用ZipInputStreams

代码片段使用Apache Commons IO,特别是IOUtils.copy

代码语言:javascript
运行
复制
public static void readInnerZipFile(File zipFile, String innerZipFileEntryName) {
    ZipFile outerZipFile = null;
    File tempFile = null;
    FileOutputStream tempOut = null;
    ZipFile innerZipFile = null;
    try {
        outerZipFile = new ZipFile(zipFile);
        tempFile = File.createTempFile("tempFile", "zip");
        tempOut = new FileOutputStream(tempFile);
        IOUtils.copy( //
                outerZipFile.getInputStream(new ZipEntry(innerZipFileEntryName)), //
                tempOut);
        innerZipFile = new ZipFile(tempFile);
        Enumeration<? extends ZipEntry> entries = innerZipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            System.out.println(entry);
            // InputStream entryIn = innerZipFile.getInputStream(entry);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // Make sure to clean up your I/O streams
        try {
            if (outerZipFile != null)
                outerZipFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        IOUtils.closeQuietly(tempOut);
        if (tempFile != null && !tempFile.delete()) {
            System.out.println("Could not delete " + tempFile);
        }
        try {
            if (innerZipFile != null)
                innerZipFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args) {
    readInnerZipFile(new File("abc.zip"), "documents/bcd.zip");
}
票数 5
EN

Stack Overflow用户

发布于 2019-02-25 15:43:23

最终让它修复了Manas Maji的答案。最小解决方案:

代码语言:javascript
运行
复制
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
import org.slf4j.*;

public void readZipFileRecursive(final Path zipFile) {
  try (final InputStream zipFileStream = Files.newInputStream(zipFile)) {
    this.readZipFileStream(zipFileStream);
  } catch (IOException e) {
    LOG.error("error reading zip file %s!", zipFile, e);
  }
}

private void readZipFileStream(final InputStream zipFileStream) {
  final ZipInputStream zipInputStream = new ZipInputStream(zipFileStream);
  ZipEntry zipEntry;
  try {
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
      LOG.info("name of zip entry: {}", zipEntry.getName());
      if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".zip")) {
        this.readZipFileStream(zipInputStream); // recursion
      }
    }
  } catch (IOException e) {
    LOG.error("error reading zip file stream", e);
  }
}

注意:不要在递归方法中关闭流。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11287486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档