67,538
社区成员
发帖
与我相关
我的任务
分享/**
* 测试解压缩功能. 将d:\\download\\test.zip文件解压到d:\\temp\\zipout目录下.
* @param baseDir 目录
* @param file 要解压的ZIP文件
* @throws Exception
*/
public static void ExtractZip(String baseDir,String file) throws Exception {
// InputStream is=new BufferedInputStream(new FileInputStream());
//String baseDir = "D:\\workspace\\swing(js)\\";
ZipFile zfile = new ZipFile(baseDir + File.separator + file);
//System.out.println(zfile.getEntries());
Enumeration zList = zfile.getEntries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
if(ze.getSize()<=0)
throw new Exception("文件长度为0");
System.out.println("Extracting: " + ze.getName() + "\t"
+ ze.getSize() + "\t" + ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(baseDir, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
System.out.println("Extracted: " + ze.getName());
}
zfile.close();
}