1:遍历压缩文件中的文件夹(包括空文件)和文件
package org.xqf.common.es;
import lombok.SneakyThrows;
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 ZipFolderExampleTest2 {
@SneakyThrows
public static void main(String[] args) {
List<String> sourceFolderList=new ArrayList<>();
sourceFolderList.add("aa");
sourceFolderList.add("bb");
String sourceFolder = "C:\\Users\\dell\\Desktop\\folder\\";
String zipFilePath = "C:\\Users\\dell\\Desktop\\folder.zip";
try {
FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
File sourceFile = new File(sourceFolder);
sourceFile.mkdirs();
if (sourceFile.exists()) {
for (String floder : sourceFolderList) {
File file = new File(sourceFolder + floder);
file.mkdir();
System.out.println(file.getName());
}
addFolderToZip(sourceFile, sourceFile.getName(), zos);
zos.finish();
zos.close();
fos.close();
System.out.println("Folder has been compressed successfully!");
} else {
System.out.println("Source folder does not exist!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
for (File file : folder.listFiles()) {
if (file.isDirectory() && file.list().length > 0) {
addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
continue;
}
if (!file.isDirectory()) {
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
fis.close();
} else {
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName() + "/"));
}
zos.closeEntry();
}
}
}
2:如果需要过滤空文件夹,修改方法addFolderToZip
rivate static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
continue;
}
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
fis.close();
} else {
zos.closeEntry();
}
}
3:创建空文件夹并压缩
在这里插入代码片package org.jeecg.common.es;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFolderExampleTest3 {
public static void main(String[] args) throws IOException {
List<String> folders = new ArrayList<>();
folders.add("folder1");
folders.add("folder2");
folders.add("folder3");
for (String folderName : folders) {
File folder = new File(folderName);
if (!folder.exists()) {
folder.mkdir();
}
}
Path zipFilePath = Paths.get("folders.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFilePath));
for (String folderName : folders) {
File folder = new File(folderName);
if (folder.exists()) {
String folderPath = folder.getPath();
if (folderPath.endsWith("/")) {
folderPath = folderPath.substring(0, folderPath.length() - 1);
}
ZipEntry zipEntry = new ZipEntry(folderPath.substring(folderPath.lastIndexOf("/") + 1) + "/");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.closeEntry();
}
folder.delete();
}
zipOutputStream.close();
}
}