简体   繁体   中英

How to convert a byte array of zip files to ZipFile with type as ZipFile?

byte[] zipBytes = fileService.get("RINV_242_20220629-150309.ZIP");
ZipFile zipFile = new ZipFile(zipBytes);

I'm not sure if my code will give you a better understanding of my problem. But I want to convert byte array to zipFile, what do I need to do?

You can't use java.util.zip.ZipFile without saving it to a file on a file system. If you want to get it from a byte[] without saving, you need to use java.util.zip.ZipInputStream , and wrap the byte[] in a ByteArrayInputStream .

Something like:

try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
    // ...
}

The thing you lose compared to ZipFile is that you can't enumerate the files or request individual files. Instead, you have to iterate over zip entries with getNextEntry() .

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