1. 程式人生 > >使用apache.commons.compress壓縮和加壓檔案工具類

使用apache.commons.compress壓縮和加壓檔案工具類

package com.my.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import java.io.*; /** * 壓縮工具類 * * @author helloworld */ @Slf4j public class ZipUtil { /** * 將檔案打包成zip壓縮包檔案 * * @param files 要壓縮的檔案 * @param zipFile 壓縮檔案 * @param deleteFileAfterZip 壓縮檔案後刪除原來的檔案,臨時檔案時記得刪除 * @return
是否壓縮成功 */
public static boolean compressFiles2Zip(List<File> files, File zipFile, boolean deleteFileAfterZip) { InputStream inputStream = null; ZipArchiveOutputStream zipArchiveOutputStream = null; try { zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile); //Use Zip64 extensions for all entries where they are required
zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded); for (File file : files) { //將每個檔案用ZipArchiveEntry封裝,使用ZipArchiveOutputStream寫到壓縮檔案 ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName()); zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry); inputStream = new FileInputStream(file); byte[] buffer = new byte[1024 * 5]; int len = -1; while ((len = inputStream.read(buffer)) != -1) { //把緩衝區的位元組寫入到ZipArchiveEntry zipArchiveOutputStream.write(buffer, 0, len); } } zipArchiveOutputStream.closeArchiveEntry(); zipArchiveOutputStream.finish(); if (deleteFileAfterZip) { for (File file : files) { file.deleteOnExit(); } } } catch (IOException e) { e.printStackTrace(); return false; } finally { try { //關閉輸入流 if (null != inputStream) { inputStream.close(); } //關閉輸出流 if (null != zipArchiveOutputStream) { zipArchiveOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * 將zip壓縮包解壓成檔案到指定資料夾 * * @param zipFilePath 要解壓的檔案 * @param targetDirPath 解壓的目的路徑 * @return 是否成功 */ public static boolean decompressZip2Files(String zipFilePath, String targetDirPath) { InputStream inputStream = null; OutputStream outputStream = null; //zip檔案輸入流 ZipArchiveInputStream zipArchiveInputStream = null; ArchiveEntry archiveEntry = null; try { File zipFile = new File(zipFilePath); inputStream = new FileInputStream(zipFile); zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8"); while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) { //獲取檔名 String archiveEntryFileName = archiveEntry.getName(); //構造解壓後文件的存放路徑 String archiveEntryPath = targetDirPath + archiveEntryFileName; //把解壓出來的檔案寫到指定路徑 File entryFile = new File(archiveEntryPath); if (!entryFile.exists()) { boolean mkdirs = entryFile.getParentFile().mkdirs(); } byte[] buffer = new byte[1024 * 5]; outputStream = new FileOutputStream(entryFile); int len = -1; while ((len = zipArchiveInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.flush(); } } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (null != outputStream) { outputStream.close(); } if (null != zipArchiveInputStream) { zipArchiveInputStream.close(); } if (null != inputStream) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * 將檔案打包成zip壓縮包檔案 * * @param docs 要壓縮的檔案 * @param targetFile 壓縮檔案 * @return 是否壓縮成功 */ public static boolean compressDoc2Zip(List<Pair<String, XWPFDocument>> docs, File targetFile) throws IOException { List<File> fileList = new ArrayList<>(); for (Pair<String, XWPFDocument> doc : docs) { String name = doc.getFirst(); XWPFDocument document = doc.getSecond(); File file = convertDocument2File(document, name); fileList.add(file); } return compressFiles2Zip(fileList, targetFile, true); } /** * 將XWPFDocument轉為File * * @param document 原始檔 * @param fileName file名稱 * @return 轉換後的file * @throws IOException 轉換那一層 */ public static File convertDocument2File(XWPFDocument document, String fileName) throws IOException { if (fileName.length() <= 3) { fileName += "-" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"); } if (!fileName.endsWith("docx")) { fileName += ".docx"; } File file = createTempFile(fileName); try (OutputStream os = new FileOutputStream(file)) { document.write(os); } return file; } /** * 建立臨時檔案 */ public static File createTempFile(String fullFileName) throws IOException { String tempDirectoryPath = FileUtils.getTempDirectoryPath(); File file = new File(tempDirectoryPath + File.separator + fullFileName); file.deleteOnExit(); boolean newFile = file.createNewFile(); log.debug("newFile {} => {}", fullFileName, newFile); return file; } public static void main(String[] args) { File f1 = new File("/Users/my/Desktop/WechatIMG78.jpeg"); File f2 = new File("/Users/my/Desktop/WechatIMG79.jpeg"); File f3 = new File("/Users/my/Desktop/aaaaa.docx"); // 壓縮檔案 boolean b = compressFiles2Zip(new File[]{f1, f2, f3}, "/Users/my/Desktop/haha.zip"); log.info("compressFiles2Zip={}", b); // 解壓縮檔案 boolean b1 = decompressZip2Files("/Users/my/Desktop/haha.zip", "/Users/zhangbaozhen/my/hahaha/"); log.info("decompressZip2Files={}", b1); } }