ITeye博客

java unzip ant

openstudy · 2011-01-04 16:55 · 编程语言 · Markdown

java解压缩zip文件,依赖ant jar包,没乱码

import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration;

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

public class ZipFileUtil { public static void unZip(String zipFileName, String destDir) { File unzipFile = new File(zipFileName); if (destDir == null || destDir.trim().length() == 0) { destDir = unzipFile.getParent(); }

	ZipFile zipFile = null;
	try {
		zipFile = new ZipFile(unzipFile, "GBK");
		Enumeration entries = zipFile.getEntries();
		while(entries.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			File destFile = new File(destDir, entry.getName());

			if (entry.isDirectory()) {// 是目录,则创建之
				destFile.mkdirs();
			} else {// 是文件
				// 如果指定文件的父目录不存在,则创建之.
				File parent = destFile.getParentFile();
				if (parent != null && !parent.exists()) {
					parent.mkdirs();
				}
				
				//执行解压
				InputStream inputStream = zipFile.getInputStream(entry);
				FileOutputStream fileOut = new FileOutputStream(destFile);
				byte buf = new byte;
				int readedBytes;
				while ((readedBytes = inputStream.read(buf)) > 0) {
					fileOut.write(buf, 0, readedBytes);
				}
				fileOut.close();
				inputStream.close();
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if(zipFile != null){
				zipFile.close();
			}
		} catch (Exception e) {
		}
	}
}

public static void main(String args) {
	String inFile = "D:/test测试.zip";
	String dir = "D:/log/out";
	unZip(inFile, dir);
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。