使用commons-compress操作zip文件(压缩和解压缩)
来源:互联网 发布:抗氧化面膜 知乎 编辑:程序博客网 时间:2024/05/21 06:36
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html
Apache Commons Compress是一个压缩、解压缩文件的类库。
可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大。
在这里写两个用Commons Compress把文件压缩成zip和从zip解压缩的方法。
直接贴上工具类代码:
/** * Zip文件工具类 * @author Luxh */public class ZipFileUtil { /** * 把文件压缩成zip格式 * @param files 需要压缩的文件 * @param zipFilePath 压缩后的zip文件路径 ,如"D:/test/aa.zip"; */ public static void compressFiles2Zip(File[] files,String zipFilePath) { if(files != null && files.length >0) { if(isEndsWithZip(zipFilePath)) { ZipArchiveOutputStream zaos = null; try { File zipFile = new File(zipFilePath); zaos = new ZipArchiveOutputStream(zipFile); //Use Zip64 extensions for all entries where they are required zaos.setUseZip64(Zip64Mode.AsNeeded); //将每个文件用ZipArchiveEntry封装 //再用ZipArchiveOutputStream写到压缩文件中 for(File file : files) { if(file != null) { ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file,file.getName()); zaos.putArchiveEntry(zipArchiveEntry); InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024 * 5]; int len = -1; while((len = is.read(buffer)) != -1) { //把缓冲区的字节写入到ZipArchiveEntry zaos.write(buffer, 0, len); } //Writes all necessary data for this entry. zaos.closeArchiveEntry(); }catch(Exception e) { throw new RuntimeException(e); }finally { if(is != null) is.close(); } } } zaos.finish(); }catch(Exception e){ throw new RuntimeException(e); }finally { try { if(zaos != null) { zaos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } } /** * 把zip文件解压到指定的文件夹 * @param zipFilePath zip文件路径, 如 "D:/test/aa.zip" * @param saveFileDir 解压后的文件存放路径, 如"D:/test/" */ public static void decompressZip(String zipFilePath,String saveFileDir) { if(isEndsWithZip(zipFilePath)) { File file = new File(zipFilePath); if(file.exists()) { InputStream is = null; //can read Zip archives ZipArchiveInputStream zais = null; try { is = new FileInputStream(file); zais = new ZipArchiveInputStream(is); ArchiveEntry archiveEntry = null; //把zip包中的每个文件读取出来 //然后把文件写到指定的文件夹 while((archiveEntry = zais.getNextEntry()) != null) { //获取文件名 String entryFileName = archiveEntry.getName(); //构造解压出来的文件存放路径 String entryFilePath = saveFileDir + entryFileName; byte[] content = new byte[(int) archiveEntry.getSize()]; zais.read(content); OutputStream os = null; try { //把解压出来的文件写到指定路径 File entryFile = new File(entryFilePath); os = new BufferedOutputStream(new FileOutputStream(entryFile)); os.write(content); }catch(IOException e) { throw new IOException(e); }finally { if(os != null) { os.flush(); os.close(); } } } }catch(Exception e) { throw new RuntimeException(e); }finally { try { if(zais != null) { zais.close(); } if(is != null) { is.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } } /** * 判断文件名是否以.zip为后缀 * @param fileName 需要判断的文件名 * @return 是zip文件返回true,否则返回false */ public static boolean isEndsWithZip(String fileName) { boolean flag = false; if(fileName != null && !"".equals(fileName.trim())) { if(fileName.endsWith(".ZIP")||fileName.endsWith(".zip")){ flag = true; } } return flag; } }
测试代码:
package cn.luxh.test; import java.io.File; import org.junit.Test; import cn.luxh.utils.ZipFileUtil; /** * 测试ZipFileUtil的压缩和解压缩方法 * @author Luxh */public class ZipFileUtilTest { @Test public void testCompressFiles2Zip() { //存放待压缩文件的目录 File srcFile = new File("D:/test"); //压缩后的zip文件路径 String zipFilePath = "d:/test2/test.zip"; if(srcFile.exists()) { File[] files = srcFile.listFiles(); ZipFileUtil.compressFiles2Zip(files, zipFilePath); } } @Test public void testDecompressZip() { //压缩包所在路径 String zipFilePath = "d:/test2/test.zip"; //解压后的文件存放目录 String saveFileDir = "d:/test2/"; //调用解压方法 ZipFileUtil.decompressZip(zipFilePath, saveFileDir); }}
- 使用commons-compress操作zip文件(压缩和解压缩)
- 使用commons-compress.jar压缩ZIP文件
- Apache Commons Compress 压缩zip
- 压缩和解压缩ZIP文件
- 使用ant实现Zip文件压缩和解压缩Zip文件
- Java使用7Zip完成文件的压缩和解压缩
- PHP实现zip压缩和解压缩操作
- apach ant 压缩和解压缩zip文件
- python怎样压缩和解压缩ZIP文件
- python怎样压缩和解压缩ZIP文件
- Java zip/gzip文件压缩和解压缩
- Java zip/gzip文件压缩和解压缩
- 【Java】IO操作之使用zip包压缩和解压缩文件
- IO操作之使用zip包压缩和解压缩文件
- 【文件压缩】 Android Jar、Zip文件压缩和解压缩处理
- apache.commons.compress zip压缩解压工具类完整版(使用递归支持无限文件夹的解压缩)
- java中生成和解压缩zip文件
- vc zip压缩和解压缩
- poj 3264 Balanced Lineup[线段树,,水]
- svn使用注意的小细节
- SSH 无密码验证登陆
- Oracle之SQLPlus简单应用
- VC遍历注册表
- 使用commons-compress操作zip文件(压缩和解压缩)
- Ubuntu12.04如何禁用触摸板
- mpeg与h26x的比较
- xcode an unknown error occurred.see the install log for more details
- SecureCRT上传下载文件教程(Zmodem使用教程)
- FusionCharts报表
- 如何解决工作中的技术问题?
- 数据库 数据仓库
- 直接从dmp文件中抽取sql 的方法