ZipControl对文件进行压缩与解压的方法
2016-11-14 16:01
225 查看
最近有个需求,就是要将手机sd卡中的文件压缩成zip格式上传到服务器中。经过百度后,得到了下面的ZipControl方法。如下:
注意:导包是导入 org.apache.tools.zip.ZipFile。这个包是ant.jar中的。大家可以去百度下载此jar.
调用就很简单:
这里可以对多个文件夹目录的文件进行压缩。里面传的参数都有说明。
解压的调用和上面差不多。就不演示了。
附上关于zip文件上传的博文地址:http://blog.csdn.net/qq_17387361/article/details/53160019
package com.xinsheng.yx.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.CheckedOutputStream; import java.util.zip.Deflater; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; import android.util.Log; /** * * [zip压缩解压帮助类]<BR> * @author henry */ public class ZipControl { private static boolean isCreateSrcDir = false;//是否创建源目录 在这里的话需要说明下。如果需要创建源目录的话。就在这里设为true否则为false; private static String TAG="ZipControl"; /** * * [对指定路径下文件的压缩处理]<BR> * [功能详细描述] * * @param src 径地址 * @param archive 指定到压缩文件夹的路径 * @param comment 描述 * @throws FileNotFoundException 文件没有找到异常 * @throws IOException IO输入异常 */ public static void writeByApacheZipOutputStream(String[] src, String archive, String comment) throws FileNotFoundException, IOException { Log.e(TAG, "writeByApacheZipOutputStream"); //----压缩文件: FileOutputStream f = new FileOutputStream(archive); //使用指定校验和创建输出流 CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32()); ZipOutputStream zos = new ZipOutputStream(csum); //支持中文 zos.setEncoding("GBK"); BufferedOutputStream out = new BufferedOutputStream(zos); //设置压缩包注释 zos.setComment(comment); //启用压缩 zos.setMethod(ZipOutputStream.DEFLATED); //压缩级别为最强压缩,但时间要花得多一点 zos.setLevel(Deflater.BEST_COMPRESSION); // 如果为单个文件的压缩在这里修改 for (int i = 0; i < src.length; i++) { Log.e(TAG, "src["+i+"] is "+src[i]); File srcFile = new File(src[i]); if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) { Log.e(TAG, "!srcFile.exists()"); throw new FileNotFoundException( "File must exist and ZIP file must have at least one entry."); } String strSrcString = src[i]; //获取压缩源所在父目录 strSrcString = strSrcString.replaceAll("////", "/"); String prefixDir = null; if (srcFile.isFile()) { prefixDir = strSrcString.substring(0, strSrcString .lastIndexOf("/") + 1); } else { prefixDir = (strSrcString.replaceAll("/$", "") + "/"); } //如果不是根目录 if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) { prefixDir = prefixDir.replaceAll("[^/]+/$", ""); } //开始压缩 writeRecursive(zos, out, srcFile, prefixDir); } out.close(); // 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用 Log.e(TAG, "Checksum: " + csum.getChecksum().getValue()); @SuppressWarnings("unused") BufferedInputStream bi; } /** * * [* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的 * java.util.zip.ZipFile 使用方式是一新的,只不过多了设置编码方式的 接口。 * * 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile 来读取压缩文件。]<BR> * * @param archive 压缩包路径 * @param decompressDir 解压路径 * @throws IOException * @throws FileNotFoundException * @throws ZipException */ @SuppressWarnings("unchecked") public static void readByApacheZipFile(String archive, String decompressDir) throws IOException, FileNotFoundException, ZipException { Log.e(TAG, "readByApacheZipFile"); BufferedInputStream bi; ZipFile zf = new ZipFile(archive, "GBK");//支持中文 Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); String entryName = ze2.getName(); String path = decompressDir + "/" + entryName; if (ze2.isDirectory()) { Log.e(TAG, "正在创建解压目录 - " + entryName); File decompressDirFile = new File(path); if (!decompressDirFile.exists()) { decompressDirFile.mkdirs(); } } else { Log.e(TAG, "正在创建解压文件 - " + entryName); String fileDir = path.substring(0, path.lastIndexOf("/")); File fileDirFile = new File(fileDir); if (!fileDirFile.exists()) { fileDirFile.mkdirs(); } BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(decompressDir + "/" + entryName)); bi = new BufferedInputStream(zf.getInputStream(ze2)); byte[] readContent = new byte[1024]; int readCount = bi.read(readContent); while (readCount != -1) { bos.write(readContent, 0, readCount); readCount = bi.read(readContent); } bos.close(); } } zf.close(); } /** * * [使用 java api 中的 ZipInputStream 类解压文件,但如果压缩时采用了 * org.apache.tools.zip.ZipOutputStream时,而不是 java 类库中的 * java.util.zip.ZipOutputStream时,该方法不能使用,原因就是编码方 式不一致导致,运行时会抛如下异常: * java.lang.IllegalArgumentException at * java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290) * * 当然,如果压缩包使用的是java类库的java.util.zip.ZipOutputStream 压缩而成是不会有问题的,但它不支持中文 ]<BR> * [功能详细描述] * * @param archive 压缩包路径 * @param decompressDir 解压路径 * @throws FileNotFoundException * @throws IOException */ public static void readByZipInputStream(String archive, String decompressDir) throws FileNotFoundException, IOException { BufferedInputStream bi; //----解压文件(ZIP文件的解压缩实质上就是从输入流中读取数据): Log.e(TAG, "开始读压缩文件"); FileInputStream fi = new FileInputStream(archive); CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32()); ZipInputStream in2 = new ZipInputStream(csumi); bi = new BufferedInputStream(in2); java.util.zip.ZipEntry ze;//压缩文件条目 //遍历压缩包中的文件条目 while ((ze = in2.getNextEntry()) != null) { String entryName = ze.getName(); if (ze.isDirectory()) { Log.e(TAG,"正在创建解压目录 - " + entryName); File decompressDirFile = new File(decompressDir + "/" + entryName); if (!decompressDirFile.exists()) { decompressDirFile.mkdirs(); } } else { Log.e(TAG, "正在创建解压文件 - " + entryName); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(decompressDir + "/" + entryName.substring(entryName.lastIndexOf("//"), entryName.length() - (entryName.lastIndexOf("//") - 2)))); byte[] buffer = new byte[1024]; int readCount = bi.read(buffer); while (readCount != -1) { bos.write(buffer, 0, readCount); readCount = bi.read(buffer); } bos.close(); } } bi.close(); Log.e(TAG, "Checksum: " + csumi.getChecksum().getValue()); } /** * * [递归压缩 * * 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径, 而Java类库中的 * java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。 使用 apache 中的这个类与 java * 类库中的用法是一新的,只是能设置编码方式了。]<BR> * [功能详细描述] * * @param zos * @param bo * @param srcFile * @param prefixDir * @throws IOException * @throws FileNotFoundException */ private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo, File srcFile, String prefixDir) throws IOException, FileNotFoundException { Log.e(TAG, "writeRecursive"); ZipEntry zipEntry; String filePath = srcFile.getAbsolutePath().replaceAll("////", "/") .replaceAll("//", "/"); if (srcFile.isDirectory()) { filePath = filePath.replaceAll("/$", "") + "/"; } String entryName = filePath.replace(prefixDir, "").replaceAll("/$", ""); if (srcFile.isDirectory()) { if (!"".equals(entryName)) { Log.e(TAG, "正在创建目录 - " + srcFile.getAbsolutePath() + " entryName=" + entryName); //如果是目录,则需要在写目录后面加上 / zipEntry = new ZipEntry(entryName + "/"); zos.putNextEntry(zipEntry); } File srcFiles[] = srcFile.listFiles(); for (int i = 0; i < srcFiles.length; i++) { writeRecursive(zos, bo, srcFiles[i], prefixDir); } } else { Log.e(TAG,"正在写文件 - " + srcFile.getAbsolutePath() + " entryName=" + entryName ); BufferedInputStream bi = new BufferedInputStream( new FileInputStream(srcFile)); //开始写入新的ZIP文件条目并将流定位到条目数据的开始处 zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int readCount = bi.read(buffer); while (readCount != -1) { bo.write(buffer, 0, readCount); readCount = bi.read(buffer); } //注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不 //然可能有的内容就会存入到后面条目中去了 bo.flush(); //文件读完后关闭 bi.close(); } } // 将字符串写入到文本文件中 public static void writeTxtToFile(String strcontent, String filePath, String fileName) { //生成文件夹之后,再生成文件,不然会出错 makeFilePath(filePath, fileName); String strFilePath = filePath+"/"+fileName; // 每次写入时,都换行写 String strContent = strcontent + "\r\n"; try { File file = new File(strFilePath); if (!file.exists()) { Log.d("TestFile", "Create the file:" + strFilePath); file.getParentFile().mkdirs(); file.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.seek(file.length()); raf.write(strContent.getBytes()); raf.close(); } catch (Exception e) { Log.e("TestFile", "Error on write File:" + e); } } // 生成文件 public static File makeFilePath(String filePath, String fileName) { File file = null; makeRootDirectory(filePath); try { file = new File(filePath + fileName); if (!file.exists()) { file.createNewFile(); } } catch (Exception e) { e.printStackTrace(); } return file; } // 生成文件夹 public static void makeRootDirectory(String filePath) { File file = null; try { file = new File(filePath); if (!file.exists()) { file.mkdir(); Log.e(TAG, "make zipdir success"); }else { Log.e(TAG, "exit zipdir"); } } catch (Exception e) { Log.i(TAG, e.toString()); } } }
注意:导包是导入 org.apache.tools.zip.ZipFile。这个包是ant.jar中的。大家可以去百度下载此jar.
调用就很简单:
log_fileSrcStrings= new String[]{sd.getPath()+"/henry_log"}; ZipControl.writeByApacheZipOutputStream(log_fileSrcStrings, log_archiveString+"/henry_log.zip", log_commentString);
这里可以对多个文件夹目录的文件进行压缩。里面传的参数都有说明。
解压的调用和上面差不多。就不演示了。
附上关于zip文件上传的博文地址:http://blog.csdn.net/qq_17387361/article/details/53160019
相关文章推荐
- Java方法进行解压/生成有密码保护的压缩文件(zip格式)
- linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- 利用ZipArchive库进行压缩和解压文件
- linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- 【转载】使用C#2.0进行文件压缩和解压
- linux下各种压缩文件以及相应的解压方法
- 使用C#2.0进行文件压缩和解压
- Linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- Linux/Unix下各种压缩文件的压缩/解压方法
- 使用C#进行文件压缩和解压
- 【转载】使用C#2.0进行文件压缩和解压
- java 中压缩和解压文件的方法
- 使用C#2.0进行文件压缩和解压
- linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- [转]Linux 下各种格式的压缩文件的解压方法
- 使用C#2.0进行文件压缩和解压
- tar.gz文件命名及压缩解压方法
- linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- linux下tar gz bz2 tgz z等众多压缩文件的解压方法
- 解压RAR时出现“不可预料的压缩文件末端”的解决方法