File Bulk Download

background
With the development of digital media, more and more media files are produced every day. Scenes involving file download become more and more common during the daily use of the system. This sets new requirements for the experience of file download. In order to improve the friendliness of file download function, it is necessary to provide file package download function.

major function
Packaging Compression for Multiple Files: Packaging Compression (7z, tar, zip format) for multiple files that need to be downloaded. You can also download compressed files.

Code Address
git@code.dayu.work:gts-compress/compress-parent.git
example
Sample Code
šŸ“Žcompress-sample.zip
setting.xml
šŸ“Žsettings.xml
Instructions
Introducing dependencies

<dependency>
		<groupId>com.aliyun.gts</groupId>
    <artifactId>compress-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Add Configuration

gts:
  compress:
    path:
      file-path: "D:\\test\\result\\"       # File Cache Path
      compress-path: "D:\\test\\result2\\"  # Compressed File Cache Path

If this configuration is not added to the configuration file, the default path will be used. The default paths are:
Path where jar is located\file\source#file cache path
Path where jar is located\file\compress-path#Compressed file cache path
The relevant adapter code is as follows

        ApplicationHome ah = new ApplicationHome(GtsCompressProperty.class);
        String docStorePath = ah.getSource().getParentFile().toString();

        logger.info("jar The path where the package is located:{}", docStorePath);
        
        if(StringUtils.isBlank(filePath)){
            this.filePath = docStorePath + "\\file\\source\\";
        }

        if(StringUtils.isBlank(compressPath)){
            this.compressPath = docStorePath + "\\file\\compress\\";
        }

        logger.info("File Cache Path:{}", this.filePath);
        logger.info("Compressed File Cache Path:{}", this.compressPath);

Interface introduction

public interface FileService {

    /**
     * Call method 1. Accept multiple input streams and return the output stream of the compressed file
     *
     * @param inputStreams List of File Input Streams
     * @param fileNames List of file names
     * @param targetName Target file name for bulk download
     * @param compressEnum File Packaging Compression Strategy
     */
    InputStream batchDownloadsByInputStream(List<InputStream> inputStreams, List<String> fileNames, String targetName,
                                            CompressEnum compressEnum)
            throws IOException, CompressExcetpion;


    /**
     * Call method 2, accept consumer function, return the output stream of compressed file
     *
     * @param consumer Callback method, in the implementation class of the interface, to download files to the file cache directory
     * @param targetName Target file name for bulk download
     * @param compressEnum File Packaging Compression Strategy
     */
    InputStream batchDownloadsByConsumer(Consumer<String> consumer, String targetName, CompressEnum compressEnum)
            throws IOException, CompressExcetpion;
}

Interface 1, by receiving multiple file streams, packages the file corresponding to the file into a compressed file, and returns the input stream corresponding to the compressed file.
Interface 2, by receiving consumer, callback consumer's accept() method, returns the path of the file cache to the parameters of accept() method. The caller can download the file to the corresponding path after consumer completes.
Call example
1. Return the output stream of a compressed file by accepting multiple input streams

@Autowired
private FileService fileService;

  /**
     * 1,Call Example 1 to generate compressed files from multiple file streams and complete the download function
     *
     * @param type type Can pass zip, tar, 7z
     * @param response
     *
     * @throws IOException
     * @throws CompressExcetpion
     */
    @GetMapping("/donwloadByInputStream")
    public void batchDownloadsByInputStream(@RequestParam("type") String type, HttpServletResponse response)
            throws IOException, CompressExcetpion {
        // Construct simulation parameters
        InputStream inputStream = new FileInputStream(new File("D:\\test\\file\\te\\1.xlsx"));
        InputStream inputStream2 = new FileInputStream(new File("D:\\test\\file\\te\\2.xlsx"));
        InputStream inputStream3 = new FileInputStream(new File("D:\\test\\file\\te\\3.xlsx"));

        List<InputStream> list1 = new ArrayList<>();
        list1.add(inputStream);
        list1.add(inputStream2);
        list1.add(inputStream3);

        List<String> list2 = new ArrayList<>();
        list2.add("1.xlsx");
        list2.add("2.xlsx");
        list2.add("3.xlsx");

        // Adapt the corresponding compressed file type based on the type parameter passed
        CompressEnum compressEnum = null;
        if ("tar".equals(type)) {
            compressEnum = CompressEnum.TAR;
        } else if ("7z".equals(type)) {
            compressEnum = CompressEnum.SEVEN_ZIP;
        } else if ("zip".equals(type)) {
            compressEnum = CompressEnum.ZIP;
        }

        // Name of file to simulate Download
        String fileName = "compressFile";

        // Make file compression calls
        InputStream resultInputStream = fileService.batchDownloadsByInputStream(list1, list2, fileName, compressEnum);

        fileName = fileName + compressEnum.getSuffix();

        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(resultInputStream, outputStream);

        outputStream.close();
        resultInputStream.close();
    }

Introduce FileService, call batchDownloadsByInputStream() method, corresponding compressed file stream will be returned.
2. Return the output stream of a compressed file by accepting a function

	@Autowired
	private FileService fileService;
   /**
     * 2,Call Example 2 to generate compressed files from multiple file streams and complete the download function
     *
     * @param type type Can pass zip, tar, 7z
     * @param ids List of file IDs to be packaged for download
     * @param response
     *
     * @throws IOException
     * @throws CompressExcetpion
     */
    @GetMapping("/donwloadByConsumer")
    public void batchDownloadsByConsumer(@RequestParam("type") String type, @RequestParam("ids") String ids,
                                         HttpServletResponse response)
            throws IOException, CompressExcetpion {

        // Adapt the corresponding compressed file type based on the type parameter passed
        CompressEnum compressEnum = null;
        if ("tar".equals(type)) {
            compressEnum = CompressEnum.TAR;
        } else if ("7z".equals(type)) {
            compressEnum = CompressEnum.SEVEN_ZIP;
        } else if ("zip".equals(type)) {
            compressEnum = CompressEnum.ZIP;
        }

        //Simulated File Name
        String fileName = "compressFile";


        List<String> idList = Splitter.on(",").trimResults().splitToList(ids);

        if (CollectionUtils.isEmpty(idList)) {
            return;
        }

        // Make file compression calls
        InputStream resultInputStream = fileService.batchDownloadsByConsumer((sourcePath) -> {
            /**
             * This simulation retrieves the file stream from the file id list and downloads the file to the sourcePath directory
             */
            idList.stream().forEach(id -> {
                // 1. Query the records of the current file based on id

                // 2. Get the current file stream from file information into oss

                // 3. Download the file to the sourcePath directory, and close the file output stream after completion, otherwise it will affect the cache cleanup function
								

            });
        }, fileName, compressEnum);

        fileName = fileName + compressEnum.getSuffix();

        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(resultInputStream, outputStream);

        outputStream.close();
        resultInputStream.close();
    }

Introduce FileService, call batchDownloadsByConsumer() method, corresponding compressed file stream will be returned.
Be careful
In the second way, a highly customizable operation can be done in the Consumer function by accepting a function that returns a call to the output stream of a compressed file. Files can be obtained not only from oss, but also from the network, disk, and so on.
Reference Links
Construction Plan for Batch File Download

Tags: Java

Posted on Sat, 18 Sep 2021 19:25:28 -0400 by dmphotography