简体   繁体   中英

how to extract zip file in memory

I have read many topics where zip file is extracted on disk. But I have a use case where zip need to be extracted in memory. ZIP file again contains list of zip file. I am asking this question after going through several posts in stack overflow. Can you please share any post / link which has some information about how to unzip files in memory?

If you want to read nested.zip files, you could try using ZipInputStream (like it was already mentioned) and check if ZipEntry (s) is also a *.zip file, in which case it can be recursively read as next.zip file. Something like:

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);
            }
        }
    }
}

so for instance having a zip file with structure like this:

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

and using readZipInputStream function like this:

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("--------------------------------");
                    }
            );
        }
    }
}

content of three.txt files will be printed:

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.
--------------------------------

The java class java.util.zip.ZipInputStream allows you to read the data from a Zip archive into a byte array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM