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 { } }
上一篇:springmvc上传zip文件并解压缩代码示例


下一篇:2013.4.A