Apache Commons Compress

来源:互联网 发布:淘宝客转链工具app 编辑:程序博客网 时间:2024/06/01 10:53

       Apache Commons是Apache软件基金会的项目,曾隶属于Jakarta项目。Commons的目的是提供可重用的、开源的Java代码。Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。

Compress

       本例主要介绍组件Compress,Compress是ApacheCommons提供压缩、解压缩文件的类库,可以操作ar, cpio, Unix dump, tar, zip,gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大。本例主要演示Compress操作对zip文件压缩与解压缩的操作。commons-compress-1.13-bin.zip 下载!

1、文件压缩成zip格式压缩包

         /** * 将文件打包成zip压缩包文件 */private static void compressFiles2Zip(){long startInt = System.currentTimeMillis();File file1 = new File("D:\\logs\\log4j.log");File file2 = new File("D:\\logs\\log4j.log.2016-07-31-19");List<File> files = new ArrayList<File>();files.add(file1);files.add(file2);File zipFile = new File("D:\\logs\\log4j.log.zip");InputStream inputStream = null;ZipArchiveOutputStream zipArchiveOutputStream = null;try {zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);for(File file : files){//将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, "test"+File.separator+file.getName());zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);inputStream = new FileInputStream(file);byte[] buffer = new byte[1024 * 5]; int len = -1;while((len = inputStream.read(buffer)) != -1) {//把缓冲区的字节写入到ZipArchiveEntryzipArchiveOutputStream.write(buffer, 0, len);}}zipArchiveOutputStream.closeArchiveEntry();zipArchiveOutputStream.finish();} catch (IOException e) {//e.printStackTrace();}finally{//关闭输入流if(null!=inputStream){try {inputStream.close();} catch (IOException e) {//e.printStackTrace();}}//关闭输出流if(null!=zipArchiveOutputStream){try {zipArchiveOutputStream.close();} catch (IOException e) {//e.printStackTrace();}}}System.out.println("耗时:"+(System.currentTimeMillis()-startInt)+"毫秒");}

2、zip压缩包解压成文件到指定文件夹

        /** * 将zip压缩包解压成文件到指定文件夹 */private static void decompressZip2Files(){long startInt = System.currentTimeMillis();String zipFilePath = "D:\\logs\\log4j.log.zip";File zipFile = new File(zipFilePath);InputStream inputStream = null;OutputStream outputStream = null;//zip文件输入流ZipArchiveInputStream zipArchiveInputStream = null;ArchiveEntry archiveEntry = null;try {inputStream = new FileInputStream(new File(zipFilePath));zipArchiveInputStream = new ZipArchiveInputStream(inputStream,"UTF-8");while(null!=(archiveEntry = zipArchiveInputStream.getNextEntry())){//获取文件名String archiveEntryFileName = archiveEntry.getName();//构造解压后文件的存放路径String archiveEntryPath = "D:\\logs\\test\\" + archiveEntryFileName;byte[] content = new byte[(int) archiveEntry.getSize()];zipArchiveInputStream.read(content);//把解压出来的文件写到指定路径File entryFile  = new File(archiveEntryPath);if(!entryFile.exists()){entryFile.getParentFile().mkdirs();}outputStream = new FileOutputStream(entryFile);outputStream.write(content);outputStream.flush();}} catch (FileNotFoundException e) {//e.printStackTrace();} catch (IOException e) {//e.printStackTrace();}finally{if(null!=outputStream){try {outputStream.close();} catch (IOException e) {//e.printStackTrace();}}if(null!=zipArchiveInputStream){try {zipArchiveInputStream.close();} catch (IOException e) {//e.printStackTrace();}}if(null!=inputStream){try {inputStream.close();} catch (IOException e) {//e.printStackTrace();}}}System.out.println("耗时:"+(System.currentTimeMillis()-startInt)+"毫秒");}

       以上分别演示zip文件压缩和解压缩的过程,以下将zip文件压缩和解压缩抽取成公共方法,但是其中未添加文件格式校验等操作,需要读者自行补充。

package com.mahaochen.apache.commons.Compress;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.commons.compress.archivers.ArchiveEntry;import org.apache.commons.compress.archivers.zip.Zip64Mode;import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;public class CommCompressZipFileUtil {/** * 将文件打包成zip压缩包文件 * @param files * @param targetFilePath * @return */public boolean compressFiles2Zip(File[] files,String targetFilePath){InputStream inputStream = null;ZipArchiveOutputStream zipArchiveOutputStream = null;try {File zipFile = new File(targetFilePath);zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);//Use Zip64 extensions for all entries where they are requiredzipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);for(File file : files){//将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);inputStream = new FileInputStream(file);byte[] buffer = new byte[1024 * 5]; int len = -1;while((len = inputStream.read(buffer)) != -1) {//把缓冲区的字节写入到ZipArchiveEntryzipArchiveOutputStream.write(buffer, 0, len);}}zipArchiveOutputStream.closeArchiveEntry();zipArchiveOutputStream.finish();} catch (IOException e) {//e.printStackTrace();return false;}finally{//关闭输入流if(null!=inputStream){try {inputStream.close();} catch (IOException e) {//e.printStackTrace();}}//关闭输出流if(null!=zipArchiveOutputStream){try {zipArchiveOutputStream.close();} catch (IOException e) {//e.printStackTrace();}}}return true;}/** * 将zip压缩包解压成文件到指定文件夹 * @param zipFilePath * @param targetDirPath * @return */public boolean decompressZip2Files(String zipFilePath,String targetDirPath){InputStream inputStream = null;OutputStream outputStream = null;//zip文件输入流ZipArchiveInputStream zipArchiveInputStream = null;ArchiveEntry archiveEntry = null;try {File zipFile = new File(zipFilePath);inputStream = new FileInputStream(zipFile);zipArchiveInputStream = new ZipArchiveInputStream(inputStream,"UTF-8");while(null!=(archiveEntry = zipArchiveInputStream.getNextEntry())){//获取文件名String archiveEntryFileName = archiveEntry.getName();//构造解压后文件的存放路径String archiveEntryPath = targetDirPath + archiveEntryFileName;//把解压出来的文件写到指定路径File entryFile  = new File(archiveEntryPath);if(!entryFile.exists()){entryFile.getParentFile().mkdirs();}byte[] buffer = new byte[1024 * 5];outputStream = new FileOutputStream(entryFile);int len = -1;while((len = zipArchiveInputStream.read(buffer)) != -1){outputStream.write(buffer,0,len);}outputStream.flush();}} catch (FileNotFoundException e) {//e.printStackTrace();return false;} catch (IOException e) {//e.printStackTrace();return false;}finally{if(null!=outputStream){try {outputStream.close();} catch (IOException e) {//e.printStackTrace();}}if(null!=zipArchiveInputStream){try {zipArchiveInputStream.close();} catch (IOException e) {//e.printStackTrace();}}if(null!=inputStream){try {inputStream.close();} catch (IOException e) {//e.printStackTrace();}}}return true;}}



0 0