一、普通方式

package com.hebta.source.code.util;import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipUtils {/** * @param files 传入需要压缩的文件数组 例如 ["/woke/file/1.jpg", "/woke/file/2.jpg"...] * @return */public String createZip(String[] files) {String zipFileName = "zipName.zip";// zipName为上一步文件打包zip时传入的zipNameFile zipFile = new File("/woke/file/" + zipFileName);if (!zipFile.getParentFile().exists()) {zipFile.getParentFile().mkdirs();}if (!zipFile.exists()) {try {zipFile.createNewFile();} catch (IOException e) {e.printStackTrace();}}zip(files, zipFile);return zipFileName;}/** * * @param files 需要压缩的文件 例如 ["/woke/file/1.jpg", "/woke/file/2.jpg"...] * @param zipFile 压缩后输出的文件 */public void zip(String[] files, File zipFile) {FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;ZipOutputStream zipOutputStream = null;BufferedInputStream bufferInputStream = null;try {// zipFileName为压缩文件的名称(xx.zip),首先在某个目录下(C:/temp/路径可以根据自己的需求进行修改)创建一个.zip结尾的文件fileOutputStream = new FileOutputStream(zipFile);zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));// 创建读写缓冲区byte[] bufs = new byte[1024 * 10];for (String file : files) {File voiceFile = new File(file);if (voiceFile.exists()) {ZipEntry zipEntry;// 压缩文件下的目录分级,如果不分目录可以直接用fileName.文件后缀zipEntry = new ZipEntry("path/folderName/fileName." + getFileExtension(voiceFile));// 创建ZIP实体,并添加进压缩包zipOutputStream.putNextEntry(zipEntry);// 读取待压缩的文件并写进压缩包里fileInputStream = new FileInputStream(voiceFile);bufferInputStream = new BufferedInputStream(fileInputStream, 1024 * 10);int read = 0;while ((read = bufferInputStream.read(bufs, 0, 1024 * 10)) != -1) {zipOutputStream.write(bufs, 0, read);}}}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferInputStream != null) {bufferInputStream.close();}if (zipOutputStream != null) {zipOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}}/** * 获取文件后缀名 * @param file * @return */private static String getFileExtension(File file) {String fileName = file.getName();if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)return fileName.substring(fileName.lastIndexOf(".") + 1);else return " ";}}

二、使用zip4j方式

引入jar:

net.lingala.zip4jzip4j2.8.0

代码块:

package com.hebta.source.code.util;import lombok.extern.slf4j.Slf4j;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.EncryptionMethod;import java.io.File;import java.util.ArrayList;import java.util.List;@Slf4jpublic class ZipUtils {/** * * @param files 需要压缩的文件 * @param zipName 文件名称 * @param pwd 密码 * @return */public static void createZip(List files, String zipName, String pwd) {ZipParameters zipParameters = new ZipParameters();zipParameters.setEncryptFiles(true);zipParameters.setEncryptionMethod(EncryptionMethod.AES);// Below line is optional. AES 256 is used by default. You can override it to use AES 128. AES 192 is supported only for extracting.zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);List filesToAdd = new ArrayList();for (String file : files) {filesToAdd.add(new File(file));}//将文件夹添加到压缩文件//new ZipFile("filename.zip").addFolder(new File("/users/some_user/folder_to_add"));ZipFile zipFile = new ZipFile(zipName, pwd.toCharArray());try {zipFile.addFiles(filesToAdd, zipParameters);} catch (ZipException e) {log.info("生成zip失败:" + e.getMessage());e.printStackTrace();}}/** * 压缩文件夹加密 * @param inputFileD:\\test要打包的文件夹 * @param outputFileD:\test1.zip 生成的压缩包的名字 * @param pwd压缩密码 */public static void zipFileWithPwd(String inputFile,String outputFile, String pwd) {// 生成的压缩文件try {ZipFile zipFile = new ZipFile(outputFile, pwd.toCharArray());ZipParameters zipParameters = new ZipParameters();zipParameters.setEncryptFiles(true);zipParameters.setEncryptionMethod(EncryptionMethod.AES);zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);// 要打包的文件夹File currentFile = new File(inputFile);File[] fs = currentFile.listFiles();// 遍历test文件夹下所有的文件、文件夹for (File f : fs) {if (f.isDirectory()) {zipFile.addFolder(f, zipParameters);} else {zipFile.addFile(f, zipParameters);}}} catch (ZipException e) {e.printStackTrace();}}}