Android Zip压缩解压缩
来源:互联网 发布:卖脐橙淘宝店名字 编辑:程序博客网 时间:2024/05/16 00:27
转载:http://crackren.iteye.com/blog/752732
参考:
http://www.eoeandroid.com/thread-167951-1-1.html
http://www.cnblogs.com/tnxk/archive/2011/09/22/2185248.html
在原作者的基础上进行了一下调整,暂时不支持中文目录
直接上修改后的代码
package com.yao.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;/** * Android Zip压缩解压缩 * */public class Zip {public Zip(){}/** * 取得压缩包中的 文件列表(文件夹,文件自选) * @param zipFileString压缩包名字(路径 + 文件名 : path/xxx.zip) * @param bContainFolder是否包括 文件夹 * @param bContainFile是否包括 文件 * @return * @throws Exception */public static List<File> getFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {List<File> fileList = new ArrayList<File>();ZipInputStream inZip = new ZipInputStream(new FileInputStream(new File(zipFileString)));ZipEntry zipEntry;String szName = "";while ((zipEntry = inZip.getNextEntry()) != null) {szName = zipEntry.getName();if (zipEntry.isDirectory()) {// get the folder name of the widgetszName = szName.substring(0, szName.length() - 1);File folder = new File(szName);if (bContainFolder) {fileList.add(folder);}} else {File file = new File(szName);if (bContainFile) {fileList.add(file);}}}inZip.close();return fileList;}/** * 返回压缩包中的文件InputStream * @param zipFileString压缩文件的名字(路径 + 文件名) * @param fileString 解压文件的名字 * @return InputStream * @throws Exception */public static InputStream UpZip(String zipFileString, String fileString)throws Exception {ZipFile zipFile = new ZipFile(zipFileString);ZipEntry zipEntry = zipFile.getEntry(fileString);return zipFile.getInputStream(zipEntry);}/** * 解压一个压缩文档 到指定位置 * @param zipFileString压缩包的名字(路径 + 文件名 : path/xxx.zip) * @param outPathString指定的路径(../path/xxx/xxx...) * @throws Exception */public static void UnZipFolder(String zipFileString, String outPathString)throws Exception {ZipInputStream inZip = new ZipInputStream(new FileInputStream(new File(zipFileString)));ZipEntry zipEntry;String szName = "";while ((zipEntry = inZip.getNextEntry()) != null) {szName = zipEntry.getName();if (zipEntry.isDirectory()) {// get the folder name of the widgetszName = szName.substring(0, szName.length() - 1);File folder = new File(outPathString + File.separator + szName);folder.mkdirs();} else {File file = new File(outPathString + File.separator + szName);file.createNewFile();// get the output stream of the fileFileOutputStream out = new FileOutputStream(file);int len;byte[] buffer = new byte[1024];// read (len) bytes into bufferwhile ((len = inZip.read(buffer)) != -1) {// write (len) byte from buffer at the position 0out.write(buffer, 0, len);out.flush();}out.close();}}inZip.close();}/** * 压缩文件,文件夹 * @param srcFileString要压缩的文件/文件夹名字(路径 + 文件名 : path/filename) * @param zipFileString指定压缩的目的和名字(路径 + 文件名: path/filename.zip) * @throws Exception */public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {//创建Zip包ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(new File(zipFileString)));//打开要输出的文件File file = new File(srcFileString);//压缩ZipFiles(file.getParent()+File.separator, file.getName(), outZip);//完成,关闭outZip.finish();outZip.close();}/** * 压缩文件 * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{if(zipOutputSteam == null)return;File file = new File(folderString+fileString);//判断是不是文件if (file.isFile()) {ZipEntry zipEntry = new ZipEntry(fileString);FileInputStream inputStream = new 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) {ZipEntry zipEntry = new ZipEntry(fileString+File.separator);zipOutputSteam.putNextEntry(zipEntry);zipOutputSteam.closeEntry();}//如果有子文件, 遍历子文件for (int i = 0; i < fileList.length; i++) {ZipFiles(folderString, fileString+File.separator+fileList[i], zipOutputSteam);}}}public void finalize() throws Throwable {}}
- Android Zip压缩解压缩
- Android Zip压缩解压缩
- Android Zip压缩解压缩
- Android Zip压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩!!!!!!!!!!!
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- Android zip文件压缩解压缩
- android压缩解压缩zip文件格式
- Android zip文件压缩解压缩
- Visual Studio 2008 安装失败(“Web 创作组件”无法安装)解决方法
- Building an OpenGL ES Environment
- 直击成都传智播客8月20日就业班开班典礼
- r22 tools cause java.lang.VerifyError when building from ant script
- 如何看懂源代码--(分析源代码方法)
- Android Zip压缩解压缩
- C++ 适配器模式
- linux数据链路访问之ETH_P_ALL等等
- MYSQL管理之主从同步管理
- 演示馆-走向世界之《洋话连篇 自助风暴系列之高级版》英语口语视频教程
- 基本类型与引用类型
- Lua源码剖析(lstrlib.c)
- c++ string操作
- android 微博客客户端