0

Currently, when trying to read a ZIP file, I cannot differentiate between an empty ZIP file and a corrupted/illegal ZIP file, as in both cases, the following while loop is never going to run:

ZipInputStream zis = new ZipInputStream(bais);
ZipEntry ze = null;
boolean hasContent = false;
while ((ze = zis.getNextEntry()) != null) {
    hasContent = true;
}

Is there a way to differentiate between empty file and illegal/corrupted file?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • Assuming `bais` is a ByteArrayInputStream, why not just check whether your byte array’s length is zero? – VGR Dec 06 '17 at 16:35
  • I'd like to know whether the file is a ZIP file or not. So an empty file is only one case. But the file could also be, for instance, a text file, an image file, whatever.. that cannot of course be decoded by a ZIP extractor. Currently, I have no way to indicate it, as when the file is not a ZIP file, I only get no results, like if it was an empty file – SomethingSomething Dec 07 '17 at 09:37
  • Use [Files.probeContentType](https://docs.oracle.com/javase/9/docs/api/java/nio/file/Files.html#probeContentType-java.nio.file.Path-). – VGR Dec 07 '17 at 13:21
  • You can use `new ZipFile(file)` to validate file: https://stackoverflow.com/a/2086792/495907 – alper Dec 08 '17 at 10:11

0 Answers0