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);
}
}