日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Java GZip 基于磁盤實現(xiàn)壓縮和解壓的方法

瀏覽:203日期:2022-08-26 11:52:59

  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>

  工具類

  其實,在通常情況下,我們都是在磁盤上進(jìn)行壓縮和解壓操作的,這樣雖然增加了操作的復(fù)雜度,但是卻無形中避免了一些問題。

  工具類針對.tar.gz格式提供了compressByTar、decompressByTar、compressByGZip、decompressByGZip四個方法,用于處理.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 基于磁盤以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文件存儲目錄. * @return 壓縮是否成功. * @throws Exception 壓縮過程中可能發(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 解壓過程中可能發(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文件存儲目錄. * @return 壓縮是否成功. * @throws Exception 壓縮過程中可能發(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 解壓過程中可能發(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; }}

  工具類測試

  在Maven依賴引入正確的情況下,復(fù)制上面的代碼到項目中,修改package,可以直接使用,下面我們對工具類進(jìn)行簡單測試。測試類代碼如下:

package com.arhorchin.securitit.compress.gzip;import com.arhorchin.securitit.compress.gzip.GZipDiskUtil;/** * @author Securitit. * @note GZipDiskUtil工具類測試. */public class GZipDiskUtilTester { public static void main(String[] args) throws Exception { GZipDiskUtil.compressByTar('C:/Users/Administrator/Downloads/個人文件/2020-07-13/files', 'C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk.tar'); GZipDiskUtil.compressByGZip('C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk.tar', 'C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk.tar.gz'); GZipDiskUtil.decompressByGZip('C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk.tar.gz', 'C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk-untar.tar'); GZipDiskUtil.decompressByTar('C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk-untar.tar', 'C:/Users/Administrator/Downloads/個人文件/2020-07-13/disk-untar'); }}

  運行測試后,通過查看disk.tar、disk.tar.gz、disk-untar.tar和解壓的目錄,可以確認(rèn)工具類運行結(jié)果無誤。

  總結(jié)

  1) 在小文件、文件數(shù)量較小且較為固定時,提倡使用內(nèi)存壓縮和解壓方式。使用內(nèi)存換時間,減少頻繁的磁盤操作。《Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓》

  2) 在大文件、文件數(shù)量較大時,提倡使用磁盤壓縮和解壓方式。過大文件對服務(wù)會造成過度的負(fù)載,磁盤壓縮和解壓可以緩解這種壓力。

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
婷婷亚洲五月| 免费看欧美美女黄的网站| 免费在线观看日韩欧美| 日韩国产在线| 成人看片网站| 美日韩精品视频| 日韩一区三区| 四季av一区二区凹凸精品| 日韩欧美三级| 爽好久久久欧美精品| 欧美1区免费| 老司机精品视频网| 国产日韩亚洲欧美精品| 久久午夜影院| 国产亚洲一区二区手机在线观看| 色一区二区三区| 91精品啪在线观看国产18| 欧美日本三区| 久久精品国产在热久久| 久久久久久黄| 欧美不卡高清| 在线亚洲精品| 蜜臀久久99精品久久久久宅男 | 亚洲综合激情在线| 美女免费视频一区| 午夜电影亚洲| 久久一区视频| 久久激情综合网| 韩国三级一区| 国产成人精选| 在线成人动漫av| 亚洲区欧美区| 国产亚洲午夜| 亚洲一区欧美激情| 在线观看免费一区二区| 久久久国产精品入口麻豆| 国产一区二区三区四区二区| 国产乱码精品一区二区亚洲| 97se综合| 首页国产欧美久久| 国产精品一区2区3区| 中文字幕成在线观看| 亚洲一区黄色| 精品久久久中文字幕| 欧美~级网站不卡| 精品国产亚洲日本| 日韩av电影一区| 日本a级不卡| 在线一区视频观看| 精品一区二区男人吃奶| 精品一区二区三区免费看| 国产精品视频一区二区三区综合| 亚洲91网站| 免费成人在线视频观看| 亚洲a一区二区三区| 欧美日韩国产一区精品一区| 黄色免费成人| 一区二区亚洲视频| 亚洲精品国产精品粉嫩| 国产美女久久| 久久免费视频66| 亚洲人www| 国产精品**亚洲精品| 久久精品国产在热久久| 国产精品最新| 黑丝一区二区三区| 国产精品mv在线观看| 亚洲精品免费观看| 宅男在线一区| 国内激情久久| 91亚洲精品视频在线观看| 麻豆一区在线| 国产一区日韩一区| 免费精品视频| 综合激情网站| 成人精品动漫一区二区三区| 欧美日韩亚洲在线观看| 国产日韩欧美一区二区三区| 成人av动漫在线观看| 精品日产乱码久久久久久仙踪林| 色综合视频一区二区三区日韩 | 波多视频一区| 蜜桃视频在线网站| 妖精视频成人观看www| 日本色综合中文字幕| 蜜桃成人精品| 亚洲色图网站| 亚洲v天堂v手机在线| 日本精品另类| 亚洲精品国产偷自在线观看| 国产精品美女在线观看直播| 亚洲国产影院| 国产一区二区三区视频在线| 亚洲尤物av| 蜜臀av在线播放一区二区三区| 国产一区 二区| 91一区二区三区四区| 精品资源在线| 日本强好片久久久久久aaa| 蜜桃视频一区二区三区| 99成人在线| 国产午夜一区| 亚洲精品电影| 欧美亚洲国产一区| 久久中文亚洲字幕| 成人福利av| 国产精品99精品一区二区三区∴| 成年男女免费视频网站不卡| 国产精品xxxav免费视频| 91久久精品无嫩草影院| 香蕉久久一区| 欧美日韩国产在线一区| 国产一区欧美| 日韩视频在线一区二区三区 | 久久一区二区三区喷水| 国产视频网站一区二区三区| 激情五月色综合国产精品| 国产一区二区精品久| 国产日韩一区二区三免费高清| 伊人国产精品| 91精品国产自产精品男人的天堂 | 国产精品伦理久久久久久| 国产精品资源| 国产亚洲观看| 韩日一区二区| 亚洲午夜天堂| 国产成人精品免费视| 久久精品国产亚洲一区二区三区| 美女视频网站久久| 中文字幕高清在线播放| 亚洲精品国模| 日韩精品一二三四| 免费av一区二区三区四区| 国产一二在线播放| 老司机精品久久| 国产伦精品一区二区三区在线播放| 国产欧美综合一区二区三区| 国产欧美91| 伊人久久亚洲| 色综合视频一区二区三区日韩| 亚洲1区在线观看| 欧美jjzz| 亚洲欧美日韩综合国产aⅴ| 一区二区电影| 四虎精品一区二区免费| 亚洲日本三级| 国产精品免费精品自在线观看| 精品国产不卡一区二区| 久久中文欧美| 蜜桃久久久久| 国模 一区 二区 三区| 久久激情综合网| 蜜臀av免费一区二区三区| 夜夜嗨一区二区| 欧美黄色一区二区| 狠狠爱www人成狠狠爱综合网| 日韩精品第一| 亚洲精品123区| 国产精品成人a在线观看| 亚洲欧美日韩国产| 日韩三级一区| 91精品91| 亚洲精品2区| 国产一在线精品一区在线观看| 国产精品超碰| 国产精品久久久久av蜜臀| 亚洲精品福利| 亚洲精品一区二区在线播放∴| 国产婷婷精品| 中文字幕一区二区三区四区久久| 91青青国产在线观看精品| 日韩专区一卡二卡| 国产精品日本| 日本不卡一区二区| 精品三级av在线导航| 国产精品99久久免费| 精品一区二区三区视频在线播放| 国产精品多人| 精品国产aⅴ| 日本午夜精品| 蜜臀久久精品| 久久99影视| 国产专区一区| 91精品尤物| 99久久久久国产精品| 免费看日韩精品| 在线看片国产福利你懂的| 视频一区二区国产| 成午夜精品一区二区三区软件| 视频在线观看一区二区三区| 日韩在线麻豆| 91免费精品国偷自产在线在线| 国产乱码午夜在线视频| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 国产精品白丝一区二区三区| 日本免费一区二区视频| 精品一区二区三区中文字幕视频| 九一精品国产| 精品一区二区三区亚洲| 亚洲精品高潮|