Search code examples
javazip

Way to check if a ZIP file is empty?


I'm using a pre-built method that returns an object of type File. It's known that the file I'm receiving is a zip of a directory. What's the quickest way I could check if it's empty i.e. has no files? Do i need to necessarily extract it?

EDIT: I think I may have ambiguously worded the title. My requirement is to check that the number of entries in the zipped directory are zero. Sorry for any confusion.


Solution

  • To check is a zip file is empty:

    boolean isEmpty;
    try (ZipFile zipFile = new ZipFile(file)) {
        isEmpty = (zipFile.size() == 0);
    }