Java GZip 基于磁盤(pán)實(shí)現(xiàn)壓縮和解壓的方法
GZip是常用的無(wú)損壓縮算法實(shí)現(xiàn),在Linux中較為常見(jiàn),像我們?cè)贚inux安裝軟件時(shí),基本都是.tar.gz格式。.tar.gz格式文件需要先對(duì)目錄內(nèi)文件進(jìn)行tar壓縮,然后使用GZip進(jìn)行壓縮。
本文針對(duì)基于磁盤(pán)的壓縮和解壓進(jìn)行演示,演示只針對(duì)一層目錄結(jié)構(gòu)進(jìn)行,多層目錄只需遞歸操作進(jìn)行即可。
Maven依賴
org.apache.commons: commons-compress: 1.19: 此依賴封裝了很多壓縮算法相關(guān)的工具類,提供的API還是相對(duì)比較底層,我們今天在它的基礎(chǔ)上做進(jìn)一步封裝。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
工具類
其實(shí),在通常情況下,我們都是在磁盤(pán)上進(jìn)行壓縮和解壓操作的,這樣雖然增加了操作的復(fù)雜度,但是卻無(wú)形中避免了一些問(wèn)題。
工具類針對(duì).tar.gz格式提供了compressByTar、decompressByTar、compressByGZip、decompressByGZip四個(gè)方法,用于處理.tar.gz格式壓縮文件,代碼如下:
package com.arhorchin.securitit.compress.gzip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.utils.IOUtils; import org.apache.log4j.Logger; /** * @author Securitit. * @note 基于磁盤(pán)以GZIP算法進(jìn)行壓縮和解壓工具類. */ public class GZipDiskUtil { /** * logger. */ private static Logger logger = Logger.getLogger(GZipDiskUtil.class); /** * UTF-8字符集. */ public static String CHARSET_UTF8 = "UTF-8"; /** * 使用TAR算法進(jìn)行壓縮. * @param sourceFolderPath 待進(jìn)行壓縮的文件夾路徑. * @param targetTarFilePath 壓縮后的TAR文件存儲(chǔ)目錄. * @return 壓縮是否成功. * @throws Exception 壓縮過(guò)程中可能發(fā)生的異常. */ public static boolean compressByTar(String sourceFolderPath, String targetTarFilePath) throws Exception { // 變量定義. File sourceFolderFile = null; FileOutputStream targetTarFos = null; TarArchiveOutputStream targetTartTaos = null; TarArchiveEntry targetTarTae = null; try { // 壓縮變量初始化. sourceFolderFile = new File(sourceFolderPath); targetTarFos = new FileOutputStream(new File(targetTarFilePath)); targetTartTaos = new TarArchiveOutputStream(targetTarFos); // 將文件添加到ZIP條目中. for (File file : sourceFolderFile.listFiles()) { try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis);) { targetTarTae = new TarArchiveEntry(file); targetTarTae.setName(file.getName()); targetTartTaos.putArchiveEntry(targetTarTae); targetTartTaos.write(IOUtils.toByteArray(bis)); targetTartTaos.closeArchiveEntry(); } } } catch (Exception ex) { logger.info("GZipDiskUtil.compressByTar.", ex); return false; } finally { if (targetTartTaos != null) targetTartTaos.close(); if (targetTarFos != null) targetTarFos.close(); } return true; } /** * 使用TAR算法進(jìn)行解壓. * @param sourceTarPath 待解壓文件路徑. * @param targetFolderPath 解壓后文件夾目錄. * @return 解壓是否成功. * @throws Exception 解壓過(guò)程中可能發(fā)生的異常. */ public static boolean decompressByTar(String sourceTarPath, String targetFolderPath) throws Exception { // 變量定義. FileInputStream sourceTarFis = null; TarArchiveInputStream sourceTarTais = null; TarArchiveEntry sourceTarTae = null; File singleEntryFile = null; try { // 解壓定義初始化. sourceTarFis = new FileInputStream(new File(sourceTarPath)); sourceTarTais = new TarArchiveInputStream(sourceTarFis); // 條目解壓縮至指定文件夾目錄下. while ((sourceTarTae = sourceTarTais.getNextTarEntry()) != null) { singleEntryFile = new File(targetFolderPath + File.separator + sourceTarTae.getName()); try (FileOutputStream fos = new FileOutputStream(singleEntryFile); BufferedOutputStream bos = new BufferedOutputStream(fos);) { bos.write(IOUtils.toByteArray(sourceTarTais)); } } } catch (Exception ex) { logger.info("GZipDiskUtil.decompressByTar.", ex); return false; } finally { if (sourceTarTais != null) sourceTarTais.close(); if (sourceTarFis != null) sourceTarFis.close(); } return true; } /** * 使用GZIP算法進(jìn)行壓縮. * @param sourceFilePath 待進(jìn)行壓縮的文件路徑. * @param targetGZipFilePath 壓縮后的GZIP文件存儲(chǔ)目錄. * @return 壓縮是否成功. * @throws Exception 壓縮過(guò)程中可能發(fā)生的異常. */ public static boolean compressByGZip(String sourceFilePath, String targetGZipFilePath) throws IOException { // 變量定義. FileInputStream sourceFileFis = null; BufferedInputStream sourceFileBis = null; FileOutputStream targetGZipFileFos = null; BufferedOutputStream targetGZipFileBos = null; GzipCompressorOutputStream targetGZipFileGcos = null; try { // 壓縮變量初始化. sourceFileFis = new FileInputStream(new File(sourceFilePath)); sourceFileBis = new BufferedInputStream(sourceFileFis); targetGZipFileFos = new FileOutputStream(targetGZipFilePath); targetGZipFileBos = new BufferedOutputStream(targetGZipFileFos); targetGZipFileGcos = new GzipCompressorOutputStream(targetGZipFileBos); // 采用commons-compress提供的方式進(jìn)行壓縮. targetGZipFileGcos.write(IOUtils.toByteArray(sourceFileBis)); } catch (Exception ex) { logger.info("GZipDiskUtil.compressByGZip.", ex); return false; } finally { if (targetGZipFileGcos != null) targetGZipFileGcos.close(); if (targetGZipFileBos != null) targetGZipFileBos.close(); if (targetGZipFileFos != null) targetGZipFileFos.close(); if (sourceFileBis != null) sourceFileBis.close(); if (sourceFileFis != null) sourceFileFis.close(); } return true; } /** * 使用GZIP算法進(jìn)行解壓. * @param sourceGZipFilePath 待解壓文件路徑. * @param targetFilePath 解壓后文件路徑. * @return 解壓是否成功. * @throws @throws Exception 解壓過(guò)程中可能發(fā)生的異常. */ public static boolean decompressByGZip(String sourceGZipFilePath, String targetFilePath) throws IOException { // 變量定義. FileInputStream sourceGZipFileFis = null; BufferedInputStream sourceGZipFileBis = null; FileOutputStream targetFileFos = null; GzipCompressorInputStream sourceGZipFileGcis = null; try { // 解壓變量初始化. sourceGZipFileFis = new FileInputStream(new File(sourceGZipFilePath)); sourceGZipFileBis = new BufferedInputStream(sourceGZipFileFis); sourceGZipFileGcis = new GzipCompressorInputStream(sourceGZipFileBis); targetFileFos = new FileOutputStream(new File(targetFilePath)); // 采用commons-compress提供的方式進(jìn)行解壓. targetFileFos.write(IOUtils.toByteArray(sourceGZipFileGcis)); } catch (Exception ex) { logger.info("GZipDiskUtil.decompressByGZip.", ex); return false; } finally { if (sourceGZipFileGcis != null) sourceGZipFileGcis.close(); if (sourceGZipFileBis != null) sourceGZipFileBis.close(); if (sourceGZipFileFis != null) sourceGZipFileFis.close(); if (targetFileFos != null) targetFileFos.close(); } return true; } }
工具類測(cè)試
在Maven依賴引入正確的情況下,復(fù)制上面的代碼到項(xiàng)目中,修改package,可以直接使用,下面我們對(duì)工具類進(jìn)行簡(jiǎn)單測(cè)試。測(cè)試類代碼如下:
package com.arhorchin.securitit.compress.gzip; import com.arhorchin.securitit.compress.gzip.GZipDiskUtil; /** * @author Securitit. * @note GZipDiskUtil工具類測(cè)試. */ public class GZipDiskUtilTester { public static void main(String[] args) throws Exception { GZipDiskUtil.compressByTar("C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/files", "C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk.tar"); GZipDiskUtil.compressByGZip("C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk.tar", "C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk.tar.gz"); GZipDiskUtil.decompressByGZip("C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk.tar.gz", "C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk-untar.tar"); GZipDiskUtil.decompressByTar("C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk-untar.tar", "C:/Users/Administrator/Downloads/個(gè)人文件/2020-07-13/disk-untar"); } }
運(yùn)行測(cè)試后,通過(guò)查看disk.tar、disk.tar.gz、disk-untar.tar和解壓的目錄,可以確認(rèn)工具類運(yùn)行結(jié)果無(wú)誤。
總結(jié)
1) 在小文件、文件數(shù)量較小且較為固定時(shí),提倡使用內(nèi)存壓縮和解壓方式。使用內(nèi)存換時(shí)間,減少頻繁的磁盤(pán)操作?!?a href="http://www.dbjr.com.cn/article/193897.htm" target="_blank">Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓》
2) 在大文件、文件數(shù)量較大時(shí),提倡使用磁盤(pán)壓縮和解壓方式。過(guò)大文件對(duì)服務(wù)會(huì)造成過(guò)度的負(fù)載,磁盤(pán)壓縮和解壓可以緩解這種壓力。
- java工具類實(shí)現(xiàn)文件壓縮zip以及解壓縮功能
- Java實(shí)現(xiàn)解壓zip和rar包的示例代碼
- Java壓縮和解壓縮ZIP文件實(shí)戰(zhàn)案例
- Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
- Java如何不解壓讀取.zip的文件內(nèi)容
- Java中zip的壓縮和解壓縮的實(shí)現(xiàn)代碼
- 用Java進(jìn)行zip文件壓縮與解壓縮
- Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法
- 使用java API實(shí)現(xiàn)zip遞歸壓縮和解壓文件夾
- 如何基于java實(shí)現(xiàn)解壓ZIP TAR等文件
- Java實(shí)現(xiàn)解壓zip壓縮包的兩種方法(支持多層級(jí))
相關(guān)文章
Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗(yàn)注解
這篇文章主要介紹了Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗(yàn)注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類
貝葉斯分類算法是統(tǒng)計(jì)學(xué)的一種分類方法,它是一類利用概率統(tǒng)計(jì)知識(shí)進(jìn)行分類的算法。本文將利用樸素貝葉斯分類算法實(shí)現(xiàn)信息分類,需要的可以參考一下2022-06-06SpringBoot優(yōu)化接口響應(yīng)時(shí)間的九個(gè)技巧
在實(shí)際開(kāi)發(fā)中,提升接口響應(yīng)速度是一件挺重要的事,特別是在面臨大量用戶請(qǐng)求的時(shí)候,本文為大家整理了9個(gè)SpringBoot優(yōu)化接口響應(yīng)時(shí)間的技巧,希望對(duì)大家有所幫助2024-01-01SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析
這篇文章主要介紹了SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Spring Boot 實(shí)現(xiàn)程序的優(yōu)雅退出(詳細(xì)步驟)
Spring Boot 為我們提供了優(yōu)雅退出的功能,使應(yīng)用程序能夠在關(guān)閉時(shí)正常處理完所有當(dāng)前請(qǐng)求,避免請(qǐng)求被中斷導(dǎo)致數(shù)據(jù)丟失或不一致等問(wèn)題,本文將全面介紹如何在 Spring Boot 應(yīng)用程序中實(shí)現(xiàn)優(yōu)雅退出,感興趣的朋友跟隨小編一起看看吧2024-03-03springboot攔截器過(guò)濾token,并返回結(jié)果及異常處理操作
這篇文章主要介紹了springboot攔截器過(guò)濾token,并返回結(jié)果及異常處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09