Packing multiple files into compressed packages

In response to business needs, we need to pack the printed pdf into compression packages, and find many compression tools on the Internet, and finally find a suitable use. (multiple pdf packages)

Paste the code directly:
package com.iShift.kachaexport.util;

import java.util.ArrayList;

public class ZIPUtil {
    public static String createZipFile(ArrayList<String> fileList, String zipFileName) {
        //Building Compressed File
        File zipFile = new File(zipFileName);
        //Initialization of ZIP Stream
        ZipOutputStream out = null;

            //Building ZIP Stream Objects
            out = new ZipOutputStream(new FileOutputStream(zipFile));
            //Cyclic processing of incoming collections
            for(int i = 0; i < fileList.size(); i++){
                //Get the target file
                File inFile = new File(fileList.get(i));
                     //Define ZipEntry objects
                     ZipEntry entry = new ZipEntry(inFile.getName());
                     //Give ZIP Stream Object Properties
                     int len = 0 ;
                     byte[] buffer = new byte[1024];
                     //Building FileInputStream stream objects
                     FileInputStream fis;
                     fis = new FileInputStream(inFile);
                     while ((len = > 0) {
                         out.write(buffer, 0, len);
                     //Close closeEntry
                     //Close FileInputStream
        }catch (IOException e) {
             try {
                 //Finally close the ZIP stream
             } catch (IOException e) {

        return zipFileName;


Process description: divide several filenames into "pdf" and get the address of PDF respectively.
Add the address to the list set, compress it with the toolkit, and upload it to 7 bulls.
Return the download address and return to the front end.

Service Impl layer:

	public String exportStudentPdfZip(String[] us, int subjectId, String beginDate, String endDate, Integer practise) throws Exception {
		ArrayList<String> list = new ArrayList<>();
		String uuid = UUIDUtil.genUUID();
		String filePath = Constants.TEMP_FILE_PATH + "/" + uuid + ".zip";
		for (String userIdStr : us) {
			Long userId = DataTypeUtil.toLong(userIdStr);
			String path = productDateRangPdfA41(userId, subjectId, beginDate, endDate, practise);
		ZIPUtil.createZipFile(list, filePath);
		byte[] file2byte = file2byte(new File(filePath));
		storeToQiniu(file2byte, uuid + ".zip");
		return Constants.FILE_SERVER + uuid + ".zip";

Controller layer:

    public Response<?> exportStudentPdf(@RequestParam(value="userIds")String userIds, @RequestParam(value="beginDate")String beginDate, @RequestParam(value="endDate")String endDate
    										, @RequestParam(value="subjectId")int subjectId, @RequestParam(value="practise", required=false, defaultValue="1")Integer practise){
    	try {
    		String[] us = userIds.split(",");
    		ArrayList<String> list = new ArrayList<String>();
    		StudentExport studentExport = new StudentExport();
    		int fileType = 0;
    		String path = null;
    		if (us.length>1) {
    			fileType = 1;
    			path = wrongBookService.exportStudentPdfZip(us, subjectId, beginDate, endDate, practise);
    		} else {
    			Long userId = DataTypeUtil.toLong(userIds);
    			path = wrongBookService.productDateRangPdfA4(userId, subjectId, beginDate, endDate, practise);
    		studentExport.setDownloadLink(path + "?attname=");
			return Response.success("Success!", studentExport);
		} catch (Exception e) {
			logger.error("", e);
			return Response.error(e);

StudentExport class:

package com.iShift.kachaexport.entry.vo;

public class StudentExport {
	private int FileType; //1: Compressed Packet 0:pdf
	private String FileLink;
	private String DownloadLink;

	public int getFileType() {
		return FileType;

	public void setFileType(int fileType) {
		FileType = fileType;

	public String getFileLink() {
		return FileLink;

	public void setFileLink(String fileLink) {
		FileLink = fileLink;

	public String getDownloadLink() {
		return DownloadLink;

	public void setDownloadLink(String downloadLink) {
		DownloadLink = downloadLink;


Upload to 7 cattle:

public void storeToQiniu(byte[] bs, String relativePath) throws Exception {
		// Construct a configuration class with a specified Zone object
		Configuration cfg = new Configuration(Zone.zone2());
		// ... other parameter reference class annotations _____________
		UploadManager uploadManager = new UploadManager(cfg);
		// ... Generate upload credentials and prepare for upload
		// If the default key is not specified, the hash value of the file content is used as the file name.
		String key = relativePath;
		// byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
		ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bs);
		Auth auth = Auth.create(Constants.ACCESS_KEY, Constants.SECRET_KEY);
		String upToken = auth.uploadToken(Constants.QINIU_BUCKET);
		Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
		// Analysis of successful upload results
		DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		if (!relativePath.equals(putRet.key)) {
			throw new Exception("Error uploading file! Looking forward to the document:" + relativePath + " Return path:" + putRet.key);
	private static byte[] file2byte(File file) {  
        byte[] buffer = null;  
        try {  
            FileInputStream fis = new FileInputStream(file);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream();  
            byte[] b = new byte[1024];  
            int n;  
            while ((n = != -1) {  
                bos.write(b, 0, n);  
            buffer = bos.toByteArray();  
        } catch (FileNotFoundException e) {  
        } catch (IOException e)  {  
        return buffer;  
Ps: In novice study, record the process of multi-file conversion compression package. Baidu has been learning a lot for a long time.

Keywords: Java

Added by CapEsiouS on Sun, 06 Oct 2019 20:40:18 +0300