一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包

java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包

2020-06-24 11:30安迪的信仰 JAVA教程

本文是利用Java原生類和apache的commons實(shí)現(xiàn)zip,gzip,7z,zlib的壓縮打包,如果你要是感興趣可以進(jìn)來了解一下。

本文主要介紹的是通過使用java的相關(guān)類可以實(shí)現(xiàn)對(duì)文件或文件夾的壓縮

zlib是一種數(shù)據(jù)壓縮程序庫(kù),它的設(shè)計(jì)目標(biāo)是處理單純的數(shù)據(jù)(而不管數(shù)據(jù)的來源是什么)。

7z 是一種新的壓縮格式,它擁有目前最高的壓縮比。

gzip是一種文件壓縮工具(或該壓縮工具產(chǎn)生的壓縮文件格式),它的設(shè)計(jì)目標(biāo)是處理單個(gè)的文件。gzip在壓縮文件中的數(shù)據(jù)時(shí)使用的就是zlib。為了保存與文件屬性有關(guān)的信息,gzip需要在壓縮文件(*.gz)中保存更多的頭信息內(nèi)容,而zlib不用考慮這一點(diǎn)。但gzip只適用于單個(gè)文件,所以我們?cè)赨NIX/Linux上經(jīng)常看到的壓縮包后綴都是*.tar.gz或*.tgz,也就是先用tar把多個(gè)文件打包成單個(gè)文件,再用gzip壓縮的結(jié)果。

zip是適用于壓縮多個(gè)文件的格式(相應(yīng)的工具有PkZip和WinZip等),因此,zip文件還要進(jìn)一步包含文件目錄結(jié)構(gòu)的信息,比gzip的頭信息更多。但需要注意,zip格式可采用多種壓縮算法,我們常見的zip文件大多不是用zlib的算法壓縮的,其壓縮數(shù)據(jù)的格式與gzip大不一樣。

所以,你應(yīng)當(dāng)根據(jù)你的具體需求,選擇不同的壓縮技術(shù):如果只需要壓縮/解壓縮數(shù)據(jù),你可以直接用zlib實(shí)現(xiàn),如果需要生成gzip格式的文件或解壓其他工具的壓縮結(jié)果,你就必須用gzip或zip等相關(guān)的類來處理了。

maven依賴
 

?
1
2
3
4
5
<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.12</version>
    </dependency>

zip格式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void zip(String input, String output, String name) throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));
    String[] paths = input.split("\\|");
    File[] files = new File[paths.length];
    byte[] buffer = new byte[1024];
    for (int i = 0; i < paths.length; i++) {
      files[i] = new File(paths[i]);
    }
    for (int i = 0; i < files.length; i++) {
      FileInputStream fis = new FileInputStream(files[i]);
      if (files.length == 1 && name != null) {
        out.putNextEntry(new ZipEntry(name));
      } else {
        out.putNextEntry(new ZipEntry(files[i].getName()));
      }
      int len;
      // 讀入需要下載的文件的內(nèi)容,打包到zip文件
      while ((len = fis.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
      out.closeEntry();
      fis.close();
    }
    out.close();
  }

gzip打包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void gzip(String input, String output, String name) throws Exception {
    String compress_name = null;
    if (name != null) {
      compress_name = name;
    } else {
      compress_name = new File(input).getName();
    }
    byte[] buffer = new byte[1024];
    try {
      GzipParameters gp = new GzipParameters();  //設(shè)置壓縮文件里的文件名
      gp.setFilename(compress_name);
      GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new FileOutputStream(output), gp);
      FileInputStream fis = new FileInputStream(input);
      int length;
      while ((length = fis.read(buffer)) > 0) {
        gcos.write(buffer, 0, length);
      }
      fis.close();
      gcos.finish();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

7z打包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static void z7z(String input, String output, String name) throws Exception {
    try {
      SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(output));
      SevenZArchiveEntry entry = null;
 
      String[] paths = input.split("\\|");
      File[] files = new File[paths.length];
      for (int i = 0; i < paths.length; i++) {
        files[i] = new File(paths[i].trim());
      }
      for (int i = 0; i < files.length; i++) {
        BufferedInputStream instream = null;
        instream = new BufferedInputStream(new FileInputStream(paths[i]));
        if (name != null) {
          entry = sevenZOutput.createArchiveEntry(new File(paths[i]), name);
        } else {
          entry = sevenZOutput.createArchiveEntry(new File(paths[i]), new File(paths[i]).getName());
        }
        sevenZOutput.putArchiveEntry(entry);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = instream.read(buffer)) > 0) {
          sevenZOutput.write(buffer, 0, len);
        }
        instream.close();
        sevenZOutput.closeArchiveEntry();
      }
      sevenZOutput.close();
    } catch (IOException ioe) {
      System.out.println(ioe.toString() + " " + input);
    }
  }

zlib打包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void zlib(String input, String output) throws Exception {
 
//    DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output));
    DeflateParameters dp = new DeflateParameters();
    dp.setWithZlibHeader(true);
    DeflateCompressorOutputStream dcos = new DeflateCompressorOutputStream(new FileOutputStream(output),dp);
    FileInputStream fis = new FileInputStream(input);
    int length = (int) new File(input).length();
    byte data[] = new byte[length];
    // int length;
    while ((length = fis.read(data)) > 0) {
      dcos.write(data, 0, length);
    }
    fis.close();
    dcos.finish();
    dcos.close();
  }

希望本文所述對(duì)你有所幫助,java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)java可以繼續(xù)關(guān)注本站。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本人成年视频在线观看 | 国产一卡2卡3卡四卡精品网 | 日本激情网 | 青青草原在线免费 | 亚洲AV久久久噜噜噜久久 | 和直男装修工在工地啪 | 情欲满载2012美国dvd | 精品一区二区高清在线观看 | 动漫人物差差插曲漫画 | www免费插插视频 | youjizzxxx69日本 | 日本视频一区在线观看免费 | 人人揉人人爽五月天视频 | 日本中文字幕黑人借宿影片 | 91久久精品国产一区二区 | 丝袜足控免费网站xx动漫漫画 | 免费观看在线 | xxxxx性13一14| 国产一区二区三区高清 | 欧美视频一区二区三区在线观看 | 欧美视频一区二区三区四区 | 亚洲第一综合天堂另类专 | 国产精品午夜国产小视频 | 深夜影院深a久久 | 美女任你模 | 成人黄页网站 | 91制片厂制作果冻传媒破解 | 五月色婷婷久久综合 | 三级欧美在线 | 亚洲精品一二区 | 特黄特色大片免费视频播放 | 亚洲视频中文字幕 | 91精品婷婷国产综合久久8 | 免费在线观看伦理片 | 羞羞视频动漫 | 白丝打脚枪 | 桃乃木香在线 | 成人四虎 | 91进入蜜桃臀在线播放 | 色老板成人永久免费视频 | 四虎国产免费 |