实现Java批量导出并压缩zip教程

关系图

erDiagram
    程序员 ||--o 初学者 : 教学

整体流程

整个过程可以分为以下几个步骤:

  1. 遍历文件夹获取所有文件路径
  2. 创建压缩文件
  3. 将文件逐个写入压缩文件
  4. 关闭压缩文件输出流

详细步骤

  1. 遍历文件夹获取所有文件路径
// 遍历文件夹获取文件路径
public List<String> getAllFilePaths(String folderPath) {
    File folder = new File(folderPath);
    List<String> filePaths = new ArrayList<>();
    if (folder.isDirectory()) {
        for (File file : folder.listFiles()) {
            if (file.isFile()) {
                filePaths.add(file.getAbsolutePath());
            }
        }
    }
    return filePaths;
}
  1. 创建压缩文件
// 创建压缩文件
public void createZipFile(String zipFilePath) {
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilePath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  1. 将文件逐个写入压缩文件
// 将文件写入压缩文件
public void writeFileToZip(String filePath, ZipOutputStream out) {
    try {
        File file = new File(filePath);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(file.getName());
        out.putNextEntry(entry);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  1. 关闭压缩文件输出流
// 关闭压缩文件输出流
public void closeZipFile(ZipOutputStream out) {
    try {
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

整体代码示例

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    // 遍历文件夹获取文件路径
    public List<String> getAllFilePaths(String folderPath) {
        File folder = new File(folderPath);
        List<String> filePaths = new ArrayList<>();
        if (folder.isDirectory()) {
            for (File file : folder.listFiles()) {
                if (file.isFile()) {
                    filePaths.add(file.getAbsolutePath());
                }
            }
        }
        return filePaths;
    }

    // 创建压缩文件
    public void createZipFile(String zipFilePath) {
        try {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 将文件写入压缩文件
    public void writeFileToZip(String filePath, ZipOutputStream out) {
        try {
            File file = new File(filePath);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(file.getName());
            out.putNextEntry(entry);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 关闭压缩文件输出流
    public void closeZipFile(ZipOutputStream out) {
        try {
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

总结

通过以上步骤和代码示例,你可以实现Java批量导出并压缩zip的功能了。希望对你有所帮助,加油!