1

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

Ersen Osman
  • 7,067
  • 8
  • 47
  • 80
  • 1
    This answers your question: http://stackoverflow.com/questions/7190618/most-efficient-way-to-check-if-a-file-is-empty-in-java-on-windows An empty ZIP file is not a valid ZIP file so you just need to check if the file is empty. – Tunaki Dec 08 '15 at 17:02
  • Why not just catch and handle the exception ?!? – Marged Dec 08 '15 at 17:08

1 Answers1

0

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.

luso
  • 2,812
  • 6
  • 35
  • 50
Beck Yang
  • 3,004
  • 2
  • 21
  • 26