Java如何生成壓縮文件工具類
更新時間:2024年06月14日 16:10:48 作者:code_now
這篇文章主要介紹了Java如何生成壓縮文件工具類問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Java生成壓縮文件工具類
文件壓縮功能在日常項目中經常會使用到,例如文件太多,需要發(fā)送給用戶,這時就需要將多個文件壓縮成一個壓縮包,然后再通過郵件或其它方式發(fā)送給用戶;
在這里給大家提供一種生成zip文件壓縮工具類,并附帶測試代碼。
測試代碼目錄結構
文件壓縮核心工具類
主要有三個入參:
List fileList
:存放所有壓縮源文件的集合File zipFile
:壓縮后的文件Map<String, String> myMap
:key-文件壓縮前名稱,value-文件壓縮后在壓縮包中的名稱
package com.bbu.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 文件壓縮工具類 * * @author code_now */ public class ZipUtils { /** * 生成壓縮文件 * @param fileList 存放所有壓縮源文件 * @param zipFile 壓縮后文件 * @param myMap key-文件壓縮前名稱,value-文件壓縮后在壓縮包中的名稱 * @throws Exception */ public static void createFileZip(List<File> fileList, File zipFile, Map<String, String> myMap) throws Exception{ if(fileList.size()>0){ byte[] buf = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); for(File file:fileList){ FileInputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry((String) myMap.get(file.getName()))); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); file.delete();// 寫進壓縮文件后,刪除臨時目錄中的源文件 } out.close(); } catch (IOException e) { throw new Exception("文件壓縮失?。? + e.getMessage()); } } } }
測試代碼
package com.bbu.test; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; import com.bbu.utils.ZipUtils; public class TestCreateZip { Log logger = LogFactory.getLog(getClass()); @Test public void createZipTest() { // 源文件存儲路徑 String srcPath = this.getClass().getClassLoader().getResource("").getPath(); // 壓縮包存儲路徑 String desPath = "D:/zipfile/"; // 用于臨時存放所有的壓縮文件 List<File> fileList = new ArrayList<File>(); // key-文件壓縮前名稱,value-文件壓縮后在壓縮包中的名稱 Map<String,String> myMpa = new HashMap<String,String>(); // 放入文件test.pdf String testPdf = srcPath + "test.pdf"; File testPdfFile =new File(testPdf); myMpa.put(testPdfFile.getName(), "newName.pdf"); fileList.add(testPdfFile); // 放入文件test.docx String testDocx = srcPath + "test.docx"; File testDocxFile =new File(testDocx); myMpa.put(testDocxFile.getName(), "newName.docx"); fileList.add(testDocxFile); // 生成壓縮包zip文件 File zipFile = new File(desPath + File.separator+System.currentTimeMillis()+".zip"); logger.info(zipFile);// 打印壓縮包文件全路徑 try { // 調用壓縮工具進行壓縮 ZipUtils.createFileZip(fileList, zipFile, myMpa); logger.info("壓縮成功!"); } catch (Exception e) { logger.info("壓縮失??!", e); } } }
測試結果
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用Spring的ApplicationEvent實現(xiàn)本地事件驅動的實現(xiàn)方法
本文介紹了如何使用Spring的ApplicationEvent實現(xiàn)本地事件驅動,通過自定義事件和監(jiān)聽器,實現(xiàn)模塊之間的松耦合,提升代碼的可維護性和擴展性。同時還介紹了異步事件和事件傳遞的相關知識2023-04-04springboot+HttpInvoke?實現(xiàn)RPC調用的方法
RPC框架大家或多或少都用過,出自于阿里系的就有dubbo,HSF,sofaRPC等,今天通過本文給大家介紹springboot+HttpInvoke?實現(xiàn)RPC調用的方法,感興趣的朋友一起看看吧2022-03-03