public static boolean unzipPackage(String zipname) {
LogUtil.v(TAG, "unzip");
InputStream input;
ZipInputStream zis;
String dirName;
String fileName;
Context context = HybridApplication.getContext();
try {
input = context.getAssets().open(zipname);
zis = new ZipInputStream(new BufferedInputStream(input));
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
int count;
LogUtil.v(TAG, "unzip " + context.getFilesDir().toString());
File path = context.getFilesDir();
while ((zipEntry = zis.getNextEntry()) != null) {
//filename = formatDirectory(zipEntry.getName());
dirName = getDirectory(zipEntry.getName());
if (zipEntry.isDirectory()) {
File directory = new File(context.getFilesDir().getAbsolutePath() + File.separator + dirName);
if (!directory.exists()) {
directory.mkdir();
}
continue;
//File fileDir = context.getDir(dirName, Context.MODE_PRIVATE);
File fileDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + dirName);
File newFile = new File(fileDir, fileName);
FileOutputStream fos = new FileOutputStream(newFile);
while ((count = zis.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
fos.close();
zis.closeEntry();
}
zis.close();
} catch (IOException e) {
LogUtil.v("PackageUpdater", "unzip failed");
e.printStackTrace();
return false;
} finally {
}
return true;
}
/**
* 从资源路径中获取目录
* @param url
* @return
*/
private static String getDirectory(String url) {
return url.substring(0, url.lastIndexOf("/"));
}
/**
* 从资源目录中获取文件名
* @param url
* @return
*/
private static String getFileName(String url) {
return url.substring(url.lastIndexOf("/") + 1, url.length());
}
LogUtil.v(TAG, "unzip");
InputStream input;
ZipInputStream zis;
String dirName;
String fileName;
Context context = HybridApplication.getContext();
try {
input = context.getAssets().open(zipname);
zis = new ZipInputStream(new BufferedInputStream(input));
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
int count;
LogUtil.v(TAG, "unzip " + context.getFilesDir().toString());
File path = context.getFilesDir();
while ((zipEntry = zis.getNextEntry()) != null) {
//filename = formatDirectory(zipEntry.getName());
dirName = getDirectory(zipEntry.getName());
if (zipEntry.isDirectory()) {
File directory = new File(context.getFilesDir().getAbsolutePath() + File.separator + dirName);
if (!directory.exists()) {
directory.mkdir();
}
continue;
}
//File fileDir = context.getDir(dirName, Context.MODE_PRIVATE);
File fileDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + dirName);
File newFile = new File(fileDir, fileName);
FileOutputStream fos = new FileOutputStream(newFile);
while ((count = zis.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
fos.close();
zis.closeEntry();
}
zis.close();
} catch (IOException e) {
LogUtil.v("PackageUpdater", "unzip failed");
e.printStackTrace();
return false;
} finally {
}
return true;
}
/**
* 从资源路径中获取目录
* @param url
* @return
*/
private static String getDirectory(String url) {
return url.substring(0, url.lastIndexOf("/"));
}
/**
* 从资源目录中获取文件名
* @param url
* @return
*/
private static String getFileName(String url) {
return url.substring(url.lastIndexOf("/") + 1, url.length());
}