Java將多張圖片合並打包成zip格式進行下載


需求:某個超市有n個商品,現需要將超市中的商品圖片按如下方式進行打包下載

打開商品列表的時候會出現不同的分區,按照分區將圖片進行打包下載,下載的文件格式為【洗漱日化區.zip】,打開xxx區,里面的內容為【洗衣粉.zip,肥皂.zip,牙膏.zip】,打開任意一個,內容為【圖片1.jpg,圖片2.jpg

 

實現思路:

1. 創建最外層文件夾【洗漱日化區.file】用於存放【洗衣粉.zip,肥皂.zip,牙膏.zip】

2. 創建轉換【洗衣粉.zip,肥皂.zip,牙膏.zip】

3. 通過I/O流將圖片寫入2中

4. 將最外層文件夾【洗漱日化區.file】轉換為zip格式

 

 

/**
 * @ProjectName: test
 * @Package: com.test
 * @ClassName: TestMain
 * @Author: ***
 * @Description:
 * @Date: 2021/1/4 14:50
 * @Version: 1.0
 */
public class TestMain {

    public static void main(String[] args) {
        //創建父級目錄 用於存放分區(分區中存放多個分類)
        String fatherPath = "D:\\洗漱日化區";
        File fatherFile = new File(fatherPath);
        try {
            if (!fatherFile.exists()) {
                fatherFile.mkdir();
            } else {
                fatherFile.delete();
                fatherFile.mkdir();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //創建分類list 存放分類
        List<String> itemList = new ArrayList<>();
        itemList.add("洗衣粉");
        itemList.add("肥皂");
        itemList.add("牙膏");

        //創建list 存放圖片
        List<File> fileList = new ArrayList<>();
        fileList.add(new File("圖片1.jpg"));
        fileList.add(new File("圖片2.jpg"));

        for (String item : itemList) {
            //遍歷存儲圖片地址
            String url = fatherPath + "/" + item + ".zip";
            File zipFile = new File(url);
            // 調用壓縮方法
            ZipMultiFileUtil.zipFiles(fileList.stream().toArray(File[]::new), zipFile);
        }

        //將項目名稱的文件夾 壓縮為zip
        String fileDir ="D:\\洗漱日化區";
        ZipMultiFileUtil.fileToZip(fatherPath, fileDir, fatherPath + ".zip");
    }

}

 

package com.kexin.locatorService.utils;

import com.kexin.locatorService.pojo.Assets;
import org.apache.commons.lang.StringUtils;

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

/**
 * @ProjectName: iot
 * @Package: com.kexin.locatorService.utils
 * @ClassName: ZipMultiFileUtil
 * @Author: ***
 * @Description:
 * @Date: 2020/12/29 16:17
 * @Version: 1.0
 */
public class ZipMultiFileUtil {

    public static void zipFiles(File[] srcFiles, File zipFile) {
        try {
            if (srcFiles.length != 0) {
                // 判斷壓縮后的文件存在不,不存在則創建
                if (!zipFile.exists()) {
                    zipFile.createNewFile();
                } else {
                    zipFile.delete();
                    zipFile.createNewFile();
                }

                // 創建 FileInputStream 對象
                FileInputStream fileInputStream = null;
                // 實例化 FileOutputStream 對象
                FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
                // 實例化 ZipOutputStream 對象
                ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
                // 創建 ZipEntry 對象
                ZipEntry zipEntry = null;
                // 遍歷源文件數組
                for (int i = 0; i < srcFiles.length; i++) {
                    // 將源文件數組中的當前文件讀入 FileInputStream 流中
                    fileInputStream = new FileInputStream(srcFiles[i]);
                    // 實例化 ZipEntry 對象,源文件數組中的當前文件
                    zipEntry = new ZipEntry(srcFiles[i].getName());
                    zipOutputStream.putNextEntry(zipEntry);
                    // 該變量記錄每次真正讀的字節個數
                    int len;
                    // 定義每次讀取的字節數組
                    byte[] buffer = new byte[1024];
                    while ((len = fileInputStream.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, len);
                    }
                }
                zipOutputStream.closeEntry();
                zipOutputStream.close();
                fileInputStream.close();
                fileOutputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
     *
     * @param sourceFilePath :待壓縮的文件路徑
     * @param zipFilePath    :壓縮后存放路徑
     * @param fileName       :壓縮后文件的名稱
     * @return
     */
    public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
        boolean flag = false;
        File sourceFile = new File(sourceFilePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        if (sourceFile.exists() == false) {
            System.out.println("待壓縮的文件目錄:" + sourceFilePath + "不存在");
            sourceFile.mkdir(); // 新建目錄
        }
        try {
            File zipFile = new File(zipFilePath + "/" + fileName);
            if (zipFile.exists()) {
                System.out.println(zipFilePath + "目錄下存在名字為:" + fileName + ".zip" + "打包文件.");
            } else {
                File[] sourceFiles = sourceFile.listFiles();
                if (null == sourceFiles || sourceFiles.length < 1) {
                    System.out.println("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮.");
                } else {
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    byte[] bufs = new byte[1024 * 10];
                    for (int i = 0; i < sourceFiles.length; i++) {
                        //創建ZIP實體,並添加進壓縮包
                        ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                        zos.putNextEntry(zipEntry);
                        //讀取待壓縮的文件並寫進壓縮包里
                        fis = new FileInputStream(sourceFiles[i]);
                        bis = new BufferedInputStream(fis, 1024 * 10);
                        int read = 0;
                        while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                            zos.write(bufs, 0, read);
                        }
                    }
                    flag = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //關閉流
            try {
                if (null != bis) bis.close();
                if (null != zos) zos.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return flag;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM