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

首頁(yè) > 編程 > Java > 正文

JAVA中的deflate壓縮實(shí)現(xiàn)方法

2019-11-26 13:54:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在文件的傳輸過(guò)程中,為了使大文件能夠更加方便快速的傳輸,一般采用壓縮的辦法來(lái)對(duì)文件壓縮后再傳輸,JAVA中的java.util.zip包中的Deflater和Inflater類(lèi)為使用者提供了DEFLATE算法的壓縮功能,以下是自已編寫(xiě)的壓縮和解壓縮實(shí)現(xiàn),并以壓縮文件內(nèi)容為例說(shuō)明,其中涉及的具體方法可查看JDK的API了解說(shuō)明。

/**   *    * @param inputByte   *      待解壓縮的字節(jié)數(shù)組   * @return 解壓縮后的字節(jié)數(shù)組   * @throws IOException   */  public static byte[] uncompress(byte[] inputByte) throws IOException {    int len = 0;    Inflater infl = new Inflater();    infl.setInput(inputByte);    ByteArrayOutputStream bos = new ByteArrayOutputStream();    byte[] outByte = new byte[1024];    try {      while (!infl.finished()) {        // 解壓縮并將解壓縮后的內(nèi)容輸出到字節(jié)輸出流bos中        len = infl.inflate(outByte);        if (len == 0) {          break;        }        bos.write(outByte, 0, len);      }      infl.end();    } catch (Exception e) {      //    } finally {      bos.close();    }    return bos.toByteArray();  }  /**   * 壓縮.   *    * @param inputByte   *      待壓縮的字節(jié)數(shù)組   * @return 壓縮后的數(shù)據(jù)   * @throws IOException   */  public static byte[] compress(byte[] inputByte) throws IOException {    int len = 0;    Deflater defl = new Deflater();    defl.setInput(inputByte);    defl.finish();    ByteArrayOutputStream bos = new ByteArrayOutputStream();    byte[] outputByte = new byte[1024];    try {      while (!defl.finished()) {        // 壓縮并將壓縮后的內(nèi)容輸出到字節(jié)輸出流bos中        len = defl.deflate(outputByte);        bos.write(outputByte, 0, len);      }      defl.end();    } finally {      bos.close();    }    return bos.toByteArray();  }  public static void main(String[] args) {    try {      FileInputStream fis = new FileInputStream("D://testdeflate.txt");      int len = fis.available();      byte[] b = new byte[len];      fis.read(b);      byte[] bd = compress(b);      // 為了壓縮后的內(nèi)容能夠在網(wǎng)絡(luò)上傳輸,一般采用Base64編碼      String encodestr = Base64.encodeBase64String(bd);      byte[] bi = uncompress(Base64.decodeBase64(encodestr));      FileOutputStream fos = new FileOutputStream("D://testinflate.txt");      fos.write(bi);      fos.flush();      fos.close();      fis.close();    } catch (Exception e) {      //    }  }

以上這篇JAVA中的deflate壓縮實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍邱县| 镇宁| 洪江市| 临泉县| 兴山县| 长宁区| 铜梁县| 东阳市| 普兰店市| 彩票| 静乐县| 瑞丽市| 房山区| 南充市| 江山市| 禹州市| 甘泉县| 梧州市| 奇台县| 登封市| 长武县| 东阳市| 利辛县| 池州市| 钟山县| 沙田区| 新乡市| 河间市| 隆昌县| 芷江| 贡觉县| 囊谦县| 镇江市| 安泽县| 天水市| 大竹县| 闽侯县| 修文县| 嵊泗县| 明溪县| 三亚市|