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;
-
-
-
-
-
-
- public class ZipCompress {
-
- private static boolean isCreateSrcDir = true;
-
-
-
-
-
- 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);
-
-
-
-
- readByZipInputStream(archive, decompressDir);
-
- readByApacheZipFile(archive, decompressDir);
- }
-
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void readByZipInputStream(String archive, String decompressDir)
- throws FileNotFoundException, IOException {
- BufferedInputStream bi;
-
- 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());
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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));
-
-
- 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();
- }
- }
- }
转载:http://write.blog.csdn.net/postedit