java创建文件夹和遍历压缩文件夹或者文件

1:遍历压缩文件中的文件夹(包括空文件)和文件

package org.xqf.common.es;

/**
 * @Author xu
 * @create 2023/3/24 17
 */

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) {
    
    
    //创建临时temp文件
	//    File zipFile = File.createTempFile("example", ".zip");
        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!");
            }
//            zipFile.delete();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    private static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
    
    
        for (File file : folder.listFiles()) {
    
    
            //file.list().length 判断目录下面是否有文件或者目录,没有直接创建,有继续循环
            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;

/**
 * @Author xu
 * @create 2023/3/24 17
 */

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();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_19891197/article/details/129787391