国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現zip,gzip,7z,zlib格式的壓縮打包

2019-11-26 13:41:55
字體:
來源:轉載
供稿:網友

本文主要介紹的是通過使用java的相關類可以實現對文件或文件夾的壓縮。

zlib是一種數據壓縮程序庫,它的設計目標是處理單純的數據(而不管數據的來源是什么)。

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

gzip是一種文件壓縮工具(或該壓縮工具產生的壓縮文件格式),它的設計目標是處理單個的文件。gzip在壓縮文件中的數據時使用的就是zlib。為了保存與文件屬性有關的信息,gzip需要在壓縮文件(*.gz)中保存更多的頭信息內容,而zlib不用考慮這一點。但gzip只適用于單個文件,所以我們在UNIX/Linux上經常看到的壓縮包后綴都是*.tar.gz或*.tgz,也就是先用tar把多個文件打包成單個文件,再用gzip壓縮的結果。

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

所以,你應當根據你的具體需求,選擇不同的壓縮技術:如果只需要壓縮/解壓縮數據,你可以直接用zlib實現,如果需要生成gzip格式的文件或解壓其他工具的壓縮結果,你就必須用gzip或zip等相關的類來處理了。

maven依賴
 

<dependency>      <groupId>org.apache.commons</groupId>      <artifactId>commons-compress</artifactId>      <version>1.12</version>    </dependency> 

zip格式

public static void zip(String input, String output, String name) throws Exception {    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output));    String[] paths = input.split("http://|");    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;      // 讀入需要下載的文件的內容,打包到zip文件      while ((len = fis.read(buffer)) > 0) {        out.write(buffer, 0, len);      }      out.closeEntry();      fis.close();    }    out.close();  }

gzip打包

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();  //設置壓縮文件里的文件名      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打包

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("http://|");      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打包

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();  }

希望本文所述對你有所幫助,java實現zip,gzip,7z,zlib格式的壓縮打包內容就給大家介紹到這里了。希望大家繼續關注我們的網站!想要學習java可以繼續關注本站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大埔县| 三门县| 临桂县| 鹤壁市| 绥宁县| 股票| 诸暨市| 东莞市| 淮南市| 晋宁县| 广东省| 台中市| 澎湖县| 股票| 大竹县| 凤凰县| 邹平县| 陆良县| 江安县| 五寨县| 达日县| 乐至县| 荆州市| 山西省| 巴彦县| 滦平县| 汉阴县| 嘉祥县| 岳普湖县| 仪陇县| 临湘市| 丘北县| 康保县| 松滋市| 安达市| 塘沽区| 宾阳县| 靖西县| 烟台市| 博客| 铅山县|