通过这个类可以直接操作里面的compress和Decompress静态方法来压缩和解压缩文件。不多说了,直接上代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Zip{
public static final String EXT = ".zip";
private static final String BASE_DIR = "";
// 符号"/"用来作为目录标识判断符
private static final String PATH = "/";
private static final int BUFFER = 2048;
public static void compress(String fileName) throws Exception{
//fileName为指定路径下的待压缩文件
File srcFile=new File(fileName);
String currentPath=srcFile.getPath();
String destPath= currentPath +EXT;
File destFile=new File(destPath);
//采用最常见的CRC32进行解压
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
destFile), new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
compress(srcFile, zos, BASE_DIR);
zos.flush();
zos.close();
}
private static void compress(File srcFile, ZipOutputStream zos,
String basePath) throws Exception {
if (srcFile.isDirectory()) {
compressDir(srcFile, zos, basePath);
} else {
compressFile(srcFile, zos, basePath);
}
}
private static void compressDir(File dir, ZipOutputStream zos,
String basePath) throws Exception {
//列出目录下的所有子目录,这些子目录可能是文件也可能仍旧是目录,采用递归方式压缩
File[] files = dir.listFiles();
// 构建空目录
if (files.length < 1) {
ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);
zos.putNextEntry(entry);
zos.closeEntry();
}
for (File file : files) {
// 递归压缩
compress(file, zos, basePath + dir.getName() + PATH);
}
}
//file为待压缩文件,dir为压缩文件中的当前路径
private static void compressFile(File file, ZipOutputStream zos, String dir)
throws Exception {
//entry为待压缩条目
ZipEntry entry = new ZipEntry(dir + file.getName());
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
//以下实现代码解压缩
public static void Decompress(String srcPath) throws Exception{
File srcFile=new File(srcPath);
String basePath = srcFile.getParent();
File destFile=new File(basePath);
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
Decompress(destFile, zis);
zis.close();
}
private static void Decompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null) {
// 文件
String dir = destFile.getPath() + File.separator + entry.getName();
File dirFile = new File(dir);
fileProber(dirFile);
if (entry.isDirectory()){
dirFile.mkdirs();
}else{
DecompressFile(dirFile, zis);
}
}
}
//当父目录不存在时候,创建目录
private static void fileProber(File dirFile) {
File parentFile = dirFile.getParentFile();
if(!parentFile.exists()){
fileProber(parentFile);
parentFile.mkdir();
}
}
private static void DecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int count;
byte data[] = new byte[BUFFER];
while ((count = zis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.close();
}
}
假设D盘下面有一个zhengZe目录,zhengZe目录下面有一个TextFile.txt文件。我们通过以下方式分别对目录和文件进行压缩: