您的位置:首页 > 编程语言 > Java开发

java实现zip压缩及解压

2010-11-28 16:28 375 查看
java JDK中提供了处理zip文件的API,不过无法处理编码方式,不支持中文编码,apache提供了一组处理zip文件的API,包含在ant.jar,支持中文编码

,以下为本人用apache ant实现zip压缩及解压的代码:

package com.work.study.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
* 使用apache API处理zip格式压缩
* @author wison Xu
*/
public class ZipTool {

public static final String SYS_ENCODING = System.getProperty("file.encoding");

/**
* 压缩文件
* @param files 文件数组
* @param zipFilePath zip文件位置
* @throws IOException
*/
public static void zip(String[] files, String zipFilePath) throws IOException {
OutputStream out = null;
OutputStream buffOut = null;
ZipOutputStream zout = null;
try {
out = new FileOutputStream(zipFilePath);
buffOut = new BufferedOutputStream(out);
zout = new ZipOutputStream(buffOut);
zout.setEncoding(SYS_ENCODING);	//设置编码方式

for (int i = 0; i < files.length; i++) {
File file = new File(files[i]);
zip(file, "", zout);	//递归压缩
}
} finally {
if (zout != null)
zout.close();
if (buffOut != null)
buffOut.close();
if (out != null)
out.close();
}
}

private static void zip(File file, String parentEntryName, ZipOutputStream zout) throws IOException {
String entryName = file.getName();
// 构建条目名称(名称中应包含父文件夹条目,如parent_dir/child_file)
if (parentEntryName != null && !"".equals(parentEntryName.trim())) {
entryName = parentEntryName + "/" + file.getName();
}
if (file.isDirectory()) {
File[] files = file.listFiles();
//如果是空文件夹,则直接往输出流放入文件夹条目
if (files.length == 0) {
ZipEntry entry = new ZipEntry(entryName+"/");
zout.putNextEntry(entry);
}
//如果不是空文件夹,则递归其下的子文件
else {
for (int i = 0; i < files.length; i++) {
File subfile = files[i];
zip(subfile, entryName, zout);
}
}
} else {
ZipEntry entry = new ZipEntry(entryName);
zip(file, entry, zout);
}
}

private static void zip(File file, ZipEntry entry, ZipOutputStream zout) throws IOException {
InputStream in = null;
BufferedInputStream buffIn = null;
try {
in = new FileInputStream(file);
buffIn = new BufferedInputStream(in, 1024);
System.out.println("compressing:" + file.getPath());
zout.putNextEntry(entry);
byte[] buff = new byte[1024];
int len = 0;
while ((len = buffIn.read(buff)) != -1) {
zout.write(buff, 0, len);
}
zout.closeEntry();
} catch (IOException e) {
throw e;
} finally {
if(buffIn != null)
buffIn.close();
if (in != null)
in.close();
}
}

/**
* 解压zip文件
* @param zipFilePath 带解压的zip文件
* @param targetDir 解压的目标文件路径
* @throws IOException
*/
public static void unzip(String zipFilePath, String targetDir) throws IOException {
File zFile = new File(zipFilePath);
ZipFile zipFile = new ZipFile(zFile, SYS_ENCODING);

// 检查目标文件目录,如果不存在则创建
if(targetDir != null && !"".equals(targetDir.trim())) {
File file = new File(targetDir);
if (!file.exists()) {
if(!file.mkdirs()) {
throw new IOException(targetDir + " is illegal!");
}
} else if (file.isFile()) {
throw new IOException(targetDir + " is a file!");
}
} else {
targetDir = zFile.getParent();
}

Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
// 如果是文件夹条目,则创建该文件夹
if (entry.isDirectory()) {
new File(targetDir + "/" + entryName).mkdirs();
}
// 如果是文件条目,则解压该文件
else {
String dir = entryName.substring(0, entryName.lastIndexOf("/"));
new File(targetDir + "/" + dir).mkdirs();
BufferedOutputStream buffOut = null;
OutputStream out = null;
InputStream in = null;
try {
System.out.println("Extracting:" + entryName);
out = new FileOutputStream(targetDir + File.separator + entryName);
buffOut = new BufferedOutputStream(out, 1024);
in = zipFile.getInputStream(entry);
int len = 0;
byte[] buff = new byte[1024];
while ((len = in.read(buff)) != -1) {
buffOut.write(buff, 0, len);
}
} catch (IOException e) {
System.out.println("/t>>>failed!");
throw e;
} finally {
if(buffOut != null)
buffOut.close();
if(out != null)
out.close();
if(in != null)
in.close();
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: