import java.util.*;
import java.util.zip.*;
import java.io.*;
public class TryUnzip {
public TryUnzip() {
}
/**
*
* 解压处理
*
* @param zipFileName
* 要进行压缩处理的文件(d:\\aa.zip ")
* @param outputDirectory
* 压缩处理后的文件路径(d:\\)
*/
public void unzip(String zipFileName, String outputDirectory)
throws Exception {
try {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
zipFileName);
java.util.Enumeration e = zipFile.getEntries();
org.apache.tools.zip.ZipEntry zipEntry = null;
while (e.hasMoreElements()) {
zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
System.out.println( "unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
System.out.println( "输出路径: " + outputDirectory
+ File.separator + name);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println( "创建目录: " + outputDirectory
+ File.separator + name);
} else {
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
// --------解决了图片失真的情况
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.close();
in.close();
}
}
} catch (Exception ex) {
}
}
public static void main(String[] args) {
try {
TryUnzip test = new TryUnzip();
test.unzip( "e:\\ok.zip ", "e:\\ ");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
这个源码是在网上找的,有很多回帖的人说可以用。ant.jar是在自己机器上搜的,里面有ZipFile这个类,可是就不不成功。
Exception in thread "main " java.lang.NoSuchMethodError: org.apache.tools.zip.ZipEntry: method <init> ()V not found
at org.apache.tools.zip.ZipFile.populateFromCentralDirectory(ZipFile.java:253)
at org.apache.tools.zip.ZipFile. <init> (ZipFile.java:142)
at org.apache.tools.zip.ZipFile. <init> (ZipFile.java:113)
at com.vxichina.esb.util.TryUnzip.unzip(TryUnzip.java:19)
at com.vxichina.esb.util.TryUnzip.main(TryUnzip.java:62)
会不会是ant.jar的问题?麻烦试验成功的宝贝儿们将这个包发给我,谢谢~!~!