Java ZIP解压缩 - Mr.Ding

Java ZIP解压缩

开源代码 热门推荐 1316 / 2019-12-03 16:47:06

Java内置的压缩工具类不完善,对中文文件名或者压缩设置密码支持不好

有这方面需求的朋友,可尝试下一个开源的组件zip4j,功能描述的还是比较强大的

1. 从Zip文件创建,添加,提取,更新,删除文件
2. 读/写密码保护的Zip文件
3. 支持AES 128/256加密
4. 支持标准邮编加密
5. 支持Zip64格式
6. 支持存储(无压缩)和Deflate压缩方法
7. 从Split Zip文件创建或提取文件(例如:z01,z02,... zip)
8. 支持Unicode文件名
9. 进度监视器

这里提供个下载地址

下载组件    下载源码

也可以从Maven得到其它版本

使用例子:

import java.io.File;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;

/**
* @author mr.ding
* @date 2019/12/03
* @version 1.0
*/
public class ZipHelper {

public static void zip(String zipfile, String file) {
zip(zipfile, null, file);
}

public static void zip(String zipfile, String password, String file) {
if(file == null || file.trim().length() < 1) {
throw new RuntimeException("source file is not set.");
}
File sourceFile = new File(file);
if(!sourceFile.exists()) {
throw new RuntimeException("source file is not exists: " + file);
}
if(!sourceFile.canRead()) {
throw new RuntimeException("source file cann't readable: " + file);
}

if(zipfile == null || zipfile.trim().length() < 1) {
throw new RuntimeException("zip file is not set.");
}
File zfile = new File(zipfile).getAbsoluteFile();
if(!zfile.exists()) {
File zdir = zfile.getParentFile();
if(zdir == null) {
throw new RuntimeException("zip file cann't be create: " + zipfile);
}
if(!zdir.exists()) {
if(!zdir.mkdirs()) {
throw new RuntimeException("zip file folder create failed: " + zdir);
}
}
if(!zdir.canWrite()) {
throw new RuntimeException("zip file dir cann't writeable.");
}
}
else if(!zfile.canWrite()) {
throw new RuntimeException("zip file cann't writeable: " + zipfile);
}

ZipFile zipFile = new ZipFile(zfile);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
parameters.setCompressionLevel(CompressionLevel.NORMAL);

if(password != null && password.length() > 0) {
zipFile.setPassword(password.toCharArray());
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(EncryptionMethod.AES);
parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
}

if(sourceFile.isFile()) {
try {
zipFile.addFile(sourceFile, parameters);
} catch (ZipException e) {
throw new RuntimeException(e);
}
}
else {
File[] files = sourceFile.listFiles();
try {
for(File f : files) {
if (f.isDirectory()) {
zipFile.addFolder(f, parameters);
} else {
zipFile.addFile(f, parameters);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

public static void unzip(String zipfile, String password, String destdir) {
if(zipfile == null || zipfile.trim().length() < 1) {
throw new RuntimeException("zip file is not set.");
}
File zfile = new File(zipfile);
if(!zfile.exists()) {
throw new RuntimeException("zip file is not found: " + zipfile);
}
if(!zfile.canRead()) {
throw new RuntimeException("zip file cann't readable: " + zipfile);
}
File ddir = null;
if(destdir == null) {
File dir = zfile.getAbsoluteFile().getParentFile();
if(dir == null) {
throw new RuntimeException("dest dir cann't be write");
}
String filename = zfile.getName();
int pos = filename.lastIndexOf(".");
if(pos != -1) {
dir = new File(dir, filename.substring(0, pos));
}
ddir = dir;
}
else {
ddir = new File(destdir.trim()).getAbsoluteFile();
}
if(!ddir.exists()) {
if(!ddir.mkdirs()) {
throw new RuntimeException("dest dir create failed: " + ddir);
}
}
else if(!ddir.canWrite()) {
throw new RuntimeException("dest dir cann't be writeable: " + ddir);
}
destdir = ddir.getAbsolutePath();
ZipFile zFile = new ZipFile(zfile);
try {
if(zFile.isEncrypted()) {
if(password == null || password.length() < 1) {
throw new RuntimeException("zip file is enctyped: " + zipfile);
}
zFile.setPassword(password.toCharArray());
}
zFile.extractAll(destdir);
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void remove(String zipfile, String password, String ...paths) {
if(paths == null || paths.length < 1) {
return ;
}
if(zipfile == null || zipfile.trim().length() < 1) {
throw new RuntimeException("zip file is not set.");
}
File zfile = new File(zipfile);
if(!zfile.exists()) {
throw new RuntimeException("zip file is not found: " + zipfile);
}
if(!zfile.canRead()) {
throw new RuntimeException("zip file cann't readable: " + zipfile);
}

ZipFile zFile = new ZipFile(zfile);
try {
if(zFile.isEncrypted()) {
if(password == null || password.length() < 1) {
throw new RuntimeException("zip file is enctyped: " + zipfile);
}
zFile.setPassword(password.toCharArray());
}
for(String path : paths) {
zFile.removeFile(path);
System.out.println("Delete: " + path);
}
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}

public static void main(String[] args) {
//zip("/Users/mr.ding/Desktop/bj.zip", "/Users/mr.ding/Desktop/bj-normal");
//zip("/Users/mr.ding/Desktop/bj-pwd.zip", "123", "/Users/mr.ding/Desktop/bj");
//unzip("/Users/mr.ding/Desktop/bj.zip", null, "/Users/mr.ding/Desktop/bj-normal");
//unzip("/Users/mr.ding/Desktop/bj-pwd.zip", "123", "/Users/mr.ding/Desktop/bj-pwd1");
remove("/Users/mr.ding/Desktop/bj.zip", null, "拍照的副本/");
}
}


注意:由于代码较长,所以没有写注释,但代码结构应该足够清晰看明白的。






上一篇:北京2019冬天第一场雪

下一篇:没有了