如何在内存中提取zip文件

km0tfn4u  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(599)

我读过很多关于在磁盘上提取zip文件的主题。但我有一个需要在内存中提取zip的用例。zip文件再次包含zip文件的列表。我是在阅读了stack overflow中的几篇文章之后问这个问题的。你能分享一些关于如何在内存中解压文件的信息吗?

3lxsmp7m

3lxsmp7m1#

如果要读取嵌套的.zip文件,可以尝试使用 ZipInputStream (就像已经提到的)检查一下 ZipEntry (s) 也是一个*.zip文件,在这种情况下,它可以作为下一个.zip文件递归读取。比如:

private static void readZipInputStream(
        InputStream inputStream, BiConsumer<ZipEntry, ByteArrayOutputStream> consumerFunction) throws IOException {

    try (ZipInputStream zipInput = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = zipInput.read(buffer)) != -1) {
                outStream.write(buffer, 0, length);
            }

            if (entry.getName().endsWith(".zip")) {
                // need to go deeper...
                ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
                readZipInputStream(inStream, consumerFunction);
            } else {
                // do something...
                consumerFunction.accept(entry, outStream);
            }
        }
    }
}

例如,有一个结构如下的zip文件:

file.zip
├─1+2.zip
│ ├─1.zip
│ │ └─1.txt
│ └─2.zip
│   └─2.txt
└─3.zip
  └─3.txt

以及使用 readZipInputStream 功能如下:

public class Application {

    public static void main(String[] args) throws IOException {
        String path = "file.zip";
        try (FileInputStream inputStream = new FileInputStream(Paths.get(path).toFile())) {
            readZipInputStream(
                    inputStream,
                    (entry, outputStream) -> {
                        System.out.println(entry.getName());
                        System.out.println("--------------------------------");
                        System.out.println(outputStream.toString());
                        System.out.println("--------------------------------");
                    }
            );
        }
    }
}

将打印三个.txt文件的内容:

1.txt
--------------------------------
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
--------------------------------
2.txt
--------------------------------
- Integer vel sem consectetur, ullamcorper leo quis, consequat mauris.
- Nulla efficitur sapien at velit fermentum condimentum.
- Vestibulum elementum nulla ut ipsum tempus, ut molestie sem sollicitudin.
--------------------------------
3.txt
--------------------------------
Morbi tincidunt ornare mi. Sed id risus tortor. Interdum et malesuada 
fames ac ante ipsum primis in faucibus. Pellentesque tincidunt, 
nulla a interdum porta, orci elit ultricies leo, in maximus orci 
tortor pulvinar est. Curabitur eget fermentum risus. Vestibulum euismod 
convallis eros, nec blandit neque blandit at.
--------------------------------
7hiiyaii

7hiiyaii2#

java类java.util.zip.zipinputstream允许您将数据从zip存档读取到字节数组中。

相关问题