麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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;	}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 粉色视频污 | 久久综合婷婷香五月 | 亚洲成人入口 | 素人视频免费观看 | 最新福利在线 | 欧美亚州 | 黄 色 免费网 站 成 人 | 一区二区三区欧美精品 | 成年人免费视频播放 | 久久久成人999亚洲区美女 | 91久久极品少妇韩国 | 99在线热播精品免费 | 7777在线视频免费播放 | 日本在线观看视频网站 | 欧美日韩中文字幕在线 | 成年性羞羞视频免费观看无限 | 免费看性xxx高清视频自由 | 亚洲免费视频大全 | japanese嫩21videos | 国产一区二区在线免费播放 | 亚洲第一成av人网站懂色 | 国产精品视频导航 | 国产一区二区三区视频在线观看 | 欧美h版电影在线观看 | 黄片毛片一级 | 一级成人欧美一区在线观看 | 成人在线观看免费 | 国产精品午夜性视频 | 国产超碰人人做人人爱 | 国内久久久久 | 欧美亚州 | 日本黄色a视频 | 1级黄色毛片 | 成人激情视频网 | 免费国产不卡午夜福在线 | 一区二区久久精品66国产精品 | 少妇的肉体k8经典 | av电影在线观看网址 | 亚洲成人激情在线 | 男女牲高爱潮免费视频男女 | 久久羞羞视频 |