posted 19 years ago
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