亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java8 Zip 壓縮與解壓縮的實現

瀏覽:11日期:2022-09-03 17:32:56

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

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

/** * Copyright sp42 [email protected] * * 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:tempbar.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()); // 大部分網絡上的源碼,這里沒有判斷子目錄 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); } /** * 內部的壓縮方法 * * @param toZip 要壓縮的目錄或文件 * @param fileName ZIP 內的文件名 * @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); } }}

到此這篇關于Java8 Zip 壓縮與解壓縮的實現的文章就介紹到這了,更多相關Java8 Zip 壓縮與解壓縮內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 一级片免费网站 | 大片免免费观看视频播放网站 | 亚洲天堂美女 | 骚婷婷 | 欧美精品亚洲精品日韩一区 | 91免费视频观看 | 久久中文字幕制服丝袜美腿 | 日本一级毛片片在线播放 | 久久久久婷婷国产综合青草 | 欧美色图影院 | 国产又色又爽的视频免费 | 伊人2222| 亚洲欧美日韩一区二区 | 色综合色综合 | 日本xxx护士与黑人 日本xxx免费视频 | 九九99视频在线观看视频观看 | 国产成人精品免费视频大全五级 | 国产一级做a爱免费观看 | 1000部啪啪未满十八勿入福利 | 大乳一级一区二区三区 | 久久国内精品自在自线400部o | 91果冻传媒文化有限公司 | 在线日韩欧美一区二区三区 | 国产精品自在线拍国产 | 一级做a爰片性色毛片小说 一级做a爰片性色毛片中国 | 亚洲欧洲一区二区三区在线 | 东京不热视频在线观看 | 免费一级毛片一级毛片aa | 国产美女色视频 | 国产美女操 | 成人黄色片视频 | 成人夜色视频在线观看网站 | 色接久久| 特级黄色生活片 | 国产精品无码2021在线观看 | 国产在线精品观看 | 黄片毛片一级 | 亚洲国产第一区 | 日韩中文字幕精品免费一区 | 亚洲免费在线视频播放 | 国产精品美女自在线观看免费 |