• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Opening a zip file using the ZipFile class

 
Greenhorn
Posts: 3
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is a pretty simple question. I have a group of text files archived into a zip file. The zip file seems to be valid and uncorrupted because I can unzip and zip it at will from the command line with no problem. Ultimately, I would like to unzip this file into a specified directory. As a first step, I am trying to construct a new ZipFile object (in java.util.zip.*) for this file using the following line of code:

where name is a java String holding the fully resolved name of the zip file. The problem is that this command throws "java.util.zip.ZipException: error in opening zip file".

I have also tried the following:

The File object is constructed okay, but then I get the same exception when I try to construct the ZipFile object. I've searched this and other forums about similar problems with the ZipFile class, but most of them seem to be more complicated than this simple problem, involving JARs and deployment of apps.

Any ideas about what could be causing this would be greatly appreciated.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class ZipFile represents a file entry within a Zip file, and not the .zip File itself. So, in your case, there will a number of ZipFile instances that can be taken from the actual .zip file.

Google for some examples. I'm sure you'll get it in no time.
 
Jeff Bullard
Greenhorn
Posts: 3
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Aditya. I think I was/am confused by the following link which seems to provide a function to unzip a file into a directory:

http://www.java2s.com/Tutorial/Java/0180__File/unzipFileIntoDirectory.htm

This function seems to take the zip file itself, seemingly an instance of class ZipFile, as an argument and then unzips it into the directory which is also supplied as an argument to the function.

I'll keep checking other sites to see if I can find examples of what you are talking about.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. Please forget about my last comment. It was all wrong.

For your original problem, can you please elaborate how exactly did you make the zip file. Especially, is there a chance that any app-specific algorithm was used for compression, as opposed to the standard zip algorithm?

Also, try to debug within ZipFile class and check which code throws this ZipException in your case.
 
Jeff Bullard
Greenhorn
Posts: 3
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for following up on this. I am still completely stuck, but I also haven't been able to give it as much attention for the last 24 hours as I would have liked.

Yes, I used the command line zip command on Mac OS X. I don't know how standard that utility is, but I do know that WinZip can unzip the the same file when I copy it to Windows XP and I can unzip it on the two flavors of Linux I have tried (CentOS and Fedora Core 10 so far). The other piece of information I have about this utility is the man page for the Mac OS X zip utility, which says "zip is a compression and file packaging utility for Unix, VMX, MSDOS, OS/2, Windows 9x/NT/XP, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS. It is analogous to a combination of the Unix commands tar and compress and is compatible with PKZIP (Phil Katz's ZIP for MSDOS systems)."

I used the recursive flag to make the file because I am zipping an entire directory with a couple thousand files in that directory. The directory name is "cempart" and the exact command I use to do the zip is: zip -r cempart.zip cempart

I thought that the recursive flag might be the problem, so as a test I just zipped two files together like this: zip test.zip fileA fileB

But even this simple zip file throws the same ZipException. When I run my code in debug mode within Netbeans, I try to step into the ZipFile constructor to see where the exception is thrown, as you suggested, but the exception is thrown immediately. I never get inside the ZipFile constructor at all. It is strange; maybe I am using the debugger incorrectly in this case, but I have had good luck stepping into Java functions before this.

Thanks again for the help, Aditya.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, the actual opening of a ZipFile happens in a (private static) native method. That means you need the OpenJDK source to see its implementation.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I have never used the class ZipFile. I did work with zip files, however, using ZipInputStream. It worked beautifully. Do you think it can serve your purpose?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic