---
title: "java unzip ant"
author: "openstudy"
date: "2011-01-04T16:55:37Z"
tags: []
categories: ["编程语言"]
slug: "java-unzip-ant"
draft: false
lastmod: "2011-01-04T16:55:37Z"
id: 858869
url: "https://openstudy.iteye.com/blog/858869"
---

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);
	}
}