• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unzipping a file using Java IO

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I even tried a lot with ZipInputStream and was totaly lost..
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic