网上各种方法的收集:

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文件的更多相关文章

  1. (转载)C#压缩解压zip 文件

    转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...

  2. C#压缩解压zip 文件

    /// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...

  3. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  4. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  5. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  6. java中自己常用到的工具类-压缩解压zip文件

    package com.ricoh.rapp.ezcx.admintoolweb.util; import java.io.File; import java.io.FileInputStream; ...

  7. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

    文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...

  8. Android 解压zip文件(支持中文)

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  9. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

随机推荐

  1. apache加载php模块失败

    LoadModule php5_module "G:/php54/php5apache2_2.dll" apache2 conf加入这个之后无法加载 解决办法 在这句之前加入PHP ...

  2. SQL分页存储过程(不支持多表联合查询,不支持多字段排序)

    CREATE PROCEDURE [dbo].[Pro_GetPageOfRecords] @PageSize INT=20, --分页大小 @CurrentPage INT, --第几页 @Clum ...

  3. C语言宏的特殊用法和几个坑(转)

    总结一下C语言中宏的一些特殊用法和几个容易踩的坑.由于本文主要参考GCC文档,某些细节(如宏参数中的空格是否处理之类)在别的编译器可能有细微差别,请参考相应文档. 宏基础 宏仅仅是在C预处理阶段的一种 ...

  4. 用JS判断用户使用的是手机端还是pc端访问

    最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,b ...

  5. (step5.1.3)hdu 1213( How Many Tables——1213)

    题目大意:输入两个整数n,m.分别表示点数.对点的操作的次数.在接下来的m行中,每行有两个整数a,b.表示a和b是好朋友.(不是好朋友的不能坐在同一个桌子上) .求需要多少张桌子 解题思路:并查集 1 ...

  6. vijos 1115 火星人

    说了那么多.事实上就是遍历全排列 #include<iostream> #include<cstdio> #include<algorithm> #include& ...

  7. veridata实验例(5)在更改主键列值,update操作将被分成两个语句

    veridata实验例(5)更改主键列值,update操作将被分成两个语句 续接"veridata实验举例(4)验证veridata查找出updata.delete操作导致的不同步现象&qu ...

  8. Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性

    CSDN 对格式支持比較弱.能够到http://user.qzone.qq.com/88285879/blog/1399382878 看一致的内容. Performance Tuning Guide  ...

  9. C++中,引用作为函数参数

    引用作为函数参数 C++之所以增加引用类型, 主要是把它作为函数参数,以扩充函数传递数据的功能. ———————————————————— c++,函数传参:(1)将变量名作为实参和形参.这时传给形参 ...

  10. 利用 操作符特性 代替if判断语句

    参考:http://blog.csdn.net/speedme/article/details/22916181 1.&&的判断特性 #include <stdio.h> ...