Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了Java多文件以ZIP壓縮包導(dǎo)出的具體代碼,供大家參考,具體內(nèi)容如下
1、使用java實(shí)現(xiàn)吧服務(wù)器的圖片打包成一個(gè)zip格式的壓縮包導(dǎo)出,多個(gè)文件打包導(dǎo)出。
2、代碼如下:
**ImageByteUtil.java**
public class ImageByteUtil{ private static float QUALITY = 0.6f; public static void compressZip(List<File> listfiles, OutputStream output,String encode, boolean compress,String alias){ ZipOutputStream zipStream = null; try { zipStream = new ZipOutputStream(output); for (File file : listfiles){ compressZip(file, zipStream, compress,alias+"_"+(listfiles.indexOf(file)+1)); } } catch (Exception e) { e.printStackTrace(); }finally { try { if (zipStream != null) { zipStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void compressZip(File file, ZipOutputStream zipStream, boolean compress,String alias) throws Exception{ FileInputStream input = null; try { input = new FileInputStream(file); //zip(input, zipStream, file.getName(), compress); zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1), compress); } catch (Exception e) { e.printStackTrace(); }finally { try { if(input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void zip(InputStream input, ZipOutputStream zipStream, String zipEntryName, boolean compress) throws Exception{ byte[] bytes = null; BufferedInputStream bufferStream = null; try { if(input == null) throw new Exception("獲取壓縮的數(shù)據(jù)項(xiàng)失敗! 數(shù)據(jù)項(xiàng)名為:" + zipEntryName); // 壓縮條目不是具體獨(dú)立的文件,而是壓縮包文件列表中的列表項(xiàng),稱為條目,就像索引一樣 ZipEntry zipEntry = new ZipEntry("圖片/"+zipEntryName); // 定位到該壓縮條目位置,開(kāi)始寫入文件到壓縮包中 zipStream.putNextEntry(zipEntry); if (compress) { bytes = ImageByteUtil.compressOfQuality(input, 0); zipStream.write(bytes, 0, bytes.length); } else { bytes = new byte[1024 * 5];// 讀寫緩沖區(qū) bufferStream = new BufferedInputStream(input);// 輸入緩沖流 int read = 0; while ((read = bufferStream.read(bytes)) != -1) { zipStream.write(bytes, 0, read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != bufferStream) bufferStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public static byte[] compressOfQuality(File file, float quality) throws Exception{ byte[] bs = null; InputStream input = null; try { input = new FileInputStream(file); bs = compressOfQuality(input,quality); } catch (Exception e) { e.printStackTrace(); } finally { try { if (input != null) input.close(); } catch (IOException e) { e.printStackTrace(); } } return bs; } public static byte[] compressOfQuality(InputStream input, float quality) throws Exception { ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); if(quality == 0){ Thumbnails.of(input).scale(1f).outputQuality(QUALITY) .toOutputStream(output); } else { Thumbnails.of(input).scale(1f).outputQuality(quality).toOutputStream(output); } return output.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (output != null) output.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
**Main.java**
public static void main(String[] args){ //要導(dǎo)出的文件集合,添加自己需要導(dǎo)出的文件 List<File> ListFiles = new ArrayList<>(); //調(diào)用工具類,參數(shù)說(shuō)明(需要導(dǎo)出的文件集,ByteArrayOutputStream對(duì)象,編碼,是否壓縮【true,false】,文件名稱前綴) ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ"); //指定導(dǎo)出格式 ReturnContext.addParam("exportFileName","extFile.zip"); ReturnContext.addParam("mimeType", "zip"); return in; }
3、此功能是根據(jù)在開(kāi)發(fā)過(guò)程中項(xiàng)目需要實(shí)現(xiàn)的,測(cè)試可正常使用,可更改定制。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作
這篇文章主要介紹了java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08詳解Java?POI?excel自定義設(shè)置單元格格式
這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來(lái)源_formats,更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn),需要的朋友可以參考下2024-01-01解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問(wèn)題
這篇文章主要介紹了解決SpringBoot中MultipartResolver和ServletFileUpload的沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot的@Value注解如何設(shè)置默認(rèn)值
這篇文章主要介紹了SpringBoot的@Value注解如何設(shè)置默認(rèn)值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Java中ArrayList在foreach里remove的問(wèn)題詳析
這篇文章主要給大家介紹了關(guān)于Java中ArrayList在foreach里remove問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-09-09Idea創(chuàng)建多模塊maven聚合項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了Idea創(chuàng)建多模塊maven聚合項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12SpringMVC通過(guò)注解獲得參數(shù)的實(shí)例
下面小編就為大家?guī)?lái)一篇SpringMVC通過(guò)注解獲得參數(shù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08