Java 解压 zip 文件

参考链接:https://qtdebug.com/util-unzip/

解决mac 系统 和window系统 解压zip 文件  有乱码的问题

添加maven包

      

    org.apache.commons
    commons-compress
    1.18

       
       

    commons-io
    commons-io
    2.6

       

代码:

package com.my.mml.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;

public class ExtractZip {

	public static final int BUFFER_SIZE = 1024;

	/**
	 * 解压 zip 文件
	 * 
	 * @param zipFile
	 *            zip 压缩文件
	 * @param destDir
	 *            zip 压缩文件解压后保存的目录
	 * @param encoding
	 *            zip 文件的编码
	 * @return 返回 zip 压缩文件里的文件名的 list
	 * @throws Exception
	 */
	public static List unZip(File zipFile, String destDir, String encoding) throws Exception {
		// 如果 destDir 为 null, 空字符串, 或者全是空格, 则解压到压缩文件所在目录
		if (destDir == null || destDir.length() == 0) {
			destDir = zipFile.getParent();
		}

		destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator;
		ZipArchiveInputStream is = null;
		List fileNames = new ArrayList();

		try {
			is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE),
					encoding);
			ZipArchiveEntry entry = null;

			while ((entry = is.getNextZipEntry()) != null) {
				fileNames.add(entry.getName());
				File file = new File(destDir, entry.getName());

				if (entry.isDirectory()) {
					FileUtils.forceMkdir(file); // 创建文件夹,如果中间有路径会自动创建
				} else {
					OutputStream os = null;

					try {
						FileUtils.touch(file);
						os = new FileOutputStream(new File(destDir, entry.getName()));
						IOUtils.copy(is, os);
					} finally {
						IOUtils.closeQuietly(os);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			IOUtils.closeQuietly(is);
		}

		return fileNames;
	}

	/**
	 * 解压 zip 文件
	 * 
	 * @param zipFile
	 *            zip 压缩文件的路径
	 * @param destDir
	 *            zip 压缩文件解压后保存的目录
	 * @param encoding
	 *            zip 文件的编码
	 * @return 返回 zip 压缩文件里的文件名的 list
	 * @throws Exception
	 */
	public static List unZip(String zipFile, String destDir, String encoding) throws Exception {
		File zipfile = new File(zipFile);
		return unZip(zipfile, destDir, encoding);
	}

	public static List unZip(String zipFile, String destDir) throws Exception {
		return unZip(zipFile, destDir, "UTF-8");
	}

	public static void main(String[] args) throws Exception {
		List names = unZip("D:/test/ziptest.zip", "d:/test/zipfile/");
		System.out.println(names);
	}

}

 

你可能感兴趣的:(java)