This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Problme with unzipping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problme with unzipping " Watch "Problme with unzipping " New topic
Author

Problme with unzipping

Geetha Desh
Greenhorn

Joined: Dec 14, 2005
Posts: 3
Hi

I m writing code to list and extract all the files when a directory name is passed as an argument.
my code works fine if the directory contains all the zip files.it extracts all the files.

the problem i m getting when a directory contains subdirectory .it is giving me exception..
=========================
the code works fine if: it extratct all the files
geetha--directory name
|a1.zip
|a2.zip
========================

the code doesnt work when
===================
geetha---root directory
|_geetha1-sub directory
|__geetha2-subdirectory under geetha1 directory
|____a1.zip
|____ a2.zip
==================================
The code is as follows:

public static void Unzip(File fin,File fout)
{
try {
//File dir = new File(args[0]);
File[] children = fin.listFiles();
int length =children.length;
for (int i=0; i<length; i++) {
File f = children[i];
System.out.println("the File name is "+f.getName());
if(f.getName().endsWith(".zip"))
{System.out.println("Zip file found "+f.getName());
Unzipme(f,fout);
}
}//forloop
}//try
catch(Exception e)
{ System.out.println("Exception"+e); }//catch

}
public static void Unzipme(File fname,File fout) {
//System.out.println(fname);
try {
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(fname);
Enumeration e = zipfile.entries();
while(e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
System.out.println("Extracting: " +entry);
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
File output_file = new File (fout,entry.getName ());
FileOutputStream fos = new FileOutputStream(output_file);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count); }
dest.flush();
dest.close();
is.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}

any suggestions?
Thanks in advance
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

Do you know this guy? Seems like you're working on the same program.

So what is the exception you're seeing? With a quick glance at the code, I don't see the problem.


[Jess in Action][AskingGoodQuestions]
Geetha Desh
Greenhorn

Joined: Dec 14, 2005
Posts: 3
Hi

i m getting filenot found exception..
Geetha Desh
Greenhorn

Joined: Dec 14, 2005
Posts: 3
Hi.

i m getting this exception

Extracting: F:\anand photos/sonu.JPG
java.io.FileNotFoundException: F:\F:\anand photos\sonu.JPG (The filename, direct
ory name, or volume label syntax is incorrect)
Thanks and Regards
payal agrawal
Ranch Hand

Joined: Oct 11, 2005
Posts: 44
Look at the URL below:

How to extract file/files from a zip file
http://www.java-tips.org/content/view/600/2/
 
Consider Paul's rocket mass heater.
 
subject: Problme with unzipping