Validate zip archive in a strict fashion

170 Views Asked by At

There is a semi-broken zip-archive that could be extracted from but gives a semi-broken file.

Ubuntu Archive Manager extracts from the archive, but gives a warning:

enter image description here

The following Java code gives no errors at all:

 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;

 import org.apache.commons.io.IOUtils;

 ...

 public static boolean isValidZipFile(final File file) {
        if (file.length() == 0) return false;

        try (ZipFile zipFile = new ZipFile(file)) {
            final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();

            while (entries.hasMoreElements()) {
                ZipArchiveEntry entry = entries.nextElement();

                if (entry.getName().endsWith(".txt")) {
                    final InputStream is = zipFile.getInputStream(entry);

                    byte[] bytes = IOUtils.toByteArray(is);

                    return true;
                }
            }
        } catch (IOException e) {
            return false;
        }

        return false;
    }

Dependencies:

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.20</version>
    </dependency>

N.B. Contents of the archive is a single text file (that's agreed in advance).

Q. How to strictly validate zip file so it is considered invalid for such a case?

P.S. It's not absolutely necessary to use apache-commons-compress, any other library could be employed as well. The ultimate target is validation reliability.

0

There are 0 best solutions below