javaandroidzip4j

Zip4j libary: How do I check if the zip file is empty?


Hello I am using the zip4j library for Java to extract zip file to a directory.

I get this zip file from the internet.

Everything works fine however I get this exception when the zip file is empty.

12-08 16:52:00.964 29829-30843/ W/System.err: net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: java.io.IOException: offset < 0: -20
12-08 16:52:00.964 29829-30843/ W/System.err:     at net.lingala.zip4j.core.HeaderReader.readZip64EndCentralDirLocator(HeaderReader.java:546)
12-08 16:52:00.965 29829-30843/ W/System.err:     at net.lingala.zip4j.core.HeaderReader.readAllHeaders(HeaderReader.java:82)
12-08 16:52:00.965 29829-30843/ W/System.err:     at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:425)
12-08 16:52:00.965 29829-30843/ W/System.err:     at net.lingala.zip4j.core.ZipFile.getFileHeaders(ZipFile.java:688)
12-08 16:52:00.965 29829-30843/ W/System.err:     at com.myapp.services.DownloadContentIntentService.onHandleIntent(DownloadContentIntentService.java:124)
12-08 16:52:00.965 29829-30843/ W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
12-08 16:52:00.965 29829-30843/ W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 16:52:00.965 29829-30843/ W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-08 16:52:00.965 29829-30843/ W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
12-08 16:52:00.965 29829-30843/ W/System.err: Caused by: net.lingala.zip4j.exception.ZipException: java.io.IOException: offset < 0: -20
12-08 16:52:00.965 29829-30843/ W/System.err:     at net.lingala.zip4j.core.HeaderReader.setFilePointerToReadZip64EndCentralDirLoc(HeaderReader.java:816)
12-08 16:52:00.965 29829-30843/ W/System.err:     at net.lingala.zip4j.core.HeaderReader.readZip64EndCentralDirLocator(HeaderReader.java:517)
12-08 16:52:00.965 29829-30843/ W/System.err:   ... 8 more
12-08 16:52:00.966 29829-30843/ W/System.err: Caused by: java.io.IOException: offset < 0: -20
12-08 16:52:00.966 29829-30843/ W/System.err:     at java.io.RandomAccessFile.seek(RandomAccessFile.java:600)
12-08 16:52:00.966 29829-30843/ W/System.err:     at net.lingala.zip4j.core.HeaderReader.setFilePointerToReadZip64EndCentralDirLoc(HeaderReader.java:814)
12-08 16:52:00.966 29829-30843/ W/System.err:   ... 9 more

This is the line in question.

 ZipFile zipFile = new ZipFile(temp);

When debugging, I found this only happens with empty zip files i.e. an archive with nothing inside.

However the problem I am having is how can I check the amount of files inside the archive because I have found if I do this

 List<FileHeader> fileHeaders = zipFile.getFileHeaders();
 if(fileHeaders != null && fileHeaders.size() > 0){

 }

I am able to get information about the zip content I.e. amount of files and size etc.

But there is one big issue. The exception gets thrown here

The exception gets thrown at this point

 ZipFile zipFile = new ZipFile(temp);

And I need the instance to access the file headers

Please help


Solution

  • The API zip.isValidZipFile() will return false if input file contains no zip entry. (Tested with zip4j v1.3.2)

    BTW, exception is not thrown when new ZipFile object.