Android平台中实现Zip文件的解压缩功能
2012-05-05 13:59
495 查看
在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面Android123给大家一个解压缩zip的java代码,可以在Android上任何版本中使用,Unzip这个静态方法比较简单,参数一为源zip文件的完整路径,参数二为解压缩后存放的文件夹。
private static void Unzip(String zipFile, String targetDir) {
int BUFFER = 4096; //这里缓冲区我们使用4KB,
String strEntry; //保存每个zip的条目名称
try {
BufferedOutputStream dest = null; //缓冲输出流
FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry; //每个zip条目的实例
while ((entry = zis.getNextEntry()) != null) {
try {
Log.i("Unzip: ","="+ entry);
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();
File entryFile = new File(targetDir + strEntry);
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
} catch (Exception cwj) {
cwj.printStackTrace();
}
}
上面是zip文件解压缩代码,希望你大家有用,需要注意的是参数均填写完整的路径,比如/mnt/sdcard/xxx.zip这样的类型。
出处:http://www.android123.com.cn/androidkaifa/822.html
private static void Unzip(String zipFile, String targetDir) {
int BUFFER = 4096; //这里缓冲区我们使用4KB,
String strEntry; //保存每个zip的条目名称
try {
BufferedOutputStream dest = null; //缓冲输出流
FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry; //每个zip条目的实例
while ((entry = zis.getNextEntry()) != null) {
try {
Log.i("Unzip: ","="+ entry);
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();
File entryFile = new File(targetDir + strEntry);
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
} catch (Exception cwj) {
cwj.printStackTrace();
}
}
上面是zip文件解压缩代码,希望你大家有用,需要注意的是参数均填写完整的路径,比如/mnt/sdcard/xxx.zip这样的类型。
出处:http://www.android123.com.cn/androidkaifa/822.html
相关文章推荐
- android平台实现橡皮擦功能
- 将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能
- Android平台使用openGL ES 2.0实现预览摄像头数据功能
- 将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能
- Android平台上使用MQTT协议实现消息推送功能
- 将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能
- Android平台下实现录音及播放录音功能的简介
- 关于android微信QQ等平台分享功能实现中遇到的问题处理
- 【Android进阶】使用第三方平台 ShareSDK 实现新浪微博的一键分享功能
- Cocos2d-x 3.x 头像选择器,本地相册与拍照+头像编辑功能(Android、IOS双平台实现)
- 移植开源项目obexftp到android平台下实现蓝牙ftp的功能
- unity游戏android平台获取粘贴板的文本,实现复制粘贴功能
- Android平台生成二维码并实现扫描 & 识别功能
- 【Android进阶】使用第三方平台 ShareSDK 实现新浪微博的一键分享功能
- 使用HanLP实现Android平台上汉字转拼音功能
- 【Android进阶】使用第三方平台 ShareSDK 实现新浪微博的一键分享功能
- android4平台用FragmentActivity代替TabActivity实现分页功能
- 【Android进阶】使用第三方平台ShareSDK实现新浪微博的一键分享功能
- [置顶] React Native实现第三方分享、登录功能(Android,IOS双平台)
- iOS 混合应用的关键点分析 - 仿 Android 平台 WebView 可注入本地对象方法的功能实现要点