最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8 Zip 壓縮與解壓縮的實現(xiàn)

 更新時間:2020年03月25日 09:32:55   作者:sp42a  
這篇文章主要介紹了Java8 Zip 壓縮與解壓縮的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

網(wǎng)上找過幾個例子都有點小問題,還是谷歌找出來的靠譜。主要是增加了指定文件的功能,通過 Java8 的 Lambda 判斷是否加入 ZIP 壓縮,比較方便。函數(shù)表達式的簽名是 Function<File, Boolean>,參數(shù)是待加入的 File 對象,返回值 true 表示允許,反之不行。

完整代碼在:https://gitee.com/sp42_admin/ajaxjs/blob/master/ajaxjs-base/src/main/java/com/ajaxjs/util/io/FileHelper.java

/**
 * Copyright sp42 frank@ajaxjs.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ajaxjs.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.ajaxjs.util.logger.LogHelper;

/**
 * ZIP 壓縮/解壓縮
 * 
 * @author sp42
 *
 */
public class ZipHelper {
 private static final LogHelper LOGGER = LogHelper.getLog(ZipHelper.class);

 /**
 * 解壓文件
 * 
 * @param save  解壓文件的路徑,必須為目錄
 * @param zipFile 輸入的解壓文件路徑,例如C:/temp/foo.zip或 c:\\temp\\bar.zip
 */
 public static void unzip(String save, String zipFile) {
 if (!new File(save).isDirectory())
  throw new IllegalArgumentException("保存的路徑必須為目錄路徑");

 long start = System.currentTimeMillis();
 File folder = new File(save);
 if (!folder.exists())
  folder.mkdirs();

 try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));) {
  ZipEntry ze;
  while ((ze = zis.getNextEntry()) != null) {
  File newFile = new File(save + File.separator + ze.getName());
  System.out.println("file unzip : " + newFile.getAbsoluteFile());

  // 大部分網(wǎng)絡(luò)上的源碼,這里沒有判斷子目錄
  if (ze.isDirectory()) {
   newFile.mkdirs();
  } else {
//   new File(newFile.getParent()).mkdirs();
   FileHelper.initFolder(newFile);
   FileOutputStream fos = new FileOutputStream(newFile);
   IoHelper.write(zis, fos, false);
   fos.close();
  }

//  ze = zis.getNextEntry();
  }
  zis.closeEntry();
 } catch (IOException e) {
  LOGGER.warning(e);
 }

 LOGGER.info("解壓縮完成,耗時:{0}ms,保存在{1}", System.currentTimeMillis() - start, save);
 }

 /**
 * 壓縮文件
 * 
 * @param toZip  要壓縮的目錄或文件
 * @param saveZip 壓縮后保存的 zip 文件名
 */
 public static void zip(String toZip, String saveZip) {
 zip(toZip, saveZip, null);
 }

 /**
 * 壓縮文件
 * 
 * @param toZip   要壓縮的目錄或文件
 * @param saveZip  壓縮后保存的 zip 文件名
 * @param everyFile 輸入 File,可在這 Lambda 里面判斷是否加入 ZIP 壓縮,返回 true 表示允許,反之不行
 */
 public static void zip(String toZip, String saveZip, Function<File, Boolean> everyFile) {
 long start = System.currentTimeMillis();
 File fileToZip = new File(toZip);

 FileHelper.initFolder(saveZip);

 try (FileOutputStream fos = new FileOutputStream(saveZip); ZipOutputStream zipOut = new ZipOutputStream(fos);) {
  zip(fileToZip, fileToZip.getName(), zipOut, everyFile);
 } catch (IOException e) {
  LOGGER.warning(e);
 }

 LOGGER.info("壓縮完成,耗時:{0}ms,保存在{1}", System.currentTimeMillis() - start, saveZip);
 }

 /**
 * 內(nèi)部的壓縮方法
 * 
 * @param toZip   要壓縮的目錄或文件
 * @param fileName ZIP 內(nèi)的文件名
 * @param zipOut  ZIP 流
 * @param everyFile 輸入 File,可在這 Lambda 里面判斷是否加入 ZIP 壓縮,返回 true 表示允許,反之不行
 */
 private static void zip(File toZip, String fileName, ZipOutputStream zipOut, Function<File, Boolean> everyFile) {
 if (toZip.isHidden())
  return;

 if (everyFile != null && !everyFile.apply(toZip)) {
  return; // 跳過不要的
 }

 try {
  if (toZip.isDirectory()) {
  zipOut.putNextEntry(new ZipEntry(fileName.endsWith("/") ? fileName : fileName + "/"));
  zipOut.closeEntry();

  File[] children = toZip.listFiles();
  for (File childFile : children) {
   zip(childFile, fileName + "/" + childFile.getName(), zipOut, everyFile);
  }

  return;
  }

  zipOut.putNextEntry(new ZipEntry(fileName));

  try (FileInputStream in = new FileInputStream(toZip);) {
  IoHelper.write(in, zipOut, false);
  }
 } catch (IOException e) {
  LOGGER.warning(e);
 }
 }
}

