Unzip file
suggest changeprivate boolean unpackZip(String path, String zipname){
InputStream is;
ZipInputStream zis;
try
{
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null){
// write the entry to a file
filename = ze.getName();
// Need to create directories if not exists, or
// it will generate an Exception...
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(path + filename);
// read the zip entry and write it
while ((count = zis.read(buffer)) != -1){
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
zis.close();
}
catch(IOException e){
e.printStackTrace();
return false;
}
return true;}
Entry names are appended to path as written. A name containing .. can write outside that directory. path needs a trailing separator, and the streams are not closed if reading throws before zis.close().
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents