引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version> <!-- 使用适当的版本号 -->
</dependency>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static Logger LOG = LoggerFactory.getLogger(ZipUtils.class);
public static void main(String[] args) {
String sourceFolderPath = "C:/aa"; // 输入文件夹路径
String zipFilePath = "D:/aa.zip"; // 输出的zip文件路径
zipFolderContents(sourceFolderPath, zipFilePath);
List<String> list = new ArrayList<>();
list.add("C:/aa/a01");
list.add("C:/aa/test.txt");
// zipFolderContents(list, zipFilePath);
System.out.println("Zip created successfully.");
}
/**
* 将指定的文件目录打成zip包
* @param sourceFolderPath
* @param zipFilePath
* @throws IOException
*/
public static boolean zipFolderContents(String sourceFolderPath, String zipFilePath) {
try {
FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
File sourceFolder = new File(sourceFolderPath);
compressFolderContents(sourceFolder, zos, "");
zos.close();
fos.close();
return true;
}catch (IOException e){
LOG.error(e.getMessage());
}
return false;
}
/**
* 数组里面可以是文件夹或文件的路径
* @param sourceFolderPaths
* @param zipFilePath
* @return
*/
public static boolean zipFolderContents(List<String> sourceFolderPaths, String zipFilePath) {
try {
FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String sourceFolderPath : sourceFolderPaths) {
File sourceFolder = new File(sourceFolderPath);
if(sourceFolder.isDirectory()){
compressFolderContents(sourceFolder, zos, sourceFolder.getName());
}else {
compressFolderContents(sourceFolder, zos, "");
}
}
zos.close();
fos.close();
return true;
}catch (IOException e){
LOG.error(e.getMessage());
}
return false;
}
private static void compressFolderContents(File folder, ZipOutputStream zos, String parentFolderName) throws IOException {
if(!folder.isDirectory()){
byte[] buffer = new byte[1024];
// 如果是文件,则将其压缩到zip包中
FileInputStream fis = new FileInputStream(folder);
zos.putNextEntry(new ZipEntry(parentFolderName + "/" + folder.getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}else {
File[] files = folder.listFiles();
byte[] buffer = new byte[1024];
for (File file : files) {
if (file.isDirectory()) {
// 如果是文件夹,则递归压缩其内容,注意更新父文件夹名称
String subFolderName = parentFolderName + "/" + file.getName() + "/";
compressFolderContents(file, zos, subFolderName);
} else {
// 如果是文件,则将其压缩到zip包中
FileInputStream fis = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(parentFolderName + "/" + file.getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
}
}
}
}