0

I receive a zip file from a server, which has to be processed. I am able to Unzip it manually. So I believe zip file is not corrupted.

Below code read the files from Zip file without unzipping it.

import java.io.File;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileReader {
    public static void main(String[] args) throws Exception{
        File file = new File("/Users/wire/data.zip"); 
        ZipFile zipFile = new ZipFile(file.getPath());//Getting error here
        for (Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            System.out.println(entry.getName());
        }
    }
}

Throws below error:

Exception in thread "main" java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:225)
    at java.util.zip.ZipFile.<init>(ZipFile.java:155)
    at java.util.zip.ZipFile.<init>(ZipFile.java:126)
    at com.log.ZipFileReader.main(ZipFileReader.java:11)

But if I unzip it manually and zip it back, above code is working with no issues, below is the output:

data/
data/file/
data/file/log.txt
__MACOSX/
__MACOSX/data/
__MACOSX/data/file/
__MACOSX/data/file/._log.txt

Note: The zip file what I receive from server size is 5MB. But once I unzip and zip it back, new zip file size became 8MB.

Added:

Apples-MacBook-Pro:~ test$ unzip -v data.zip
Archive:  data.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of data.zip or
        data.zip.zip, and cannot find data.zip.ZIP, period.
Molay
  • 1,154
  • 2
  • 19
  • 42
  • @ScaryWombat It shouldn't matter, check for yourself. Anyway, I would expect an `IOException` if the file could not be found, not a `ZipException`. – Tim Biegeleisen Apr 18 '18 at 05:15
  • 1
    So what if you do `unzip -v data.zip` at a prompt? What compression method does it say has been used? – David Conrad Apr 18 '18 at 05:16
  • Yep, just noticed it was throwing `ZipException` – Scary Wombat Apr 18 '18 at 05:16
  • @TimBiegeleisen I tried that as well, still throws same error – Molay Apr 18 '18 at 05:17
  • @DavidConrad getting this message : Archive: data.zip End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. – Molay Apr 18 '18 at 05:22
  • unable to unzip with this command : unzip -v data.zip – Molay Apr 18 '18 at 05:23
  • So, it must be a self-extracting zip file? (What are you successfully unpacking it with?) Zip files can have extra data at the beginning, and *some* unzip software knows how to ignore that and search for the zip file within the larger file. This was by design so a small unzip executable could be stuck on the front of the file. Obviously, Java isn't one that can handle that. – David Conrad Apr 18 '18 at 05:23
  • i double click on the zip file, it's able to unzip, sorry i am new to MAC – Molay Apr 18 '18 at 05:25
  • Maybe try `file data.zip` and see what file magic thinks the file is. – David Conrad Apr 18 '18 at 05:26
  • data.zip: bzip2 compressed data, block size = 900k – Molay Apr 18 '18 at 05:27
  • It's definitely not a zip file. Sounds like it's a tar.bz2 file. – David Conrad Apr 18 '18 at 05:28
  • Thanks for pointing it out, will explore further on this. – Molay Apr 18 '18 at 05:34

1 Answers1

0

Based on the comments, the file is a misnamed .tar.bz2 file, not a real .zip file. According to this answer and this answer, it can be unpacked with the Apache Commons Compress library.

David Conrad
  • 15,432
  • 2
  • 42
  • 54