手机中的Zip格式文件解压和文件夹的压缩
2012-12-16 16:20
344 查看
今天主要跟大家介绍下如何通过代码把手机存储中的zip文件解压到指定的地方以及如何对很多文件或者文件夹进行压缩生成zip格式的压缩文件。
1.将某个Zip文件解压到指定目录下:
![](http://img.my.csdn.net/uploads/201212/17/1355711767_8250.png)
2.将文件夹压缩成压缩文件并放到指定位置
1.将某个Zip文件解压到指定目录下:
/** * 解压缩一个Zip格式的压缩文件 * * @param zipFile 需要解压缩文件 * @param folderPath 解压缩的目标目录 * @throws IOException 当解压缩过程出错时抛出 */ public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException { File desDir = new File(folderPath); if (!desDir.exists()) { desDir.mkdirs();//mkdirs()无论是否父目录存在,都会创建目录成功! } ZipFile zf = new ZipFile(zipFile); for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) { //压缩文件里面的每一个文件或者文件夹都对应一个ZipEntry,通过zf.entries()函数可以把所有的文件或者文件夹对应的ZipEntry都读出来 ZipEntry entry = ((ZipEntry)entries.nextElement()); String str = folderPath + File.separator + entry.getName(); Log.i("zip", "file name = " + entry.getName());//打印出压缩文件中的所有文件或者文件夹名 File desFile = new File(str); if (!desFile.exists()) { File fileParentDir = desFile.getParentFile(); if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if(entry.isDirectory()){//用来判断当前的ZipEntry对应的是文件还是文件夹 desFile.mkdir();//mkdir()就是创建一个目录,但是前提是创建的目录的父目录一定要存在 }else{ desFile.createNewFile(); InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(desFile); byte buffer[] = new byte[1024 * 1024]; int realLength; while ((realLength = in.read(buffer)) > 0) { out.write(buffer, 0, realLength); } in.close(); out.close(); } } } }现假如手机存储根目录下有个sample.zip,通过调用upZipFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample.zip"), Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample");就会将文件解压到根目录的sample文件夹中,打印的log如下:
![](http://img.my.csdn.net/uploads/201212/17/1355711767_8250.png)
2.将文件夹压缩成压缩文件并放到指定位置
/** * 批量压缩文件(夹) * * @param resFileList 要压缩的文件(夹)列表 * @param zipFile 生成的压缩文件 * @throws IOException 当压缩过程出错时抛出 */ public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException { ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream( zipFile), 1024 * 1024)); for (File resFile : resFileList) { zipFile(resFile, zipout, ""); } zipout.close(); } /** * 压缩文件 * * @param resFile 需要压缩的文件(夹) * @param zipout 压缩的目的文件 * @param rootpath 压缩的文件路径 * @throws FileNotFoundException 找不到文件时抛出 * @throws IOException 当压缩过程出错时抛出 */ private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException { rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName(); if(!resFile.exists()){ return; } if (resFile.isDirectory()) { File[] fileList = resFile.listFiles(); for (File file : fileList) { zipFile(file, zipout, rootpath); } rootpath = rootpath + File.separatorChar;//添加File.separatorChar表示该ZipEntry对应的是文件夹,也就是此时调用ZipEntry.isDirectory()会返回true zipout.putNextEntry(new ZipEntry(rootpath)); } else { byte buffer[] = new byte[1024 * 1024]; BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE); zipout.putNextEntry(new ZipEntry(rootpath)); int realLength; while ((realLength = in.read(buffer)) != -1) { zipout.write(buffer, 0, realLength); } in.close(); zipout.flush(); zipout.closeEntry(); } }如果有什么问题或者不对的地方随时欢迎留言,谢谢。。。
相关文章推荐
- Linux下如何解压rar、zip格式文件?Linux VPS安装7zip/使用方法/压缩解压中文文件名乱码解决方案
- Java实现文件压缩与解压[zip格式,gzip格式]
- 使用C#压缩解压rar和zip格式文件
- java压缩及解压(.zip)文件文件夹
- 使用Java对文件或文件夹的压缩, 解压, 加密和解密
- java 压缩/解压 zip 多个文件和文件夹
- 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货
- zend framework将zip格式的压缩文件导入并解压到指定文件
- Windows系统下Python压缩和解压文件、文件夹操作
- Unity LZMA GZIP 压缩、解压文件 和 打包文件夹 (支持进度回调)
- java 压缩、解压文件、文件夹。
- 跨平台的zip文件压缩处理,支持压缩解压文件夹
- Linux压缩解压文件夹、文件——tar
- zend framework将zip格式的压缩文件导入并解压到指定文件
- c#解压压缩文件和文件夹
- C# 解压压缩文件类(ZIP格式)
- linux windows 下的.rar .zip格式文件的压缩与解压
- 【091】Linux打包压缩文件夹和解压文件
- Java实现文件压缩与解压[zip格式,gzip格式]
- 用ZipInputStream和ZipOutputStream实现文件及文件夹的压缩解压