java Zip文件解压缩

java Zip文件解压缩

为了解压缩zip都折腾两天了,查看了许多谷歌、百度来的code,
真实无语了,绝大多数是不能用的。这可能跟我的开发环境有关吧。
我用的是Ubuntu14.04,eclipse 用的是STS3.5,jdk81.8.0_20
经过两天的努力检验了无数的code终于让我找到一个还能用的可以解决中文乱码问题。
这个项目用maven构建的依赖jar坐标如下

<!-- 用于zip文件解压缩 -->
<dependency>
    <groupId>ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.7.0</version>
</dependency>

代码如下:

package com.uujava.mbfy.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tools.zip.ZipOutputStream;
/**
 * @author k
 * @date 2014年10月7日 上午8:04:51
 * @Description: TODO(用于压缩和解压缩zip文件)
 * 尚须解决bug:
 * 这里采用硬编码这定文件编码,是否有办法读取压缩文件时判断起内部文件名编码。
 */
public final class ZipUtils {

	public static void main(String[] args) throws Exception {
		// ZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip", "/home/k/Documents/testzip/index.html");
		unZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip",
				"/home/k/Documents/testzip/zip");
	}
	public static void zip(ZipOutputStream out, File f, String base,
			boolean first) throws Exception {
		if (first) {
			if (f.isDirectory()) {
				out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));
				base = base + f.getName();
				first = false;
			} else
				base = f.getName();
		}
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			base = base + "/";
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName(), first);
			}
		} else {
			out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int b;
			System.out.println(base);
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();
		}
	}

	@SuppressWarnings("unchecked")
	public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,
			String unZipRoot) throws Exception, IOException {
		java.util.Enumeration e = zipFile.getEntries();
		System.out.println(zipFile.getEncoding());
		org.apache.tools.zip.ZipEntry zipEntry;
		while (e.hasMoreElements()) {
			zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
			InputStream fis = zipFile.getInputStream(zipEntry);
			if (zipEntry.isDirectory()) {
			} else {
				File file = new File(unZipRoot + File.separator
						+ zipEntry.getName());
				File parentFile = file.getParentFile();
				parentFile.mkdirs();
				FileOutputStream fos = new FileOutputStream(file);
				byte[] b = new byte[1024];
				int len;
				while ((len = fis.read(b, 0, b.length)) != -1) {
					fos.write(b, 0, len);
				}
				fos.close();
				fis.close();
			}
		}
	}

	public static void ZipFile(String zipFileName, String inputFileName)
			throws Exception {
		org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(
				new FileOutputStream(zipFileName));
		out.setEncoding("gbk");// 设置的和文件名字格式一样或开发环境编码设置一样的话就能正常显示了
		File inputFile = new File(inputFileName);
		zip(out, inputFile, "", true);
		System.out.println("zip done");
		out.close();
	}

	public static void unZipFile(String unZipFileName, String unZipPath)
			throws Exception {
		org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
				unZipFileName, "gbk");
		unZipFileByOpache(zipFile, unZipPath);
		System.out.println("unZip Ok");
	}

}


原文地址:https://www.cnblogs.com/mxcy/p/ZipUtils.html