Java解压和压缩带密码的zip文件过程详解
作者:玄月初心 发布时间:2023-07-16 15:32:16
前言
JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名。
为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzipaes开源项目。
该项目在google code下托管 ,仅支持AES压缩和解压zip文件( This library only supports Win-Zip's 256-Bit AES mode.)。网站上下载的文件是源代码,最新版本为winzipaes_src_20120416.zip,本示例就是在此基础上编写。
详述
项目使用很简单,利用源码自己导出一个jar文件,在项目中引用即可。
这里有一个需要注意的问题,就是如果给定ZIP文件没有密码,那么就不能使用该项目解压,如果压缩文件没有密码却使用该项目解压在这里会报一个异常,所以使用中需要注意:加密ZIP文件可以使用它解压,没有加密的就需要采取其它方式了。
此文就是采用修改后的winzipaes编写,并记录详细修改步骤。
winzipaes项目依赖bcprov的jar包
示例
在研究该项目时写了一个工具类,本来准备用在项目中,最后找到了更好的解决方案zip4j来代替,所以最终没有采用。
package com.ninemax.demo.zip.decrypt;
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");
}
}
压缩多个文件时,有两个方法(第一种没试):
(1) 预先把多个文件压缩成zip,然后调用enc.addAll(inZipFile, password);方法将多个zip文件加进来。
(2)针对需要压缩的文件循环调用enc.add(inFile, password);,每次都用相同的密码。
修改源码后的项目可到上面提到的博客去下载,或者参照博客自己修改,其实也很容易,毕竟只有几处改动。
另外我的CSDN下载频道也上传了修改后的源码和jar包,也可以去那里下载。
修改记录
需要修改的文件有:
ExtZipOutputStream
ExtZipEntry
AesZipFileEncrypter
在ExtZipOutputStream里增加一成员变量并添加两个方法:
protected String encoding = "iso-8859-1";
public boolean utf8Flg = false;
public void setEncoding(String encoding) {
this.encoding = encoding;
utf8Flg |= isUTF8(encoding);
}
protected boolean isUTF8(String encoding) {
if (encoding == null) {
// check platform's default encoding
encoding = System.getProperty("file.encoding");
}
return "UTF8".equalsIgnoreCase(encoding)
|| "UTF-8".equalsIgnoreCase(encoding);
}
然后将ExtZipOutputStream的(134行和158行左右)iso-8859-1编码替换成上面设置的编码格式
接着,再将106行左右文件名长度取得代码改成:
writeShort(entry.getName().getBytes(encoding).length); // file name length
这里有个地方需要注意,当文件名是utf8编码格式的时候,需要设置Zip包的通用位标志 (不明白)
第十一个比特为1,代码修改如下:
修改ExtZipEntry类在initEncryptedEntry方法基础上增加一个重载方法:
public void initEncryptedEntry(boolean utf8Flag) {
setCrc(0); // CRC-32 / for encrypted files it's 0 as AES/MAC checks integritiy
this.flag |= 1; // bit0 - encrypted
if (utf8Flag) {
this.flag |=(1 << 11);
}
// flag |= 8; // bit3 - use data descriptor
this.primaryCompressionMethod = 0x63;
byte[] extraBytes = new byte[11];
extraBytes = new byte[11];
// extra data header ID for AES encryption is 0x9901
extraBytes[0] = 0x01;
extraBytes[1] = (byte)0x99;
// data size (currently 7, but subject to possible increase in the
// future)
extraBytes[2] = 0x07; // data size
extraBytes[3] = 0x00; // data size
// Integer version number specific to the zip vendor
extraBytes[4] = 0x02; // version number
extraBytes[5] = 0x00; // version number
// 2-character vendor ID
extraBytes[6] = 0x41; // vendor id
extraBytes[7] = 0x45; // vendor id
// AES encryption strength - 1=128, 2=192, 3=256
extraBytes[8] = 0x03;
// actual compression method - 0x0000==stored (no compression) - 2 bytes
extraBytes[9] = (byte) (getMethod() & 0xff);
extraBytes[10] = (byte) ((getMethod() & 0xff00) >> 8);
setExtra(extraBytes);
}
其实就是增加一个参数并增加了下面这段代码:
if (utf8Flag) {
this.flag |=(1 << 11);
}
当然不要忘了将调用该方法地方修改一下,传进utf8Flag参数
AesZipFileEncrypter类里有两处(在两个add方法中)其它地方不需改动。
来源:https://blog.csdn.net/zhyh1986/article/details/7724229


猜你喜欢
- 一、Druid简介Druid是阿里开源的数据库连接池,作为后起之秀,性能比dbcp、c3p0更高,使用也越来越广泛。当然Druid不仅仅是一
- 上一节是把大概的流程给过了一遍,但是还有很多地方没有说到,后续的慢慢会涉及到,敬请期待!这次我们说说垃圾收集器,又名gc,顾名思义,就是收集
- 目前Android在全世界市场上大约有75%的占有率,国人Android手机的持有比例更甚,甚至达到90%以上。因此搞计算机的一听说手机应用
- 查看最新消息要用到类似新浪微博下拉刷新 功能!把最新的消息显示在最上面! 代码如下: PullToRefreshListView类代码 pa
- 前言上一篇我们认识了Kotlin编程语言,也搭建好开发环境。本篇就进入Kotlin的基础语法介绍,与其他编程语言一样,Kotlin也有自己的
- 兄dei,耐心把我的写的看完,我写的不繁琐,很好理解.IDEA插件之Mybatis Log plugin
- 目前较常用的分页实现办法有两种:1.每次翻页都修改SQL,向SQL传入相关参数去数据库实时查出该页的数据并显示。2.查出数据库某张表的全部数
- 前言在SpringBoot中,对于JavaBean的属性一般都绑定在配置文件中,比如application.properties/appli
- 递归是一个非常有用的知识点。写点实例帮助自己记忆中间有过程代码首先一个javapojo类package com.qcf.po;import
- 在使用spring框架中我们都知道,某个类如果使用了@Service、@Autowire 这种依赖注入的方式引用了其他对象,在另外一个类中,
- 前言在前面的文章中其实大家也已经看到我使用过collect(Collectors.toList()) 将数据最后汇总成一个 List 集合。
- C#事件标准命名规则一些开源代码的事件命名很混乱,以此文章用作本人以后工作的参考。事件的名称事件始终是指某个操作,这个操作可能正在发生,也可
- 一、MyBatis的逆向⼯程(1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,SqlMapper.xml⽂件,以及Mappe
- Android中的Service和其调用者既可以在同一个App中,也可以在不同的App。如果Service在App1中,而调用Service
- zenDiscovery实现机制ping是集群发现的基本手段,通过在网络上广播或者指定ping某些节点获取集群信息,从而可以找到
- 比如要获取打开摄像头的应用程序名称,只需要在frameworks/base/core/android/hardware/Camera.jav
- Redis模糊匹配批量删除操作,使用RedisTemplate操作: public void deleteByPrex(String pre
- 概述ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码。目标是能够对QR编码、Data Matrix、UPC的1D
- 1、锁优化在JDK6之前,通过synchronized来实现同步效率是很低的,被synchronized包裹的代码块经过javac编译后,会
- 如下所示:<?xml version="1.0" encoding="utf-8" ?>