assets里的zip压缩文件并解压到指定路径下保存
2015-12-25 15:39
489 查看
有时候发布app,在assets中会自带一些压缩包,打开应用解压放在用户手机sd卡中,
解压这一块就是下面的代码,要判断解压出来的是直接的文件还是一个文件夹,并存储到指定路径下面就好
public
class
UnzipAssets {
/** |
*解压Assets中的文件 |
*@param context上下文对象 |
*@param assetName压缩包文件名 |
*@param outputDirectory输出目录 |
*@throws IOException |
*/ |
public static void unZip(Context context, String assetName,String outputDirectory) throws IOException { |
//创建解压目标目录 |
File file = new File(outputDirectory); |
//如果目标目录不存在,则创建 |
if (!file.exists()) { |
file.mkdirs(); |
} |
InputStream inputStream = null ; |
//打开压缩文件 |
inputStream = context.getAssets().open(assetName); |
ZipInputStream zipInputStream = new ZipInputStream(inputStream); |
//读取一个进入点 |
ZipEntry zipEntry = zipInputStream.getNextEntry(); |
//使用1Mbuffer |
byte [] buffer = new byte [ 1024 * 1024 ]; |
//解压时字节计数 |
int count = 0 ; |
//如果进入点为空说明已经遍历完所有压缩包中文件和目录 |
while (zipEntry != null ) { |
//如果是一个目录 |
if (zipEntry.isDirectory()) { |
//String name = zipEntry.getName(); |
//name = name.substring(0, name.length() - 1); |
file = new File(outputDirectory + File.separator + zipEntry.getName()); |
file.mkdir(); |
} else { |
//如果是文件 |
file = new File(outputDirectory + File.separator |
+ zipEntry.getName()); |
//创建该文件 |
file.createNewFile(); |
FileOutputStream fileOutputStream = new FileOutputStream(file); |
while ((count =zipInputStream.read(buffer)) > 0 ) { |
fileOutputStream.write(buffer, 0 , count); |
} |
fileOutputStream.close(); |
} |
//定位到下一个文件入口 |
zipEntry = zipInputStream.getNextEntry(); |
} |
zipInputStream.close(); |
} |
}
相关文章推荐
- JDK5个小工具
- 常用utils android
- 详解Mysql case then使用
- 【Java】HTTP Web调试工具Fiddler介绍和使用教程
- lua遍历调用C#泛型List、IEnumberable、Dictionary
- 创建虚拟机中的nova-scheduler 调度配置
- 数据结构课程设计——图书信息管理系统设计
- linux从命令行发送邮件
- action使用addFieldError显示提示信息
- OC内存管理
- 蓝牙后台接收数据(BLE4.0)
- angular2快速开始
- javascript核心概念
- 数据结构之排序算法(四)-归并排序
- java读取.properties文件
- js中的3种弹出式消息提醒(警告窗口,确认窗口,信息输入窗口)的命令式
- sqlserver 复制表结构(可以含有数据 或 只要表结构)
- GET POST方法长度限制
- 基础架构
- Ruby on Rails 项目里面字符串过滤html标签