Java GZip 基於記憶體實現壓縮和解壓的方法
GZip是常用的無失真壓縮演算法實現,在Linux中較為常見,像我們在Linux安裝軟體時,基本都是.tar.gz格式。.tar.gz格式檔案需要先對目錄內檔案進行tar壓縮,然後使用GZip進行壓縮。
本文針對基於磁碟的壓縮和解壓進行演示,演示只針對一層目錄結構進行,多層目錄只需遞迴操作進行即可。
Maven依賴
org.apache.commons: commons-compress: 1.19: 此依賴封裝了很多壓縮演算法相關的工具類,提供的API還是相對比較底層,我們今天在它的基礎上做進一步封裝。
<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>
工具類
在實際應用中,對應不同需求,可能需要生成若干檔案,然後將其壓縮。在某些應用中,檔案較小、檔案數量較少且較為固定,頻繁與磁碟操作,會帶來不必要的效率影響。
工具類針對.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 基於記憶體以ZIP演算法進行壓縮和解壓工具類. */ public class GZipRamUtil { /** * 使用TAR演算法進行壓縮. * @param sourceFileBytesMap 待壓縮檔案的Map集合. * @return 壓縮後的TAR檔案位元組陣列. * @throws Exception 壓縮過程中可能發生的異常,若發生異常,則返回的位元組陣列長度為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演算法進行解壓. * @param sourceZipFileBytes TAR檔案位元組陣列. * @return 解壓後的檔案Map集合. * @throws Exception 解壓過程中可能發生的異常,若發生異常,返回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演算法進行壓縮. * @param sourceFileBytesMap 待壓縮檔案的Map集合. * @return 壓縮後的GZIP檔案位元組陣列. * @throws Exception 壓縮過程中可能發生的異常,若發生異常,則返回的位元組陣列長度為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提供的方式進行壓縮. gzipGcos.write(sourceFileBytes); } finally { if (gzipGcos != null) { gzipGcos.close(); } if (null == gzipBaos) { gzipBaos = new ByteArrayOutputStream(); } } return gzipBaos.toByteArray(); } /** * 使用GZIP演算法進行解壓. * @param sourceGZipFileBytes GZIP檔案位元組陣列. * @return 解壓後的檔案Map集合. * @throws Exception 解壓過程中可能發生的異常,若發生異常,則返回的位元組陣列長度為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提供的方式進行解壓. gzipBaos.write(IOUtils.toByteArray(sourceGZipGcis)); } finally { if (sourceGZipGcis != null) sourceGZipGcis.close(); } return gzipBaos.toByteArray(); } }
工具類測試
在Maven依賴引入正確的情況下,複製上面的程式碼到專案中,修改package,可以直接使用,下面我們對工具類進行簡單測試。測試類程式碼如下:
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[]>(); // 設定檔案列表. 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()); } }
執行測試後,通過檢視ram.tar.gz和控制檯輸出解壓後文件數量,可以確認工具類執行結果無誤。
總結
1) 在小檔案、檔案數量較小且較為固定時,提倡使用記憶體壓縮和解壓方式。使用記憶體換時間,減少頻繁的磁碟操作。
2) 在大檔案、檔案數量較大時,提倡使用磁碟壓縮和解壓方式。過大檔案對服務會造成過度的負載,磁碟壓縮和解壓可以緩解這種壓力。《Java GZip 基於磁碟實現壓縮和解壓》
到此這篇關於Java GZip 基於記憶體實現壓縮和解壓的文章就介紹到這了,更多相關Java GZip 實現壓縮和解壓內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!