SoFunction
Updated on 2025-04-21

Springboot multi-file compression implementation process

springboot multi-file compression

The project requires that all attachments be compressed and the steps are recorded.

Environmental dependency

When I was using jdk8, it basically included all available packages, but if it is a special requirement, it is necessary to import it yourself.

Implementation steps

Just paste the code, I tested it locally, so the file paths are all local paths, remember to modify it

	// Multiple files are compressed into zip    // Multiple files are compressed into zip and downloaded   public void downloadMoreFile(HttpServletResponse response) {

        String fileName1= "XXXXX\\Desktop\\2022.1fp\\";
        String fileName2= "XXXXX\\Desktop\\2022.1fp\\";
        // Create file object        File aFile= new File(fileName1);
        File bFile= new File(fileName2);
        // Build a file collection to store all files to be compressed        List<File> files = new ArrayList<>();
        (aFile);
        (bFile);
        // Determine the existence of the file. If the file does not exist, it cannot be compressed.        if (() && ()) {
            // Absolute path of the compressed package            String zipTmp = "XXXXXX\\Desktop\\2022.1fp\\";
            // Compression            zipd(zipTmp,files,response);
        }
    }
  • zipd implementation content
/**
      * Compression
      * @param zipTmp compressed package absolute path
      * @param files Collection of files to be compressed
      * @param response http request represents the object of the response
      */
    public void zipd(String zipTmp,List<File> files,HttpServletResponse response){
        // Create a compressed package file object        File zipTmpFile = new File(zipTmp);
        try {
// If the compressed package file already exists, delete the original file            if (()) {
                ();
            }
// No new compressed file exists            ();
// Remove the blank lines in the jsp compiled html header.            ();
// FileOutputStream byte output stream superclass File output stream is used to write data to the output stream File or a FileDescriptor            // Create file output stream            FileOutputStream fous = new FileOutputStream(zipTmpFile);
// ZipOutputStream compression stream// This stream is used to write files in ZIP file format, including support for compressed and uncompressed entries, that is, package files into compressed files, which are often used for attachment downloads (multi-file downloads) and file compression storage.            ZipOutputStream zipOut = new ZipOutputStream(fous);
            // Looping the file to be compressed            int size = ();
            for (int i = 0; i < size; i++) {
                File file = (i);
                // Write files to compressed stream                zipFile(file, zipOut);
            }
            // Close the stream            ();
            ();
//            downloadZip(zipTmpFile, response);
        } catch (IOException e) {
            ();
        }
    }
  • zipFile implementation content
   /**
      *
      * @param inputFile file to be compressed
      * @param ouputStream compressed stream
      */
    public void zipFile(File inputFile, ZipOutputStream ouputStream) {
        try {
            if (()) {
                if (()) {
                    // File input stream to be compressed                    FileInputStream IN = new FileInputStream(inputFile);
// Create a BufferedInputStream with a specified buffer size, save its parameters, enter stream in for later use.                    BufferedInputStream bins = new BufferedInputStream(IN, 512);
// ZipEnter: Indicates the entry of the compressed file (file directory)                    ZipEntry entry = new ZipEntry(());
                    // Create an entry file in the zip stream                    (entry);

                    int nNumber;
                    byte[] buffer = new byte[512];
                    while ((nNumber = (buffer)) != -1) {
                        (buffer, 0, nNumber);
                    }
                    // Close the stream                    ();
                    ();
                } else {
                    // Handle folder situation                    try {
                        File[] files = ();
                        for (int i = 0; i < ; i++) {
                            zipFile(files[i], ouputStream);
                        }
                    } catch (Exception e) {
                        ();
                    }
                }
            }
        } catch (Exception e) {
            ();
        }
    }

If you need to download, remember to add the following method

public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
        if (() == false) {
            ("The file directory to be compressed:" + file + "Not exists.");
        } else {
            try {
                // Download the file in stream form.                InputStream fis = new BufferedInputStream(new FileInputStream(()));
                byte[] buffer = new byte[()];
                (buffer);
                ();
                // Clear response                ();

                OutputStream toClient = new BufferedOutputStream(());
                ("application/octet-stream");

                // If the output is a file with a Chinese name, you need to use the method to process it here                ("Content-Disposition",
                        "attachment;filename=" + new String(().getBytes("GB2312"), "ISO8859-1"));
                (buffer);
                ();
                ();
            } catch (Exception ex) {
                ();
            } finally {
                try {
                    File f = new File(());
                    ();
                } catch (Exception e) {
                    ();
                }
            }
        }
        return response;
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.