/**
* 解压Assets目录下ZIP包
*
* @param context 上下文
* @param assetName 被解压的压缩包名称
* @param outputDirectory 解压后存放的路径
*/
public static void unZipFromAssets(Context context, String assetName, String outputDirectory) {
try {
InputStream dataSource = context.getAssets().open(assetName);
ZipInputStream in = new ZipInputStream(dataSource);
ZipEntry entry = in.getNextEntry();
while (entry != null) {
LOG.debug("CAR-SHOW", "unZipFromAssets ZipEntry name = " + entry.getName());
// 创建以zip包文件名为目录名的根目录
File file = new File(outputDirectory);
if (!file.exists()){
file.mkdirs();
}
if (entry.isDirectory()) {
String name = entry.getName();
name = name.substring(0, name.length() - 1);
file = new File(outputDirectory + File.separator + name);
if (!file.exists()){
file.mkdir();
}
} else {
file = new File(outputDirectory + File.separator + entry.getName());
if (!file.exists()){
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.close();
}
}
// 读取下一个ZipEntry
entry = in.getNextEntry();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Android 将Assets 目录中的ZIP压缩包解压至本地指定文件中
最新推荐文章于 2026-05-22 10:29:34 发布
本文介绍了一种从Android应用的Assets目录解压ZIP文件的方法。通过提供的代码示例,展示了如何使用Java实现这一功能,包括创建必要的目录结构、处理ZIP文件中的每个条目,并将内容写入指定的目标路径。


3527

被折叠的 条评论
为什么被折叠?



