国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java多文件以ZIP壓縮包導出的實現方法

2024-07-14 08:41:35
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Java多文件以ZIP壓縮包導出的具體代碼,供大家參考,具體內容如下

1、使用java實現吧服務器的圖片打包成一個zip格式的壓縮包導出,多個文件打包導出。 
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("獲取壓縮的數據項失敗! 數據項名為:" + zipEntryName);       // 壓縮條目不是具體獨立的文件,而是壓縮包文件列表中的列表項,稱為條目,就像索引一樣       ZipEntry zipEntry = new ZipEntry("圖片/"+zipEntryName);      // 定位到該壓縮條目位置,開始寫入文件到壓縮包中       zipStream.putNextEntry(zipEntry);      if (compress) {         bytes = ImageByteUtil.compressOfQuality(input, 0);         zipStream.write(bytes, 0, bytes.length);       } else {        bytes = new byte[1024 * 5];// 讀寫緩沖區         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){  //要導出的文件集合,添加自己需要導出的文件  List<File> ListFiles = new ArrayList<>();  //調用工具類,參數說明(需要導出的文件集,ByteArrayOutputStream對象,編碼,是否壓縮【true,false】,文件名稱前綴)  ImageByteUtil.compressZip(ListFiles, out, "UTF-8", false,"LWJ");  //指定導出格式  ReturnContext.addParam("exportFileName","extFile.zip");  ReturnContext.addParam("mimeType", "zip");  return in;}

3、此功能是根據在開發過程中項目需要實現的,測試可正常使用,可更改定制。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 濮阳县| 泾川县| 宁武县| 兴安县| 高密市| 三都| 莫力| 子洲县| 林州市| 保德县| 十堰市| 榆林市| 托克托县| 榆树市| 万宁市| 长顺县| 互助| 陈巴尔虎旗| 肇源县| 滨州市| 开远市| 绥德县| 永昌县| 札达县| 裕民县| 板桥市| 班戈县| 新建县| 额济纳旗| 长春市| 武安市| 马关县| 海兴县| 武宣县| 措美县| 志丹县| 泸定县| 达日县| 新宾| 郯城县| 平泉县|