Monday, June 20, 2011

What to do in I/O and File ?

18 June 2011
How to unzip a .zip file ?

public static void unzip(String filePath, InputStream is) {
ZipInputStream in = null;
try {
in = new ZipInputStream(is);
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
if (entry.isDirectory()) {
File file1 = new File(filePath + entry.getName());
file1.mkdir();
} else {
FileOutputStream outputfile = new FileOutputStream(filePath + entry.getName());
int data = 0;
while ((data = in.read()) != -1) {
outputfile.write( data );
}
outputfile.close();
}
}
} catch (IOException e) {
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ignored) {
}
in = null;
}
}

No comments:

Post a Comment