Java解压缩ZIP文件

我们需要用到第三方的jar包:winszipaes_zh_CN_supported_20120416.jar

下载地址:

http://pan.baidu.com/s/1hqfi5Pa

这个包还依赖org.apache.commons.io.jar包。

下载地址:

http://pan.baidu.com/s/1eQuLDXg

至于使用方式,只要看一下DecryptionZipUtil.java就会了。


package com.ninemax.demo.zip.decrypt.zh_cn;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;

import org.apache.commons.io.FileUtils;

import de.idyl.winzipaes.AesZipFileDecrypter;
import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESDecrypter;
import de.idyl.winzipaes.impl.AESDecrypterBC;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import de.idyl.winzipaes.impl.ExtZipEntry;

/**
 * 压缩指定文件或目录为ZIP格式压缩文件
 * 支持中文(修改源码后)
 * 支持密码(仅支持256bit的AES加密解密)
 * 依赖bcprov项目(bcprov-jdk16-140.jar)
 * 
 * @author zyh
 */
public class DecryptionZipUtil {
	
	/**
	 * 使用指定密码将给定文件或文件夹压缩成指定的输出ZIP文件
	 * @param srcFile 需要压缩的文件或文件夹
	 * @param destPath 输出路径
	 * @param passwd 压缩文件使用的密码
	 */
	public static void zip(String srcFile,String destPath,String passwd) {
		AESEncrypter encrypter = new AESEncrypterBC();
		AesZipFileEncrypter zipFileEncrypter = null;
		try {
			zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);
			/**
			 * 此方法是修改源码后添加,用以支持中文文件名
			 */
			zipFileEncrypter.setEncoding("utf8");
			File sFile = new File(srcFile);
			/**
			 * AesZipFileEncrypter提供了重载的添加Entry的方法,其中:
			 * add(File f, String passwd) 
			 * 			方法是将文件直接添加进压缩文件
			 * 
			 * add(File f,  String pathForEntry, String passwd)
			 * 			方法是按指定路径将文件添加进压缩文件
			 * pathForEntry - to be used for addition of the file (path within zip file)
			 */
			doZip(sFile, zipFileEncrypter, "", passwd);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				zipFileEncrypter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 具体压缩方法,将给定文件添加进压缩文件中,并处理压缩文件中的路径
	 * @param file 给定磁盘文件(是文件直接添加,是目录递归调用添加)
	 * @param encrypter AesZipFileEncrypter实例,用于输出加密ZIP文件
	 * @param pathForEntry ZIP文件中的路径
	 * @param passwd 压缩密码
	 * @throws IOException
	 */
	private static void doZip(File file, AesZipFileEncrypter encrypter,
			String pathForEntry, String passwd) throws IOException {
		if (file.isFile()) {
			pathForEntry += file.getName();
			encrypter.add(file, pathForEntry, passwd);
			return;
		}
		pathForEntry += file.getName() + File.separator;
		for(File subFile : file.listFiles()) {
			doZip(subFile, encrypter, pathForEntry, passwd);
		}
	}
	
	/**
	 * 使用给定密码解压指定压缩文件到指定目录
	 * @param inFile 指定Zip文件
	 * @param outDir 解压目录
	 * @param passwd 解压密码
	 */
	public static void unzip(String inFile, String outDir, String passwd) {
		File outDirectory = new File(outDir);
		if (!outDirectory.exists()) {
			outDirectory.mkdir();
		}
		AESDecrypter decrypter = new AESDecrypterBC();
		AesZipFileDecrypter zipDecrypter = null;
		try {
			zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);
			AesZipFileDecrypter.charset = "utf-8";
			/**
			 * 得到ZIP文件中所有Entry,但此处好像与JDK里不同,目录不视为Entry
			 * 需要创建文件夹,entry.isDirectory()方法同样不适用,不知道是不是自己使用错误
			 * 处理文件夹问题处理可能不太好
			 */
			List<ExtZipEntry> entryList = zipDecrypter.getEntryList();
			for(ExtZipEntry entry : entryList) {
				String eName = entry.getName();
				String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);
				File extractDir = new File(outDir, dir);
				if (!extractDir.exists()) {
					FileUtils.forceMkdir(extractDir);
				}
				/**
				 * 抽出文件
				 */
				File extractFile = new File(outDir + File.separator + eName);
				zipDecrypter.extractEntry(entry, extractFile, passwd);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DataFormatException e) {
			e.printStackTrace();
		} finally {
			try {
				zipDecrypter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 压缩测试
		 * 可以传文件或者目录
		 */
//		zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");
//		zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");
		
		unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");
	}
}


解压缩zip文件中含有中文名的文件时,可能会出现乱码或者解压失败的问题。这是因为在压缩文件时,文件名使用了默认编码格式,而在解压缩时,解压软件使用了不同的编码格式,导致文件名解析错误。 为了解决这个问题,可以通过指定解压缩文件名的编码格式来解决。以下是一个Java程序示例,用于解压缩zip文件并处理中文文件名的编码问题: ```java import java.io.*; import java.util.*; import java.util.zip.*; public class ZipUtils { public static void unzip(File zipFile, File destDir, String charset) throws IOException { if (!destDir.exists()) { destDir.mkdirs(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile), Charset.forName(charset)); ZipEntry entry; byte[] buffer = new byte[1024]; while ((entry = zipIn.getNextEntry()) != null) { String fileName = entry.getName(); if (entry.isDirectory()) { File subDir = new File(destDir, fileName); subDir.mkdirs(); continue; } File file = new File(destDir, fileName); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); int len; while ((len = zipIn.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); } zipIn.close(); } } ``` 在调用此方法时,可以指定解压缩文件名的编码格式,例如: ```java ZipUtils.unzip(new File("sun.zip"), new File("destDir"), "GBK"); ``` 这里的编码格式使用了GBK,根据实际情况可以修改为其他编码格式。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值