Java GZip 基于內(nèi)存實(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)的工具類(lèi),提供的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>
工具類(lèi)
在實(shí)際應(yīng)用中,對(duì)應(yīng)不同需求,可能需要生成若干文件,然后將其壓縮。在某些應(yīng)用中,文件較小、文件數(shù)量較少且較為固定,頻繁與磁盤(pán)操作,會(huì)帶來(lái)不必要的效率影響。
工具類(lèi)針對(duì).tar.gz格式提供了compressByTar、decompressByTar、compressByGZip、decompressByGZip四個(gè)方法,用于處理.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)行壓縮和解壓工具類(lèi).
*/
public class GZipRamUtil {
/**
* 使用TAR算法進(jìn)行壓縮.
* @param sourceFileBytesMap 待壓縮文件的Map集合.
* @return 壓縮后的TAR文件字節(jié)數(shù)組.
* @throws Exception 壓縮過(guò)程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長(zhǎng)度為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 解壓過(guò)程中可能發(fā)生的異常,若發(fā)生異常,返回Map集合長(zhǎng)度為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 壓縮過(guò)程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長(zhǎng)度為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 解壓過(guò)程中可能發(fā)生的異常,若發(fā)生異常,則返回的字節(jié)數(shù)組長(zhǎng)度為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();
}
}
工具類(lèi)測(cè)試
在Maven依賴引入正確的情況下,復(fù)制上面的代碼到項(xiàng)目中,修改package,可以直接使用,下面我們對(duì)工具類(lèi)進(jìn)行簡(jiǎn)單測(cè)試。測(cè)試類(lèi)代碼如下:
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工具類(lèi)測(cè)試.
*/
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/個(gè)人文件/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/個(gè)人文件/2020-07-13/ram.tar.gz"), ramBytes);
ramBytes = GZipRamUtil.decompressByGZip(ramBytes);
fileBytesMap = GZipRamUtil.decompressByTar(ramBytes);
System.out.println(fileBytesMap.size());
}
}
運(yùn)行測(cè)試后,通過(guò)查看ram.tar.gz和控制臺(tái)輸出解壓后文件數(shù)量,可以確認(rèn)工具類(lèi)運(yùn)行結(jié)果無(wú)誤。
總結(jié)
1) 在小文件、文件數(shù)量較小且較為固定時(shí),提倡使用內(nèi)存壓縮和解壓方式。使用內(nèi)存換時(shí)間,減少頻繁的磁盤(pán)操作。
2) 在大文件、文件數(shù)量較大時(shí),提倡使用磁盤(pán)壓縮和解壓方式。過(guò)大文件對(duì)服務(wù)會(huì)造成過(guò)度的負(fù)載,磁盤(pán)壓縮和解壓可以緩解這種壓力。《Java GZip 基于磁盤(pán)實(shí)現(xiàn)壓縮和解壓》
到此這篇關(guān)于Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的文章就介紹到這了,更多相關(guān)Java GZip 實(shí)現(xiàn)壓縮和解壓內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java工具類(lèi)實(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 基于磁盤(pán)實(shí)現(xiàn)壓縮和解壓的方法
- 使用java API實(shí)現(xiàn)zip遞歸壓縮和解壓文件夾
- 如何基于java實(shí)現(xiàn)解壓ZIP TAR等文件
- Java實(shí)現(xiàn)解壓zip壓縮包的兩種方法(支持多層級(jí))
相關(guān)文章
詳解java實(shí)現(xiàn)簡(jiǎn)單掃碼登錄功能(模仿微信網(wǎng)頁(yè)版掃碼)
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單掃碼登錄功能(模仿微信網(wǎng)頁(yè)版掃碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置
本篇文章主要介紹了詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
JDK源碼分析之String、StringBuilder和StringBuffer
這篇文章主要給大家介紹了關(guān)于JDK源碼分析之String、StringBuilder和StringBuffer的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用jdk具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Java使用ScriptEngine動(dòng)態(tài)執(zhí)行代碼(附Java幾種動(dòng)態(tài)執(zhí)行代碼比較)
這篇文章主要介紹了Java使用ScriptEngine動(dòng)態(tài)執(zhí)行代碼,并且分享Java幾種動(dòng)態(tài)執(zhí)行代碼比較,需要的朋友可以參考下2021-04-04
idea2020.1最新版永久破解/pycharm也可用(步驟詳解)
這篇文章主要介紹了idea2020.1最新版永久破解/pycharm也可用,本文給大家分享簡(jiǎn)單實(shí)現(xiàn)步驟,通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Springboot實(shí)現(xiàn)定時(shí)任務(wù)的4種方式舉例詳解
在我們開(kāi)發(fā)項(xiàng)目過(guò)程中經(jīng)常需要定時(shí)任務(wù)來(lái)幫助我們來(lái)做一些內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Springboot實(shí)現(xiàn)定時(shí)任務(wù)的4種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式
這篇文章主要介紹了springboot項(xiàng)目中controller層與前端的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Java IO中字節(jié)流復(fù)制圖片實(shí)現(xiàn)代碼
這篇文章主要介紹了Java IO中字節(jié)流復(fù)制圖片實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04

