Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法
GZip是常用的無損壓縮算法實現(xiàn),在Linux中較為常見,像我們在Linux安裝軟件時,基本都是.tar.gz格式。.tar.gz格式文件需要先對目錄內(nèi)文件進(jìn)行tar壓縮,然后使用GZip進(jìn)行壓縮。
本文針對基于磁盤的壓縮和解壓進(jìn)行演示,演示只針對一層目錄結(jié)構(gòu)進(jìn)行,多層目錄只需遞歸操作進(jìn)行即可。
Maven依賴
org.apache.commons: commons-compress: 1.19: 此依賴封裝了很多壓縮算法相關(guān)的工具類,提供的API還是相對比較底層,我們今天在它的基礎(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>
工具類
在實際應(yīng)用中,對應(yīng)不同需求,可能需要生成若干文件,然后將其壓縮。在某些應(yīng)用中,文件較小、文件數(shù)量較少且較為固定,頻繁與磁盤操作,會帶來不必要的效率影響。
工具類針對.tar.gz格式提供了compressByTar、decompressByTar、compressByGZip、decompressByGZip四個方法,用于處理.tar.gz格式壓縮文件,代碼如下:
package com.arhorchin.securitit.compress.gzip;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;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.io.IOUtils;/** * @author Securitit. * @note 基于內(nèi)存以ZIP算法進(jìn)行壓縮和解壓工具類. */public class GZipRamUtil { /** * 使用TAR算法進(jìn)行壓縮. * @param sourceFileBytesMap 待壓縮文件的Map集合. * @return 壓縮后的TAR文件字節(jié)數(shù)組. * @throws Exception 壓縮過程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長度為0. */ public static byte[] compressByTar(Map<String, byte[]> tarFileBytesMap) throws Exception { // 變量定義. ByteArrayOutputStream tarBaos = null; TarArchiveOutputStream tarTaos = null; TarArchiveEntry tarTae = null; try { // 壓縮變量初始化. tarBaos = new ByteArrayOutputStream(); tarTaos = new TarArchiveOutputStream(tarBaos); // // 將文件添加到TAR條目中. for (Map.Entry<String, byte[]> fileEntry : tarFileBytesMap.entrySet()) { tarTae = new TarArchiveEntry(fileEntry.getKey()); tarTae.setName(fileEntry.getKey()); tarTae.setSize(fileEntry.getValue().length); tarTaos.putArchiveEntry(tarTae); tarTaos.write(fileEntry.getValue()); tarTaos.closeArchiveEntry(); } } finally { if (tarTaos != null) { tarTaos.close(); } if (null == tarBaos) { tarBaos = new ByteArrayOutputStream(); } } return tarBaos.toByteArray(); } /** * 使用TAR算法進(jìn)行解壓. * @param sourceZipFileBytes TAR文件字節(jié)數(shù)組. * @return 解壓后的文件Map集合. * @throws Exception 解壓過程中可能發(fā)生的異常,若發(fā)生異常,返回Map集合長度為0. */ public static Map<String, byte[]> decompressByTar(byte[] sourceTarFileBytes) throws Exception { // 變量定義. TarArchiveEntry sourceTarTae = null; ByteArrayInputStream sourceTarBais = null; TarArchiveInputStream sourceTarTais = null; Map<String, byte[]> targetFilesFolderMap = null; try { // 解壓變量初始化. targetFilesFolderMap = new HashMap<String, byte[]>(); sourceTarBais = new ByteArrayInputStream(sourceTarFileBytes); sourceTarTais = new TarArchiveInputStream(sourceTarBais); // 條目解壓縮至Map中. while ((sourceTarTae = sourceTarTais.getNextTarEntry()) != null) { targetFilesFolderMap.put(sourceTarTae.getName(), IOUtils.toByteArray(sourceTarTais)); } } finally { if (sourceTarTais != null) sourceTarTais.close(); } return targetFilesFolderMap; } /** * 使用GZIP算法進(jìn)行壓縮. * @param sourceFileBytesMap 待壓縮文件的Map集合. * @return 壓縮后的GZIP文件字節(jié)數(shù)組. * @throws Exception 壓縮過程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長度為0. */ public static byte[] compressByGZip(byte[] sourceFileBytes) throws IOException { // 變量定義. ByteArrayOutputStream gzipBaos = null; GzipCompressorOutputStream gzipGcos = null; try { // 壓縮變量初始化. gzipBaos = new ByteArrayOutputStream(); gzipGcos = new GzipCompressorOutputStream(gzipBaos); // 采用commons-compress提供的方式進(jìn)行壓縮. gzipGcos.write(sourceFileBytes); } finally { if (gzipGcos != null) { gzipGcos.close(); } if (null == gzipBaos) { gzipBaos = new ByteArrayOutputStream(); } } return gzipBaos.toByteArray(); } /** * 使用GZIP算法進(jìn)行解壓. * @param sourceGZipFileBytes GZIP文件字節(jié)數(shù)組. * @return 解壓后的文件Map集合. * @throws Exception 解壓過程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長度為0. */ public static byte[] decompressByGZip(byte[] sourceGZipFileBytes) throws IOException { // 變量定義. ByteArrayOutputStream gzipBaos = null; ByteArrayInputStream sourceGZipBais = null; GzipCompressorInputStream sourceGZipGcis = null; try { // 解壓變量初始化. gzipBaos = new ByteArrayOutputStream(); sourceGZipBais = new ByteArrayInputStream(sourceGZipFileBytes); sourceGZipGcis = new GzipCompressorInputStream(sourceGZipBais); // 采用commons-compress提供的方式進(jìn)行解壓. gzipBaos.write(IOUtils.toByteArray(sourceGZipGcis)); } finally { if (sourceGZipGcis != null) sourceGZipGcis.close(); } return gzipBaos.toByteArray(); }}
工具類測試
在Maven依賴引入正確的情況下,復(fù)制上面的代碼到項目中,修改package,可以直接使用,下面我們對工具類進(jìn)行簡單測試。測試類代碼如下:
package com.arhorchin.securitit.compress.gzip;import java.io.File;import java.util.HashMap;import java.util.Map;import org.apache.commons.io.FileUtils;import com.arhorchin.securitit.compress.gzip.GZipRamUtil;/** * @author Securitit. * @note GZipRamUtil工具類測試. */public class GZipRamUtilTester { public static void main(String[] args) throws Exception { Map<String, byte[]> fileBytesMap = null; fileBytesMap = new HashMap<String, byte[]>(); // 設(shè)置文件列表. File dirFile = new File('C:/Users/Administrator/Downloads/個人文件/2020-07-13/files'); for (File file : dirFile.listFiles()) { fileBytesMap.put(file.getName(), FileUtils.readFileToByteArray(file)); } byte[] ramBytes = GZipRamUtil.compressByTar(fileBytesMap); ramBytes = GZipRamUtil.compressByGZip(ramBytes); FileUtils.writeByteArrayToFile(new File('C:/Users/Administrator/Downloads/個人文件/2020-07-13/ram.tar.gz'), ramBytes); ramBytes = GZipRamUtil.decompressByGZip(ramBytes); fileBytesMap = GZipRamUtil.decompressByTar(ramBytes); System.out.println(fileBytesMap.size()); }}
運(yùn)行測試后,通過查看ram.tar.gz和控制臺輸出解壓后文件數(shù)量,可以確認(rèn)工具類運(yùn)行結(jié)果無誤。
總結(jié)
1) 在小文件、文件數(shù)量較小且較為固定時,提倡使用內(nèi)存壓縮和解壓方式。使用內(nèi)存換時間,減少頻繁的磁盤操作。
2) 在大文件、文件數(shù)量較大時,提倡使用磁盤壓縮和解壓方式。過大文件對服務(wù)會造成過度的負(fù)載,磁盤壓縮和解壓可以緩解這種壓力。《Java GZip 基于磁盤實現(xiàn)壓縮和解壓》
到此這篇關(guān)于Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的文章就介紹到這了,更多相關(guān)Java GZip 實現(xiàn)壓縮和解壓內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼2. ASP中常用的22個FSO文件操作函數(shù)整理3. 三個不常見的 HTML5 實用新特性簡介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項目的過程8. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
