import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
*
* @create 2009-7-25 下午06:17:33
* @since
*/
public class ZipUtil {
public static final String FILE_SEPARATOR = System
.getProperty("file.separator");
/**
* 将指定的文件解压缩到指定的文件夹,解压后的文件夹目录和给定的压缩文件名相同.
*
* @param zipFilePath
* 全路径
* @param unZipDirectory
* 全路径
* @return 解压缩文件是否成功.
* @throws IOException
*/
@SuppressWarnings("unused")
public static boolean unZipFile(String zipFilePath, String unZipDirectory)
throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<?> entries = zipFile.getEntries();
if (zipFile == null) {
return false;
}
while (entries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
File f = new File(unZipDirectory + FILE_SEPARATOR
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!f.exists() && !f.mkdirs())
throw new IOException("Couldn't create directory: " + f);
} else {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(
zipFile.getInputStream(zipEntry));
File destDir = f.getParentFile();
if (!destDir.exists() && !destDir.mkdirs()) {
throw new IOException("Couldn't create dir " + destDir);
}
os = new BufferedOutputStream(new FileOutputStream(f));
int b = -1;
while ((b = is.read()) != -1) {
os.write(b);
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
}
}
}
zipFile.close();
return true;
}
/**
* 压缩一个文件
*
* @param filePath 被压缩的文件路径
* @param zipPath 压缩后的存放路径
* @param zipfileName 压缩后的文件名称
* @return
*/
public static boolean zipFile(String filePath, String zipPath, String zipfileName) {
BufferedReader in = null;
ZipOutputStream out = null;
try {
File file = new File(filePath);
in = new BufferedReader(new InputStreamReader(new FileInputStream(
filePath), "ISO-8859-1"));
FileOutputStream f = new FileOutputStream(zipPath+"/"+zipfileName+".zip");
CheckedOutputStream ch = new CheckedOutputStream(f, new CRC32());
out = new ZipOutputStream(new BufferedOutputStream(ch));
//out.setEncoding("GBK");
int c;
out.putNextEntry(new ZipEntry(file.getName()));
while ((c = in.read()) != -1)
out.write(c);
}
catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 压缩一个目录
* @param dir 被压缩的文件夹
* @param zipPath 压缩后的存放路径
* @param zipfileName 压缩后的文件名称
* @return
*/
public static boolean zipDirectory(String dir, String zipPath, String zipfileName) {
ZipOutputStream out = null;
try {
File dirFile = new File(dir);
if (!dirFile.isDirectory())
return false;
FileOutputStream fo = new FileOutputStream(zipPath+"/"+zipfileName+".zip");
CheckedOutputStream ch = new CheckedOutputStream(fo, new CRC32());
out = new ZipOutputStream(new BufferedOutputStream(ch));
//out.setEncoding("GBK");
zip(out, dirFile, "");
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static void zip(ZipOutputStream out, File f, String base)
throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream is = new FileInputStream(f);
BufferedInputStream in = new BufferedInputStream(is);// 修改BUG!二进制输出采用buffered
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
}
/**
* 对临时生成的文件夹和文件夹下的文件进行删除
*/
public static void deletefile(String delpath) {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + File.separator + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
} else if (delfile.isDirectory()) {
deletefile(delpath + File.separator + filelist[i]);
}
}
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//解压文件后缀必须是zip,否则报错。
// String zipFilePath = "D:\\axis.zip";
// String unZipDirectory = "D:\\";
// try {
// boolean flag = ZipUtil1.unZipFile(zipFilePath, unZipDirectory);
// if (flag) {
// System.out.println("解压成功");
// }
// } catch (IOException e) { // TODO Auto-generated catch block
// e.printStackTrace();
// }
//压缩文件 中文名称测试
//ZipUtil1.zipFile("D:\\副本.log", "D:\\","中");
//压缩目录
ZipUtil.zipDirectory("D:\\新建文件夹", "D:\\", "新建");
//删除临时文件
//ZipUtil1.deletefile("D:\\新建文件夹\\副本.log");
}
}