Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Problem definition: My java

ganeshmb (Programmer) (OP)
6 Jun 03 0:44
Problem definition:
My java program tries to unzip a zip file using java.util.zip classes. It fails right in the first line given below:
  ZipFile oZip = new ZipFile(strFileName);
    
Exception thrown:
The following exception is thrown with the above line.

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:105)
        at java.util.zip.ZipFile.<init>(ZipFile.java:70)

Important note:
1) The file name is properly defined and it is a valid zip file.  Any suspicion like a wrong file path or improper access privilege can be ruled out.
2) The zip file was not generated using java.util.zip. It was generated by a third party tool.  However, the zip file would open without any problem with unzip utility in Sun solaris or with a winzip program in Windows 2000. But, my java program using java.util.zip API is not able to unzip it.

My Questions:
What is so different about the zip file that Java implementation refuse s to unzip it?
Is there a standard protocol( on top of normal zipping mechanism)  that the zipping program should have followed so that my java program would unzip it correctly?
Or, is there any other way to unzip files correctly in Java?

I have tried several things, haven't had any luck so far.  Any pointer will be greatly appreciated.

Thanks in advance,
Ganesh
mailtorakeshkumar (Programmer)
6 Jun 03 2:57
I have successfully tried to unzip and read the contents of a zip file.This zip file too has been zipped using a third party tool.I am sending you the sample code for your benefit.

/**
*The following program unzips a zip file and reads and writes to the console the contents of each file  
*which are the costituents of the zip file one after another
*you can tailor this program to suit your requirements
*/
import java.util.zip.*;
import java.io.*;
import java.util.*;

public class readZipFile
    {

    public static void main( String[] args )
        {

        try
          {
           File f = new File( "D:\\Resume\\Rakesh Kumar.zip" );
           ZipFile z = new ZipFile( f );
           Enumeration e = z.entries();
           InputStream is = null;
           InputStreamReader isr = null;
           BufferedReader br = null;
           while( e.hasMoreElements() )
               {
               ZipEntry ze = (ZipEntry) e.nextElement() ;
               is = z.getInputStream( ze );
               isr = new InputStreamReader(is);
               br = new BufferedReader(isr);
               String line=null;
               while ( (line = br.readLine()) != null)
                        System.out.println(line);  
               }
            br.close();
            isr.close();
            is.close();
            z.close();
            
           }
           catch (ZipException zex)
              {
                zex.printStackTrace();  
              }
           catch (IOException ioe)
              {
                ioe.printStackTrace();  
              }
             
       }

ganeshmb (Programmer) (OP)
6 Jun 03 3:12
Rakesh!
Thank you very much for your reply.  I tried extracing my zip with your program, it's failing with the same exception.  As I mentioned in my previous mail, the same zip opens with Pkzip and winzip.  So, all my original questions are still pertinent.

What is so different about the zip file(generated by my third-party tool, not all as the one used by Rakesh is working fine)  that the Java implementation refuses to unzip it?
Is there a standard protocol( on top of normal zipping mechanism)  that the zipping program should have followed so that my java program would unzip it correctly?
Or, is there any other way to unzip files correctly in Java?

Thanks again for your effort.

Ganesh
mailtorakeshkumar (Programmer)
6 Jun 03 4:07
Ganesh,
Can you send me a sample zip file generated by your third party tool so that I can look further into it.
my e-mailId is mailto_rakesh_kumar@yahoo.com
jwenting (Programmer)
6 Jun 03 5:46
It seems indeed logical that the file may be in a format that Java doesn't natively support.
There are several flavours of the Zip format, and PKUnzip and Winzip support them all (or most of them) as well as being able to handle some errors transparently, maybe Java is more picky.
idarke (Programmer)
6 Jun 03 10:43
If you haven't already done so, you might try writing a small program that creates a zip file using the java.util.zip package and then try to unzip that.

That should give you a definitive answer.
ganeshmb (Programmer) (OP)
6 Jun 03 12:31
idarke,
I did that, the zip created using java.util.zip is opening properly for me.  

jwenting ,
It seems indeed logical that the file may be in a format that Java doesn't natively support.
You are correct. Looks like, my only option now is to look for a 3-rd party Java API to counter my 3-rd party generated zip .  I am looking for one over the internet.   Please let me know if you are aware of one such.  


Rakesh,
I have sent the problametic zip to you by mail.

Appreciate your help folks!
Ganesh



ganeshmb (Programmer) (OP)
11 Jun 03 12:34
I just wanted to let you know that I have obtained a solution for my problem.  For some weird reason, java.util.zip.ZipFile doesn't work but it works if I directly get hold of Zip input stream using java.util.zip.ZipInputStream.  I have just re-written the class of mailtorakeshkumar to explain this.  Thanks everyone for your valuable inputs!.  

/**
*The following program unzips a zip file and reads and writes to the console the contents of each file
*which are the costituents of the zip file one after another
*you can tailor this program to suit your requirements
*/
import java.util.zip.*;
import java.io.*;
import java.util.*;

public class ReadZipFile
    {

    public static void main( String[] args )
        {

        try
          {
           //ZipFile z = new ZipFile( f );
           /**
            * Pass file URL here
            */
           BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[0]));
           ZipInputStream zin = new ZipInputStream(in);
           System.out.println("After opening zip");
           InputStream is = null;
           InputStreamReader isr = null;
           BufferedReader br = null;
           ZipEntry ze;
           while((ze=zin.getNextEntry())!= null)
               {
               /**
                * Passing zipinput stream here, but this would
                * open the stream only for the current entry
                * selected with last getNextEntry.
                */
               isr = new InputStreamReader(zin);
               br = new BufferedReader(isr);
               String line=null;
               while ( (line = br.readLine()) != null)
                        System.out.println(line);
               }
            br.close();
            isr.close();
            zin.close();

           }
           catch (ZipException zex)
              {
                zex.printStackTrace();
              }
           catch (IOException ioe)
              {
                ioe.printStackTrace();
              }

       }
     }

