一、普通方式
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时传入的zipName
File 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:文章来源:https://www.toymoban.com/news/detail-508980.html
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.8.0</version>
</dependency>
代码块:文章来源地址https://www.toymoban.com/news/detail-508980.html
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;
@Slf4j
public class ZipUtils {
/**
*
* @param files 需要压缩的文件
* @param zipName 文件名称
* @param pwd 密码
* @return
*/
public static void createZip(List<String> 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<File> 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 inputFile D:\\test 要打包的文件夹
* @param outputFile D:\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();
}
}
}
到了这里,关于java 文件压缩zip【两种方式】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!