添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.
                @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, 2018 at 5:15
                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, 2018 at 5:16
                @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, 2018 at 5:22
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.