欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java GZIP壓縮與解壓縮代碼實(shí)例

 更新時(shí)間:2020年01月09日 14:32:21   作者:那些年的代碼  
這篇文章主要介紹了Java GZIP壓縮與解壓縮代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Java GZIP壓縮與解壓縮代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.GZIP壓縮

public static byte[] compress(String str, String encoding) {
    if (str == null || str.length() == 0) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
      gzip = new GZIPOutputStream(out);
      gzip.write(str.getBytes(encoding));
      gzip.close();
    } catch ( Exception e) {
      e.printStackTrace();
    }
    return out.toByteArray();
  }

2.GZIP解壓縮

public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
      GZIPInputStream ungzip = new GZIPInputStream(in);
      byte[] buffer = new byte[256];
      int n;
      while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return out.toByteArray();
  }

3.工具代碼集合

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtils {
  public static final String GZIP_ENCODE_UTF_8 = "UTF-8"; 
  public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

  
  public static byte[] compress(String str, String encoding) {
    if (str == null || str.length() == 0) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
      gzip = new GZIPOutputStream(out);
      gzip.write(str.getBytes(encoding));
      gzip.close();
    } catch ( Exception e) {
      e.printStackTrace();
    }
    return out.toByteArray();
  }
  
  public static byte[] compress(String str) throws IOException { 
    return compress(str, GZIP_ENCODE_UTF_8); 
  }
  
  public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
      GZIPInputStream ungzip = new GZIPInputStream(in);
      byte[] buffer = new byte[256];
      int n;
      while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return out.toByteArray();
  }
  
  public static String uncompressToString(byte[] bytes, String encoding) { 
    if (bytes == null || bytes.length == 0) { 
      return null; 
    } 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    ByteArrayInputStream in = new ByteArrayInputStream(bytes); 
    try {
      GZIPInputStream ungzip = new GZIPInputStream(in); 
      byte[] buffer = new byte[256]; 
      int n; 
      while ((n = ungzip.read(buffer)) >= 0) { 
        out.write(buffer, 0, n); 
      } 
      return out.toString(encoding);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  
  public static String uncompressToString(byte[] bytes) { 
    return uncompressToString(bytes, GZIP_ENCODE_UTF_8); 
  } 
  
  public static void main(String[] args) throws IOException {
    String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    System.out.println("字符串長(zhǎng)度:"+s.length());
    System.out.println("壓縮后::"+compress(s).length);
    System.out.println("解壓后:"+uncompress(compress(s)).length);
    System.out.println("解壓字符串后::"+uncompressToString(compress(s)).length());
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于java Springboot實(shí)現(xiàn)教務(wù)管理系統(tǒng)詳解

    基于java Springboot實(shí)現(xiàn)教務(wù)管理系統(tǒng)詳解

    這篇文章主要介紹了Java 實(shí)現(xiàn)簡(jiǎn)易教務(wù)管理系統(tǒng)的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • IDEA部署jeesite3完美運(yùn)行教程詳解

    IDEA部署jeesite3完美運(yùn)行教程詳解

    這篇文章主要介紹了IDEA部署jeesite3完美運(yùn)行教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java集合框架 arrayblockingqueue應(yīng)用分析

    java集合框架 arrayblockingqueue應(yīng)用分析

    ArrayBlockingQueue是一個(gè)由數(shù)組支持的有界阻塞隊(duì)列。此隊(duì)列按 FIFO(先進(jìn)先出)原則對(duì)元素進(jìn)行排序。隊(duì)列的頭部 是在隊(duì)列中存在時(shí)間最長(zhǎng)的元素
    2012-11-11
  • Spring?Security權(quán)限管理小結(jié)

    Spring?Security權(quán)限管理小結(jié)

    SpringSecurity是一個(gè)權(quán)限管理框架,核心是認(rèn)證和授權(quán),前面已經(jīng)系統(tǒng)的給大家介紹過了認(rèn)證的實(shí)現(xiàn)和源碼分析,本文重點(diǎn)來介紹下權(quán)限管理,需要的朋友可以參考下
    2022-08-08
  • java基于TCP協(xié)議實(shí)現(xiàn)聊天程序

    java基于TCP協(xié)議實(shí)現(xiàn)聊天程序

    這篇文章主要為大家詳細(xì)介紹了java基于TCP協(xié)議實(shí)現(xiàn)聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • JVM加載class文件的原理機(jī)制實(shí)例詳解

    JVM加載class文件的原理機(jī)制實(shí)例詳解

    Java是一種具有動(dòng)態(tài)性的解釋型語(yǔ)言,類(class)只有被加載到JVM后才能運(yùn)行,接下來通過本文給大家介紹JVM加載class文件的原理機(jī)制詳解,感興趣的朋友一起看看吧
    2022-04-04
  • Java自旋鎖的實(shí)現(xiàn)示例

    Java自旋鎖的實(shí)現(xiàn)示例

    自旋鎖是一種特殊的鎖,用于解決多線程同步問題,本文主要介紹了Java自旋鎖的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Java web入門指南之在Idea上創(chuàng)建Java web項(xiàng)目

    Java web入門指南之在Idea上創(chuàng)建Java web項(xiàng)目

    好多書上的JavaWeb教程都是Eclipse以及MyEclipse,當(dāng)然這里不論IDE的好壞,下面這篇文章主要給大家介紹了關(guān)于Java web入門指南之在Idea上創(chuàng)建Java web項(xiàng)目的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • synchronized底層實(shí)現(xiàn)原理

    synchronized底層實(shí)現(xiàn)原理

    這篇文章主要介紹了synchronized底層實(shí)現(xiàn)原理,想弄懂它的實(shí)現(xiàn)synchronized的原理,我們只能通過看編譯好的字節(jié)碼文件,下面文章的詳細(xì)內(nèi)容,我們就先從測(cè)試類開始吧,需要的小伙伴可以參考一下
    2022-01-01
  • SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼

    SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot中發(fā)送QQ郵件功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評(píng)論