到此這篇關(guān)于Java8 Zip 壓縮與解壓縮的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java8 Zip 壓縮與解壓縮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成Swagger2構(gòu)建在線API文檔的代碼詳解

    SpringBoot集成Swagger2構(gòu)建在線API文檔的代碼詳解

    這篇文章主要介紹了SpringBoot集成Swagger2構(gòu)建在線API文檔,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Spring Boot搭建文件上傳服務(wù)的方法

    Spring Boot搭建文件上傳服務(wù)的方法

    這篇文章主要為大家詳細介紹了Spring Boot搭建文件上傳服務(wù)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Prometheus pushgateway的使用詳解

    Prometheus pushgateway的使用詳解

    為了防止 pushgateway 重啟或意外掛掉,導(dǎo)致數(shù)據(jù)丟失,我們可以通過 -persistence.file 和 -persistence.interval 參數(shù)將數(shù)據(jù)持久化下來,接下來通過本文給大家介紹下Prometheus pushgateway的使用,感興趣的朋友一起看看吧
    2021-11-11
  • Spring?Boot源碼實現(xiàn)StopWatch優(yōu)雅統(tǒng)計耗時

    Spring?Boot源碼實現(xiàn)StopWatch優(yōu)雅統(tǒng)計耗時

    這篇文章主要為大家介紹了Spring?Boot源碼實現(xiàn)StopWatch優(yōu)雅統(tǒng)計耗時,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼

    Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼

    這篇文章主要介紹了Flutter實現(xiàn)文本組件、圖標及按鈕組件的代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • Intellij IDEA Debug調(diào)試技巧(小結(jié))

    Intellij IDEA Debug調(diào)試技巧(小結(jié))

    這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Springboot如何配置多個Redis數(shù)據(jù)源(非集群)

    Springboot如何配置多個Redis數(shù)據(jù)源(非集群)

    這篇文章主要介紹了Springboot如何配置多個Redis數(shù)據(jù)源(非集群)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 詳解 Java中日期數(shù)據(jù)類型的處理之格式轉(zhuǎn)換的實例

    詳解 Java中日期數(shù)據(jù)類型的處理之格式轉(zhuǎn)換的實例

    這篇文章主要介紹了詳解 Java中日期數(shù)據(jù)類型的處理之格式轉(zhuǎn)換的實例的相關(guān)資料,日期以及時間格式處理,在Java中時間格式一般會涉及到的數(shù)據(jù)類型包括Calendar類和Date類,需要的朋友可以參考下
    2017-08-08
  • SpringBoot處理請求參數(shù)中包含特殊符號

    SpringBoot處理請求參數(shù)中包含特殊符號

    今天寫代碼遇到了一個問題,請求參數(shù)是個路徑“D:/ExcelFile”,本文就詳細的介紹一下該錯誤的解決方法,感興趣的可以了解一下
    2021-06-06
  • Java Arrays工具類用法詳解

    Java Arrays工具類用法詳解

    這篇文章主要介紹了Java Arrays工具類用法,結(jié)合實例形式分析了java Arrays工具類針對數(shù)組元素修改、復(fù)制、排序等操作使用技巧與相關(guān)注意事項,需要的朋友可以參考下
    2019-05-05

最新評論

陵川县| 兴海县| 罗江县| 沙田区| 凯里市| 灌南县| 北流市| 海兴县| 尼玛县| 茂名市| 平武县| 清水县| 磐石市| 万载县| 义马市| 凯里市| 绿春县| 西昌市| 寿阳县| 绥德县| 涿鹿县| 榆林市| 全南县| 永嘉县| 托克托县| 定陶县| 黑水县| 东平县| 丽水市| 鞍山市| 博乐市| 怀集县| 鲜城| 开江县| 德清县| 民乐县| 旌德县| 平南县| 江孜县| 外汇| 临邑县|