Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unzipping a file using Java IO  RSS feed

 
shirish katti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys here is the code with which i am trying to unzip a zipped file (Not to be done manually )

package com.unix;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Unzip {

/** Unzipp a Zip */
public Unzip(){}

public static void main(String args[]) throws IOException {

String inDirectory="C:\\searchResult";
String outDirectory="images/";

String[] filenames;
//String[] files=null;

File file = new File(inDirectory);
filenames =file.list();

System.out.println("FileName:"+filenames);

for(int i=0; i< filenames.length; i++) { //Don't forget this!!
File fs= new File(file.getName() + File.separator + filenames[i]);
System.out.println("File Name: " + fs);
System.out.println("Get Zip File: " + filenames[i]);

------------------------------------------------------------------------ZipFile f=new ZipFile(inDirectory+"\\"+ filenames[i]);
showing exception on the above line as too many entries


System.out.println("Got ZipFile:"+f);
//fs.delete();
Enumeration entries= f.entries();
System.out.println("Decompressing Data");

while(entries.hasMoreElements()){

ZipEntry entry=(ZipEntry) entries.nextElement();

InputStream in = f.getInputStream(entry);
FileOutputStream out = new FileOutputStream(outDirectory +
"" + entry.getName());

for(int ch=in.read();ch!=-1;ch=in.read()) out.write(ch);
out.close();
in.close();
} //end while
f.close();
} // end for
try {
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("Unzipping Completed Successfully.");
} //end main
} //end class

After running this code i am getting:

FileName:[Ljava.lang.String;@45a877
File Name: searchResult\search_result.zip
Get Zip File: search_result.zip
Exception in thread "main" java.util.zip.ZipException: too many entries in ZIP file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:203)
at java.util.zip.ZipFile.<init>(ZipFile.java:84)
at com.unix.Unzip.main(Unzip.java:35)


Can any one help me ???
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you use ZipInputStream instead of ZipFile then it should work fine.
 
shirish katti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I even tried a lot with ZipInputStream and was totaly lost..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? Show the code, and let us know how it did or did not work. Be sure to UseCodeTags when you post the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!