Android Tutorial

12.27.2011

Unzip file not password in Android



/****************************************************************************
* Unzip file not password
*
* Example:
* String path = "/sdcard/files.zip";
* String folderOut = "/data/data" + mCtx.getPackageName() + "/folder";
* File fi = new File(path);
* InputStream zipIn = new FileInputStream(fi);
* // if asset : AssetManager assetManager = getResources().getAssets();
* // InputStream zipIn = assetManager.open("files.zip");
* unZipFileNotPass(zipIn, folderOut);
*
******************************************************************************/



public static void unZipFileNotPass(InputStream zipIn, String folderOut) {
final int BUFFER_SIZE = 8192;
try {
//
BufferedInputStream bis = new BufferedInputStream(zipIn,
BUFFER_SIZE);
ZipInputStream zis = new ZipInputStream(bis);
ZipEntry ent;
while ((ent = zis.getNextEntry()) != null) {
//
String fn = folderOut + "/" + ent.getName();
if (ent.isDirectory()) {
File f = new File(fn);
if (!f.exists()) {
f.mkdirs();
}
} else {
String nameFile = ent.getName();
if (nameFile.contains("/")) {
String folder = nameFile.substring(0,
nameFile.indexOf("/"));
File f = new File(folderOut + "/" + folder);
if (!f.exists()) {
f.mkdirs();
}
}
//
FileOutputStream fos = new FileOutputStream(fn);
byte[] buf = new byte[BUFFER_SIZE];
int cnt = 0;
while ((cnt = zis.read(buf)) != -1) {
fos.write(buf, 0, cnt);
}
fos.flush();
fos.close();
}
}
zis.close();
} catch (Exception e) {
Log.e("Decompress", "unzip");
}
}

No comments:

Post a Comment