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) { //把緩沖區的字節寫入到ZipArchiveEntry zipArchiveOutputStream.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 required zipArchiveOutputStream.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) { //把緩沖區的字節寫入到ZipArchiveEntry zipArchiveOutputStream.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; }}
新聞熱點
疑難解答