Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)
網(wǎng)上找過(guò)幾個(gè)例子都有點(diǎn)小問(wèn)題,還是谷歌找出來(lái)的靠譜。主要是增加了指定文件的功能,通過(guò) Java8 的 Lambda 判斷是否加入 ZIP 壓縮,比較方便。函數(shù)表達(dá)式的簽名是 Function<File, Boolean>,參數(shù)是待加入的 File 對(duì)象,返回值 true 表示允許,反之不行。
/** * 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ò)上的源碼,這里沒(méi)有判斷子目錄 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("解壓縮完成,耗時(shí):{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("壓縮完成,耗時(shí):{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; // 跳過(guò)不要的 } 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 壓縮與解壓縮的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java8 Zip 壓縮與解壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼
MinIO?是一款基于Go語(yǔ)言的高性能對(duì)象存儲(chǔ)服務(wù),本文主要介紹了Springboot集成minio實(shí)現(xiàn)文件存儲(chǔ)的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01SpringCloud?客戶端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法
Ribbon 是 Netflix 提供的一個(gè)基于 Http 和 TCP 的客戶端負(fù)載均衡工具,且已集成在 Eureka 依賴(lài)中,這篇文章主要介紹了SpringCloud?客戶端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-06-06Java編程實(shí)現(xiàn)打地鼠文字游戲?qū)嵗a
這篇文章主要介紹了Java編程實(shí)現(xiàn)打地鼠文字游戲?qū)嵗a,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11java數(shù)據(jù)結(jié)構(gòu)圖論霍夫曼樹(shù)及其編碼示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結(jié)構(gòu)圖論霍夫曼樹(shù)及其編碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11IntelliJ IDEA 老司機(jī)居然還沒(méi)用過(guò) Stream Trace功能(問(wèn)題小結(jié))
很多朋友酷愛(ài)Java8 Stream功能,但是在使用過(guò)程中總不是那么順利,下面通過(guò)本文給大家分享idea Stream Trace調(diào)試過(guò)程遇到的問(wèn)題,需要的朋友參考下吧2021-05-05java HashMap,TreeMap與LinkedHashMap的詳解
這篇文章主要介紹了 java HashMap,TreeMap與LinkedHashMap的詳解的相關(guān)資料,這里提供實(shí)例代碼,幫助大家學(xué)習(xí)理解 這部分的內(nèi)容,需要的朋友可以參考下2016-11-11java中BeanUtils.copyProperties的用法(超詳細(xì))
本文介紹了BeanUtils.copyProperties()方法的使用,包括其功能、用法、注意事項(xiàng)和示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08