最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年08月22日 14:33:40   作者:超哥說碼  
這篇文章主要介紹了Java GZip 基于磁盤實現(xiàn)壓縮和解壓,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考需要的朋友可以參考下

  GZip是常用的無損壓縮算法實現(xiàn),在Linux中較為常見,像我們在Linux安裝軟件時,基本都是.tar.gz格式。.tar.gz格式文件需要先對目錄內(nèi)文件進行tar壓縮,然后使用GZip進行壓縮。

  本文針對基于磁盤的壓縮和解壓進行演示,演示只針對一層目錄結(jié)構(gòu)進行,多層目錄只需遞歸操作進行即可。

  Maven依賴

  org.apache.commons: commons-compress: 1.19: 此依賴封裝了很多壓縮算法相關(guān)的工具類,提供的API還是相對比較底層,我們今天在它的基礎(chǔ)上做進一步封裝。

<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.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算法進行壓縮和解壓工具類.
 */
public class GZipDiskUtil {

 /**
 * logger.
 */
 private static Logger logger = Logger.getLogger(GZipDiskUtil.class);

 /**
 * UTF-8字符集.
 */
 public static String CHARSET_UTF8 = "UTF-8";

 /**
 * 使用TAR算法進行壓縮.
 * @param sourceFolderPath 待進行壓縮的文件夾路徑.
 * @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算法進行解壓.
 * @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算法進行壓縮.
 * @param sourceFilePath 待進行壓縮的文件路徑.
 * @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提供的方式進行壓縮.
  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算法進行解壓.
 * @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提供的方式進行解壓.
  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依賴引入正確的情況下,復制上面的代碼到項目中,修改package,可以直接使用,下面我們對工具類進行簡單測試。測試類代碼如下:

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和解壓的目錄,可以確認工具類運行結(jié)果無誤。

總結(jié)

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

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

相關(guān)文章

  • java中的tostring方法的具體用法

    java中的tostring方法的具體用法

    這篇文章主要介紹了java中的tostring方法的具體用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面我們來一起學習一下吧
    2019-06-06
  • SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題

    SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題

    這篇文章主要介紹了SpringBoot2.0.3打印默認數(shù)據(jù)源為 HikariDataSource (null)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 如何動態(tài)改變Retrofit的base url和rest版本詳解

    如何動態(tài)改變Retrofit的base url和rest版本詳解

    這篇文章主要給大家介紹了關(guān)于如何動態(tài)改變Retrofit的base url和rest版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • SpringBoot整合JWT實戰(zhàn)教程

    SpringBoot整合JWT實戰(zhàn)教程

    JWT(JSON?Web?Token)是一種用于身份驗證和授權(quán)的開放標準(RFC?7519),它使用JSON格式傳輸信息,可以在不同系統(tǒng)之間安全地傳遞數(shù)據(jù),這篇文章主要介紹了SpringBoot整合JWT實戰(zhàn)教程,需要的朋友可以參考下
    2023-06-06
  • 全局記錄Feign的請求和響應日志方式

    全局記錄Feign的請求和響應日志方式

    這篇文章主要介紹了全局記錄Feign的請求和響應日志方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java插入修改刪除數(shù)據(jù)庫數(shù)據(jù)的基本方法

    Java插入修改刪除數(shù)據(jù)庫數(shù)據(jù)的基本方法

    這篇文章主要介紹了Java插入修改刪除數(shù)據(jù)庫數(shù)據(jù)的基本方法,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • Java實現(xiàn)的獲取和判斷文件頭信息工具類用法示例

    Java實現(xiàn)的獲取和判斷文件頭信息工具類用法示例

    這篇文章主要介紹了Java實現(xiàn)的獲取和判斷文件頭信息工具類,結(jié)合實例形式分析了Java針對文件讀取及頭信息判斷相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Mybatis的核心架構(gòu)及源碼解讀

    Mybatis的核心架構(gòu)及源碼解讀

    這篇文章主要介紹了Mybatis的核心架構(gòu)及源碼解讀,mybatis是一款半自動化的持久層框架,它封裝了JDBC操作,支持定制化SQL,高級映射,但它的數(shù)據(jù)庫無關(guān)性較低,需要的朋友可以參考下
    2023-08-08
  • SWT(JFace)體驗之模擬BorderLayout布局

    SWT(JFace)體驗之模擬BorderLayout布局

    SWT(JFace)體驗之模擬BorderLayout布局代碼。
    2009-06-06
  • BeanUtils.copyProperties擴展--實現(xiàn)String轉(zhuǎn)Date

    BeanUtils.copyProperties擴展--實現(xiàn)String轉(zhuǎn)Date

    這篇文章主要介紹了BeanUtils.copyProperties擴展--實現(xiàn)String轉(zhuǎn)Date操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評論

云安县| 高雄市| 上杭县| 栾城县| 确山县| 水城县| 澄城县| 静宁县| 横山县| 万山特区| 芒康县| 丘北县| 峨眉山市| 扶沟县| 宣武区| 巴马| 南投市| 福泉市| 黄浦区| 宿迁市| 镇安县| 大埔县| 江西省| 磐安县| 扶风县| 苍南县| 遵义市| 昔阳县| 深泽县| 县级市| 乐安县| 曲周县| 耒阳市| 油尖旺区| 大足县| 宜川县| 安新县| 长宁县| 牙克石市| 淅川县| 莎车县|