//java.util.zip 解压zip文件,不能解决中文乱码的问题
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZIPUtil {
public static void main(String[] args) throws Exception {
unzip("c:/1.zip", "c:");
}
static String getSuffixName(String name) {
return name.substring(0, name.lastIndexOf("."));
}
public static void unzip(String zipFilePath, String unzipDirectory)
throws Exception {
// 创建文件对象
File file = new File(zipFilePath);
// 创建zip文件对象
ZipFile zipFile = new ZipFile(file);
// 创建本zip文件解压目录
File unzipFile = new File(unzipDirectory + "/"
+ getSuffixName(file.getName()));
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdir();
// 得到zip文件条目枚举对象
Enumeration zipEnum = zipFile.entries();
// 定义输入输出流对象
InputStream input = null;
OutputStream output = null;
// 循环读取条目
System.out
.println("name\t\t\tsize\t\t\tcompressedSize\t\t\tisDirectory");
while (zipEnum.hasMoreElements()) {
// 得到当前条目
ZipEntry entry = (ZipEntry) zipEnum.nextElement();
String entryName = new String(entry.getName().getBytes("ISO8859_1"));
System.out.println(entryName + "\t\t\t" + entry.getSize()
+ "\t\t\t" + entry.getCompressedSize() + "\t\t\t\t\t\t\t"
+ entry.isDirectory());
// 若当前条目为目录则创建
if (entry.isDirectory())
new File(unzipFile.getAbsolutePath() + "/" + entryName).mkdir();
else { // 若当前条目为文件则解压到相应目录
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(unzipFile
.getAbsolutePath()
+ "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
input.close();
output.flush();
output.close();
}
}
}
// 返回条目的注释字符串;如果没有,则返回 null
// System.out.println(entry.getComment());
// 返回未压缩条目数据的 CRC-32 校验和;如果未知,则返回 -1
// System.out.println(entry.getCrc());
// 返回条目的压缩方法;如果未指定,则返回 -1
// System.out.println(entry.getMethod());
// 返回条目的额外字段数据;如果没有,则返回 null
// System.out.println(entry.getExtra());
// 返回条目的修改时间;如果未指定,则返回 -1
// System.out.println(entry.getTime());
}
//使用apache ant version1.7的tools.zip来解压zip文件,解决中文问题
/*
*sunjun
*2007-12-26
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class ZIPUtil {
/**
* 创建目录
*
* @param path
* 目录绝对路径名
*/
static void createDir(String path) {
File dir = new File(path);
if (dir.exists() == false)
dir.mkdir();
}
/**
* 取得文件名,不包含后缀名
*
* @param name
* 完整文件名
* @return 文件名(不包含后缀名)
*/
static String getSuffixName(String name) {
return name.substring(0, name.lastIndexOf("."));
}
public static void main(String[] args) throws Exception {
unzip("F:/aaaaaaaa.zip", "F:");
System.out.println("over....................");
}
/**
* 解压zip文件
*
* @param zipFilePath
* zip文件绝对路径
* @param unzipDirectory
* 解压到的确
* @throws Exception
*/
public static void unzip(String zipFilePath, String unzipDirectory)
throws Exception {
// 创建文件对象
File file = new File(zipFilePath);
// 创建zip文件对象
ZipFile zipFile = new ZipFile(file);
// 创建本zip文件解压目录
File unzipFile = new File(unzipDirectory + "/"
+ getSuffixName(file.getName()));
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdir();
// 得到zip文件条目枚举对象
Enumeration zipEnum = zipFile.getEntries();
// 定义输入输出流对象
InputStream input = null;
OutputStream output = null;
// 定义对象
ZipEntry entry = null;
// 循环读取条目
while (zipEnum.hasMoreElements()) {
// 得到当前条目
entry = (ZipEntry) zipEnum.nextElement();
String entryName = new String(entry.getName());
// 用/分隔条目名称
String names[] = entryName.split("\\/");
int length = names.length;
String path = unzipFile.getAbsolutePath();
for (int v = 0; v < length; v++) {
if (v < length - 1) { // 最后一个目录之前的目录
path += "/" + names[v] + "/";
createDir(path);
} else { // 最后一个
if (entryName.endsWith("/")) // 为目录,则创建文件夹
createDir(unzipFile.getAbsolutePath() + "/" + entryName);
else { // 为文件,则输出到文件
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(unzipFile
.getAbsolutePath()
+ "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
// 关闭流
input.close();
output.flush();
output.close();
}
}
}
}
}
}