Home > Software design >  How to convert a byte array of zip files to ZipFile with type as ZipFile?
How to convert a byte array of zip files to ZipFile with type as ZipFile?

Time:06-30

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?

CodePudding user response:

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

  • Related