当前位置: 移动技术网 > IT编程>开发语言>Java > Java GZip 基于内存实现压缩和解压的方法

Java GZip 基于内存实现压缩和解压的方法

2020年08月23日  | 移动技术网IT编程  | 我要评论
欢迎大家关注本博,同时欢迎大家评论交流,可以给个赞哦!!!  gzip是常用的无损压缩算法实现,在linux中较为常见,像我们在linux安装软件时,基本都是.tar.gz格式

欢迎大家关注本博,同时欢迎大家评论交流,可以给个赞哦!!!

  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 实现压缩和解压内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网