michaelkrauklis (Programmer)
15 Jan 04 17:53
Just a quick note, the final solution will only work with text files.  It will give indeterminate results with binary files.  Stick to streams with binary, readers and writers are for text files in which you know the encoding.

MYenigmaSELF
myenigmaself@myenigmaself.gaiden.com
http://myenigmaself.gaiden.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in."

Kruzer (Programmer)
14 Sep 04 16:46
From this code snippet below how would I convert the ZipInputStream so that it only uses streams?

CODE


String file = "C:\\test.zip";
            String[] args = { "C:\junk.pdf" };
            //FileOutputStream f = new FileOutputStream(file);
            FileOutputStream f = new FileOutputStream(file);
            CheckedOutputStream csum =
                new CheckedOutputStream(f, new Adler32());
            ZipOutputStream out =
                new ZipOutputStream(new BufferedOutputStream(csum));
                        for (int i = 0; i < args.length; i++) {
                                BufferedReader in = new BufferedReader(new FileReader(args[i]));
                out.putNextEntry(new ZipEntry(args[i]));
                int c;
                while ((c = in.read()) != -1)
                    out.write(c);
                in.close();
            }
            out.close();
            // Checksum valid only after the file
            // has been closed!
            System.out.println("Checksum: " + csum.getChecksum().getValue());
            // Now extract the files:
            System.out.println("Reading file");
            FileInputStream fi = new FileInputStream(file);
            CheckedInputStream csumi =
                new CheckedInputStream(fi, new Adler32());
            ZipInputStream in2 =
                new ZipInputStream(new BufferedInputStream(csumi));
            ZipEntry ze;
            while ((ze = in2.getNextEntry()) != null) {
                int x;
                while ((x = in2.read()) != -1)
                    System.out.write(x);
            }
            System.out.println("Checksum: " + csumi.getChecksum().getValue());
            in2.close();
            // Alternative way to open and read
            // zip files:
            ZipFile zf = new ZipFile(file);
            Enumeration e = zf.entries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry) e.nextElement();
                System.out.println("File: " + ze2);
                // ... and extract the data as before
            }

Dano
dan_kryzer@hotmail.com
What's your major malfunction

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close