Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem definition: My java

Status
Not open for further replies.

ganeshmb

Programmer
Dec 5, 2001
32
0
0
US
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:
Code:
  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
 
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( &quot;D:\\Resume\\Rakesh Kumar.zip&quot; );
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();
}

}

 
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
 
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
 
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.
 
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.
 
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



 
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!.
Code:
/**
*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(&quot;After opening zip&quot;);
           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();
              }

       }
     }
 
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:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top