国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Apache Commons Compress

2019-11-11 07:49:29
字體:
來源:轉載
供稿:網友

       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;	}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莲花县| 巴南区| 德清县| 乐清市| 江口县| 白城市| 双峰县| 北票市| 丹寨县| 新和县| 谷城县| 郯城县| 金川县| 阿坝| 泽普县| 揭阳市| 铁岭县| 泗洪县| 志丹县| 四会市| 玛纳斯县| 西贡区| 太湖县| 克什克腾旗| 芜湖市| 余庆县| 望都县| 浠水县| 德钦县| 称多县| 武义县| 芜湖县| 巴彦县| 汾西县| 平遥县| 南溪县| 鄯善县| 桦甸市| 萝北县| 永福县| 辰溪县|