android压缩解压zip文件
网上各种方法的收集:
1、上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩功能。对上次解压缩zip功能进行了修改。
现在也可以解压 那些包含子目录的zip文件。
/**
* 解压缩功能.
* 将zipFile文件解压到folderPath目录下.
* @throws Exception
*/
public int upZipFile(File zipFile, String folderPath)throws ZipException,IOException {
//public static void upZipFile() throws Exception{
ZipFile zfile=new ZipFile(zipFile);
Enumeration zList=zfile.entries();
ZipEntry ze=null;
byte[] buf=new byte[1024];
while(zList.hasMoreElements()){
ze=(ZipEntry)zList.nextElement();
if(ze.isDirectory()){
Log.d("upZipFile", "ze.getName() = "+ze.getName());
String dirstr = folderPath + ze.getName();
//dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "str = "+dirstr);
File f=new File(dirstr);
f.mkdir();
continue;
}
Log.d("upZipFile", "ze.getName() = "+ze.getName());
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
int readLen=0;
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
Log.d("upZipFile", "finishssssssssssssssssssss");
return 0;
} /**
* 给定根目录,返回一个相对路径所对应的实际文件名.
* @param baseDir 指定根目录
* @param absFileName 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
public static File getRealFileName(String baseDir, String absFileName){
String[] dirs=absFileName.split("/");
File ret=new File(baseDir);
String substr = null;
if(dirs.length>1){
for (int i = 0; i < dirs.length-1;i++) {
substr = dirs[i];
try {
//substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312"); } catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret=new File(ret, substr); }
Log.d("upZipFile", "1ret = "+ret);
if(!ret.exists())
ret.mkdirs();
substr = dirs[dirs.length-1];
try {
//substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "substr = "+substr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ret=new File(ret, substr);
Log.d("upZipFile", "2ret = "+ret);
return ret;
}
return ret;
}
记得要在AndroidManifest.xml里添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
2、我 们在日常生活中会用到解压缩,这个是非常重要的,那么我们在android系统中有没有解压缩那。如果有的话,那我们如何实现Zip文件的解压缩功能呢? 那么我们就看看下面的解析吧,因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面给大家一个解压缩 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();
}
}
上面是Android开发网总结的zip文件解压缩代码,希望你大家有用,需要注意的是参数均填写完整的路径,比如/mnt/sdcard/xxx.zip这样的类型。
下面的方法是,解压只有一个文件组成的zip到当前目录,并且给解压出的文件重命名:
public static void unzipSingleFileHereWithFileName(String zipPath, String name) throws IOException{
File zipFile = new File(zipPath);
File unzipFile = new File(zipFile.getParent() + "/" + name);
ZipInputStream zipInStream = null;
FileOutputStream unzipOutStream = null;
try {
zipInStream = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry = zipInStream.getNextEntry();
if (!zipEntry.isDirectory()) {
unzipOutStream = new FileOutputStream(unzipFile);
byte[] buf = new byte[4096];
int len = -1;
while((len = zipInStream.read(buf)) != -1){
unzipOutStream.write(buf, 0, len);
}
}
} finally {
if(unzipOutStream != null){
unzipOutStream.close();
}
if (zipInStream != null) {
zipInStream.close();
}
}
}
3、研究了一下Android上Zip的用法,写了个类把常用的几种方法写了出来
/**
* Android Zip压缩解压缩
* @author Ren.xia
* @version 1.0
* @updated 26-七月-2010 13:04:27
*/
public class XZip { public XZip(){ } /**
* 取得压缩包中的 文件列表(文件夹,文件自选)
* @param zipFileString 压缩包名字
* @param bContainFolder 是否包括 文件夹
* @param bContainFile 是否包括 文件
* @return
* @throws Exception
*/
public static java.util.List<java.io.File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception { android.util.Log.v("XZip", "GetFileList(String)"); java.util.List<java.io.File> fileList = new java.util.ArrayList<java.io.File>();
java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
java.util.zip.ZipEntry zipEntry;
String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName(); if (zipEntry.isDirectory()) { // get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
java.io.File folder = new java.io.File(szName);
if (bContainFolder) {
fileList.add(folder);
} } else {
java.io.File file = new java.io.File(szName);
if (bContainFile) {
fileList.add(file);
}
}
}//end of while inZip.close(); return fileList;
} /**
* 返回压缩包中的文件InputStream
* @param zipFileString 压缩文件的名字
* @param fileString 解压文件的名字
* @return InputStream
* @throws Exception
*/
public static java.io.InputStream UpZip(String zipFileString, String fileString)throws Exception {
android.util.Log.v("XZip", "UpZip(String, String)");
java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(zipFileString);
java.util.zip.ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry); } /**
* 解压一个压缩文档 到指定位置
* @param zipFileString 压缩包的名字
* @param outPathString 指定的路径
* @throws Exception
*/
public static void UnZipFolder(String zipFileString, String outPathString)throws Exception {
android.util.Log.v("XZip", "UnZipFolder(String, String)");
java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
java.util.zip.ZipEntry zipEntry;
String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName(); if (zipEntry.isDirectory()) { // get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
folder.mkdirs(); } else { java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
file.createNewFile();
// get the output stream of the file
java.io.FileOutputStream out = new java.io.FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}//end of while inZip.close(); }//end of func /**
* 压缩文件,文件夹
* @param srcFileString 要压缩的文件/文件夹名字
* @param zipFileString 指定压缩的目的和名字
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {
android.util.Log.v("XZip", "ZipFolder(String, String)"); //创建Zip包
java.util.zip.ZipOutputStream outZip = new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(zipFileString)); //打开要输出的文件
java.io.File file = new java.io.File(srcFileString); //压缩
ZipFiles(file.getParent()+java.io.File.separator, file.getName(), outZip); //完成,关闭
outZip.finish();
outZip.close(); }//end of func /**
* 压缩文件
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private static void ZipFiles(String folderString, String fileString, java.util.zip.ZipOutputStream zipOutputSteam)throws Exception{
android.util.Log.v("XZip", "ZipFiles(String, String, ZipOutputStream)"); if(zipOutputSteam == null)
return; java.io.File file = new java.io.File(folderString+fileString); //判断是不是文件
if (file.isFile()) { java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(fileString);
java.io.FileInputStream inputStream = new java.io.FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry); int len;
byte[] buffer = new byte[4096]; while((len=inputStream.read(buffer)) != -1)
{
zipOutputSteam.write(buffer, 0, len);
} zipOutputSteam.closeEntry();
}
else { //文件夹的方式,获取文件夹下的子文件
String fileList[] = file.list(); //如果没有子文件, 则添加进去即可
if (fileList.length <= 0) {
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(fileString+java.io.File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
} //如果有子文件, 遍历子文件
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);
}//end of for }//end of if }//end of func public void finalize() throws Throwable { } }
android压缩解压zip文件的更多相关文章
- (转载)C#压缩解压zip 文件
转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...
- C#压缩解压zip 文件
/// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- java中自己常用到的工具类-压缩解压zip文件
package com.ricoh.rapp.ezcx.admintoolweb.util; import java.io.File; import java.io.FileInputStream; ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...
- Android 解压zip文件(支持中文)
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- Android 解压zip文件
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
随机推荐
- 转:JS线程和JS阻塞页面加载的问题
前几日写了一篇文章,介绍了js阻塞页面加载的问题.当时是通过例子来验证的.今天,我介绍一下浏览器内核,从原理上介绍一下js阻塞页面加载的原因. 浏览器的内核是多线程的,它们在内核制控下相互配合以保持同 ...
- 深入探究VC —— 编译器cl.exe(1)
cl.exe的功能是将源代码文件编译为可提供链接器使用的obj对象文件.cl.exe命令行参数形式如下: CL (option...) file... [option | file]... [lib. ...
- DIOR HOMME_百度百科
DIOR HOMME_百度百科 DIOR HOMME 编辑 Dior Homme 男装品牌,中文名迪奥·桀傲,由迪奥 (Dior) 在2001年更名更来,品牌来源地法国.迪奥·桀傲 ...
- AFNetworking 进行网络监测
AFNetworking 进行网络监测 引入头文件,创建检测判断BOOL值 // 网络请求的头文件 #import <AFNetworking.h> @interface ViewCont ...
- ZigBee研究之旅(二)
在学习ZigBee设备CC2530模块时,编程后程序无法运行,但又十分确定程序的真确性的情况下,看看是不是project栏下的option选项配置的有问题,我是经常在这里出问题,一开始找不到原因,特此 ...
- Axure滚动效果实现
下面的这个透明区域用于显示滚动效果,它本身是一个处于隐藏状态的动态面板,它里面也放了一个动态面板用于产生移动的效果 里面的动态面板起名“实际内容”,注意它的默认状态是“状态2”,状态2和状态一的内容一 ...
- android Graphics(二):路径及文字
前言:今天项目进入攻关期,他们改Bug要改疯掉了,主管为了激励大家,给大家发了一封邮件,讲到他对项目和学习的理解,一个很好的图形模型,分享给大家,如图在下面给出:(不便给出原文,我仅做转述)无论是学习 ...
- ExtJS学习第一天 MessageBox
此文用来记录学习笔记: •学习任何技术,首先都要从Helloworld开始,那么我们首要任务就是写一个简单的HelloWorld程序,带领同学们走进ExtJS的世界. •Ext.onReady:这个方 ...
- if语句之求一元二次方程
思路:1.首先明白什么叫做一元二次方程,当a不等于0的时候,此方程是一元二次方程 2.根据公式derta=b*b-4*a*c来判断根的情况 ①derta>0时,方程有两个不相等的实根 ②dert ...
- 动态规划---最长上升子序列问题(O(nlogn),O(n^2))
LIS(Longest Increasing Subsequence)最长上升子序列 或者 最长不下降子序列.很基础的题目,有两种算法,复杂度分别为O(n*logn)和O(n^2) . ******* ...