Java对文件夹压缩并下载

在日常开发中,我们经常需要将一些文件夹进行压缩并下载到本地。Java提供了一种便捷的方式来实现这个功能。本文将介绍如何使用Java对文件夹进行压缩并下载,同时提供相应的代码示例。

压缩文件夹

要对文件夹进行压缩,我们可以使用Java中的ZipOutputStream类。这个类可以用来将文件夹中的内容逐个添加到压缩文件中。

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

public class ZipFolder {

    public static void zipFolder(String sourceFolder, String zipFile) throws IOException {
        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);

        addFolderToZip(sourceFolder, sourceFolder, zos);

        zos.close();
        fos.close();
    }

    private static void addFolderToZip(String folder, String baseFolder, ZipOutputStream zos) throws IOException {
        File file = new File(folder);
        if (file.isDirectory()) {
            for (String fileName : file.list()) {
                addFolderToZip(folder + File.separator + fileName, baseFolder, zos);
            }
        } else {
            FileInputStream fis = new FileInputStream(folder);
            String entryName = folder.substring(baseFolder.length() + 1);
            ZipEntry zipEntry = new ZipEntry(entryName);
            zos.putNextEntry(zipEntry);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            zos.closeEntry();
            fis.close();
        }
    }
}

上面的代码定义了一个ZipFolder类,其中包含了zipFolder方法和addFolderToZip方法。zipFolder方法用来压缩整个文件夹,addFolderToZip方法用来逐个添加文件到压缩文件中。

下载压缩文件

一旦文件夹被成功压缩,我们就可以将压缩文件下载到本地。这里我们需要使用HttpServletResponse对象将文件流写入到响应中,以实现下载功能。

import javax.servlet.http.HttpServletResponse;

public class DownloadFile {

    public static void downloadFile(HttpServletResponse response, String zipFilePath, String zipFileName) throws IOException {
        File file = new File(zipFilePath);
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);
        response.setContentLength((int) file.length());

        FileInputStream fis = new FileInputStream(file);
        OutputStream os = response.getOutputStream();

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }

        os.flush();
        os.close();
        fis.close();
    }
}

上面的代码定义了一个DownloadFile类,其中包含了downloadFile方法用来下载压缩文件。在该方法中,我们设置了响应的ContentType和Content-Disposition头,然后将文件流写入到响应输出流中。

示例

下面是一个简单的示例,演示了如何对文件夹进行压缩并下载。

public class Main {

    public static void main(String[] args) {
        String sourceFolder = "path/to/source/folder";
        String zipFile = "path/to/zip/file.zip";
        String zipFileName = "file.zip";

        try {
            ZipFolder.zipFolder(sourceFolder, zipFile);
            DownloadFile.downloadFile(response, zipFile, zipFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

总结

通过本文的介绍,我们学习了如何使用Java对文件夹进行压缩并下载到本地。首先,我们利用ZipOutputStream类对文件夹进行压缩,然后使用HttpServletResponse对象将压缩文件写入到响应中。这种方法既简单又高效,非常适用于日常开发中对文件夹进行操作的需求。希望本文对您有所帮助,谢谢阅读!

pie
    title 文件夹压缩和下载功能
    "压缩文件夹" : 70
    "下载压缩文件" : 30

通过本文的介绍,相信读者已经了解了如何使用Java对文件夹进行压缩并下载的方法。希望读者能够在