Apache Commons Compress 定义了tar, zip, gzip等一系列关于压缩文件的API ,弥补了JDK中在这方面的不足,官网链接:http://commons.apache.org/proper/commons-compress/index.html
官方介绍如下:
The Apache Commons Compress library defines an API for working with ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200, bzip2, 7z, arj, lzma, snappy, DEFLATE and Z files.
The code in this component has many origins:
The bzip2, tar and zip support came from Avalon's Excalibur, but originally from Ant, as far as life in Apache goes. The tar package is originally Tim Endres' public domain package. The bzip2 package is based on the work done by Keiron Liddle as well as Julian Seward's libbzip2. It has migrated via:
Ant -> Avalon-Excalibur -> Commons-IO -> Commons-Compress.
The cpio package has been contributed by Michael Kuss and the jRPM project.
在官网的Examples页面有大量关于Apache Commons Compress library API使用的介绍与Demo,有兴趣的同学可以去好好研究一下!
这里我使用Apache Commons Compress实现了Zip文件的 压缩和读取功能,如有不对地方恳请大家指出!
package com.ricky.java.compress;
import java.io.BufferedInputStream;
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 java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
public class ApacheCompressTest {
/**
* @param args
*/
public static void main(String[] args) {
// testWriteZip();
testReadZip();
}
protected static void testReadZip() {
readZip(new File("E:/第三方类库/commons-compress-1.9-bin.zip"));
}
protected static void testWriteZip() {
File srcFile = new File("D:/export/tel.json");
File destFile = new File("D:/export/tel.zip");
writeZip(srcFile, destFile, "tel");
}
public static void unZip(File srcFile, String destPath) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<ZipArchiveEntry> e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipArchiveEntry entry = e.nextElement();
File file = new File(destPath, entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
InputStream in = zipFile.getInputStream(entry);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
OutputStream out = new FileOutputStream(file);
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void readZip(File zipFile) {
ZipArchiveInputStream zipInput = null;
try {
zipInput = new ZipArchiveInputStream(new FileInputStream(zipFile));
ZipArchiveEntry zipEntry = zipInput.getNextZipEntry();
while (zipEntry != null) {
String name = zipEntry.getName();
if (zipEntry.isDirectory()) {
System.out.println("-d name=" + name);
} else {
System.out.println("-name=" + name);
}
zipEntry = zipInput.getNextZipEntry();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipInput != null) {
try {
zipInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void writeZip(File srcFile, File destFile, String name) {
ZipArchiveOutputStream zipOutput = null;
InputStream in = null;
try {
zipOutput = new ZipArchiveOutputStream(new FileOutputStream(
destFile));
in = new BufferedInputStream(new FileInputStream(srcFile));
ZipArchiveEntry entry = null;
if (name != null && name.length() > 0) {
entry = new ZipArchiveEntry(name);
} else {
entry = new ZipArchiveEntry(srcFile.getName());
}
entry.setSize(srcFile.length());
zipOutput.putArchiveEntry(entry);
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
zipOutput.write(buffer, 0, len);
}
zipOutput.closeArchiveEntry();
//可继续添加 文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zipOutput != null) {
try {
zipOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}