zip扮演着歸檔和壓縮兩個角色;gzip並不將文件歸檔,僅只是對單個文件進行壓縮,所以,在UNIX平臺上,命令tar通常用來創建一個檔案文件,然後命令gzip來將檔案文件壓縮。
Java I/O類庫還收錄了一些能讀寫壓縮格式流的類。要想提供壓縮功能,只要把它們包在已有的I/O類的外面就行了。這些類不是Reader和Writer,而是InputStream和OutStreamput的子類。這是因爲壓縮算法是針對byte而不是字符的。
相關類與接口:
Checksum接口:被類Adler32和CRC32實現的接口
Adler32:使用Alder32算法來計算Checksum數目
CRC32:使用CRC32算法來計算Checksum數目
CheckedInputStream:InputStream派生類,可得到輸入流的校驗和Checksum,用於校驗數據的完整性
CheckedOutputStream:OutputStream派生類,可得到輸出流的校驗和Checksum,用於校驗數據的完整性
DeflaterOutputStream:壓縮類的基類。
ZipOutputStream:DeflaterOutputStream的一個子類,把數據壓縮成Zip文件格式。
GZIPOutputStream:DeflaterOutputStream的一個子類,把數據壓縮成GZip文件格式
InflaterInputStream:解壓縮類的基類
ZipInputStream:InflaterInputStream的一個子類,能解壓縮Zip格式的數據
GZIPInputStream:InflaterInputStream的一個子類,能解壓縮Zip格式的數據
ZipEntry類:表示 ZIP 文件條目
ZipFile類:此類用於從 ZIP 文件讀取條目
用GZIP進行對單個文件壓縮
GZIP的接口比較簡單,因此如果你只需對一個流進行壓縮的話,可以使用它。當然它可以壓縮字符流,與可以壓縮字節流,下面是一個對GBK編碼格式的文本文件進行壓縮的。
壓縮類的用法非常簡單;只要用GZIPOutputStream 或ZipOutputStream把輸出流包起來,再用GZIPInputStream 或ZipInputStream把輸入流包起來就行了。剩下的都是些普通的I/O操作。
package com.apache.gzip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/** 利用apache提供的ant.jar,提供對單個文件與目錄的壓縮,並支持是否需要創建壓縮源目錄、中文路徑
* @Title:
* @Description:ZipCompress
* @Version 1.2
*/
public class ZipCompress {
private static boolean isCreateSrcDir = true;//是否創建源目錄
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String src = "f:\\中文包";//指定壓縮源,可以是目錄或文件
String decompressDir = "f:\\depress";//解壓路徑
String archive = "f:\\中文壓縮文件.zip";//壓縮包路徑
String comment = "Java Zip 測試.";//壓縮包註釋
//----壓縮文件或目錄
writeByApacheZipOutputStream(src,archive,comment);
/*
* 讀壓縮文件,註釋掉,因爲使用的是apache的壓縮類,所以使用java類庫中
* 解壓類時出錯,這裏不能運行
*/
readByZipInputStream(archive, decompressDir);
//----使用apace ZipFile讀取壓縮文件
readByApacheZipFile(archive, decompressDir);
}
/**對文件夾或者文件進行壓縮
*
* @Time 2012-3-9 上午09:32:35 create
* @param src
* @param archive
* @param comment
* @throws FileNotFoundException
* @throws IOException
* @author jiangzhenming
*/
public static void writeByApacheZipOutputStream(String src, String archive,
String comment) throws FileNotFoundException, IOException {
//----壓縮文件:
FileOutputStream f = new FileOutputStream(archive);
//使用指定校驗和創建輸出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream zos = new ZipOutputStream(csum);
//支持中文
zos.setEncoding("GBK");
BufferedOutputStream out = new BufferedOutputStream(zos);
//設置壓縮包註釋
zos.setComment(comment);
//啓用壓縮
zos.setMethod(ZipOutputStream.DEFLATED);
//壓縮級別爲最強壓縮,但時間要花得多一點
zos.setLevel(Deflater.BEST_COMPRESSION);
File srcFile = new File(src);
if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {
throw new FileNotFoundException(
"File must exist and ZIP file must have at least one entry.");
}
//獲取壓縮源所在父目錄
src = src.replaceAll("\\\\", "/");
String prefixDir = null;
if (srcFile.isFile()) {
prefixDir = src.substring(0, src.lastIndexOf("/") + 1);
} else {
prefixDir = (src.replaceAll("/$", "") + "/");
}
//如果不是根目錄
if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {
prefixDir = prefixDir.replaceAll("[^/]+/$", "");
}
//開始壓縮
writeRecursive(zos, out, srcFile, prefixDir);
out.close();
// 注:校驗和要在流關閉後才準備,一定要放在流被關閉後使用
System.out.println("Checksum: " + csum.getChecksum().getValue());
BufferedInputStream bi;
}
/**
* 使用 org.apache.tools.zip.ZipFile 解壓文件,它與 java 類庫中的
* java.util.zip.ZipFile 使用方式是一新的,只不過多了設置編碼方式的
* 接口。
*
* 注,apache 沒有提供 ZipInputStream 類,所以只能使用它提供的ZipFile
* 來讀取壓縮文件。
* @param archive 壓縮包路徑
* @param decompressDir 解壓路徑
* @throws IOException
* @throws FileNotFoundException
* @throws ZipException
*/
public static void readByApacheZipFile(String archive, String decompressDir)
throws IOException, FileNotFoundException, ZipException {
BufferedInputStream bi;
ZipFile zf = new ZipFile(archive, "GBK");//支持中文
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = decompressDir + "/" + entryName;
if (ze2.isDirectory()) {
System.out.println("正在創建解壓目錄 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
System.out.println("正在創建解壓文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
decompressDir + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();
}
/**
* 使用 java api 中的 ZipInputStream 類解壓文件,但如果壓縮時採用了
* org.apache.tools.zip.ZipOutputStream時,而不是 java 類庫中的
* java.util.zip.ZipOutputStream時,該方法不能使用,原因就是編碼方
* 式不一致導致,運行時會拋如下異常:
* java.lang.IllegalArgumentException
* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290)
*
* 當然,如果壓縮包使用的是java類庫的java.util.zip.ZipOutputStream
* 壓縮而成是不會有問題的,但它不支持中文
*
* @param archive 壓縮包路徑
* @param decompressDir 解壓路徑
* @throws FileNotFoundException
* @throws IOException
*/
public static void readByZipInputStream(String archive, String decompressDir)
throws FileNotFoundException, IOException {
BufferedInputStream bi;
//----解壓文件(ZIP文件的解壓縮實質上就是從輸入流中讀取數據):
System.out.println("開始讀壓縮文件");
FileInputStream fi = new FileInputStream(archive);
CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32());
ZipInputStream in2 = new ZipInputStream(csumi);
bi = new BufferedInputStream(in2);
java.util.zip.ZipEntry ze;//壓縮文件條目
//遍歷壓縮包中的文件條目
while ((ze = in2.getNextEntry()) != null) {
String entryName = ze.getName();
if (ze.isDirectory()) {
System.out.println("正在創建解壓目錄 - " + entryName);
File decompressDirFile = new File(decompressDir + "/" + entryName);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
System.out.println("正在創建解壓文件 - " + entryName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
decompressDir + "/" + entryName));
byte[] buffer = new byte[1024];
int readCount = bi.read(buffer);
while (readCount != -1) {
bos.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
bos.close();
}
}
bi.close();
System.out.println("Checksum: " + csumi.getChecksum().getValue());
}
/**
* 遞歸壓縮
*
* 使用 org.apache.tools.zip.ZipOutputStream 類進行壓縮,它的好處就是支持中文路徑,
* 而Java類庫中的 java.util.zip.ZipOutputStream 壓縮中文文件名時壓縮包會出現亂碼。
* 使用 apache 中的這個類與 java 類庫中的用法是一新的,只是能設置編碼方式了。
*
* @param zos
* @param bo
* @param srcFile
* @param prefixDir
* @throws IOException
* @throws FileNotFoundException
*/
private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,
File srcFile, String prefixDir) throws IOException, FileNotFoundException {
ZipEntry zipEntry;
String filePath = srcFile.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(
"//", "/");
if (srcFile.isDirectory()) {
filePath = filePath.replaceAll("/$", "") + "/";
}
String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");
if (srcFile.isDirectory()) {
if (!"".equals(entryName)) {
System.out.println("正在創建目錄 - " + srcFile.getAbsolutePath()
+ " entryName=" + entryName);
//如果是目錄,則需要在寫目錄後面加上 /
zipEntry = new ZipEntry(entryName + "/");
zos.putNextEntry(zipEntry);
}
File srcFiles[] = srcFile.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
writeRecursive(zos, bo, srcFiles[i], prefixDir);
}
} else {
System.out.println("正在寫文件 - " + srcFile.getAbsolutePath() + " entryName="
+ entryName);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));
//開始寫入新的ZIP文件條目並將流定位到條目數據的開始處
zipEntry = new ZipEntry(entryName);
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int readCount = bi.read(buffer);
while (readCount != -1) {
bo.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
//注,在使用緩衝流寫壓縮文件時,一個條件完後一定要刷新一把,不
//然可能有的內容就會存入到後麪條目中去了
bo.flush();
//文件讀完後關閉
bi.close();
}
}
}