溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Java怎么對字符串進行壓縮與解壓縮

發(fā)布時間:2021-03-05 14:50:22 來源:億速云 閱讀:504 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)使用Java怎么對字符串進行壓縮與解壓縮,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Java可以用來干什么

Java主要應(yīng)用于:1. web開發(fā);2. Android開發(fā);3. 客戶端開發(fā);4. 網(wǎng)頁開發(fā);5. 企業(yè)級應(yīng)用開發(fā);6. Java大數(shù)據(jù)開發(fā);7.游戲開發(fā)等。

方法一:用 Java8中的gzip

/**
 * 使用gzip壓縮字符串
 * @param str 要壓縮的字符串
 * @return
 */
public static String compress(String str) {
  if (str == null || str.length() == 0) {
    return str;
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  GZIPOutputStream gzip = null;
  try {
    gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (gzip != null) {
      try {
        gzip.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}
 
/**
 * 使用gzip解壓縮
 * @param compressedStr 壓縮字符串
 * @return
 */
public static String uncompress(String compressedStr) {
  if (compressedStr == null) {
    return null;
  }
 
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ByteArrayInputStream in = null;
  GZIPInputStream ginzip = null;
  byte[] compressed = null;
  String decompressed = null;
  try {
    compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
    in = new ByteArrayInputStream(compressed);
    ginzip = new GZIPInputStream(in);
    byte[] buffer = new byte[1024];
    int offset = -1;
    while ((offset = ginzip.read(buffer)) != -1) {
      out.write(buffer, 0, offset);
    }
    decompressed = out.toString();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (ginzip != null) {
      try {
        ginzip.close();
      } catch (IOException e) {
      }
    }
    if (in != null) {
      try {
        in.close();
      } catch (IOException e) {
      }
    }
    if (out != null) {
      try {
        out.close();
      } catch (IOException e) {
      }
    }
  }
  return decompressed;
}

方法二:用org.apache.commons.codec.binary.Base64

/**
 * 使用org.apache.commons.codec.binary.Base64壓縮字符串
 * @param str 要壓縮的字符串
 * @return
 */
public static String compress(String str) {
  if (str == null || str.length() == 0) {
    return str;
  }
  return Base64.encodeBase64String(str.getBytes());
}
 
/**
 * 使用org.apache.commons.codec.binary.Base64解壓縮
 * @param compressedStr 壓縮字符串
 * @return
 */
public static String uncompress(String compressedStr) {
  if (compressedStr == null) {
    return null;
  }
  return Base64.decodeBase64(compressedStr);
}

注意事項

在web項目中,服務(wù)器端將加密后的字符串返回給前端,前端再通過ajax請求將加密字符串發(fā)送給服務(wù)器端處理的時候,在http傳輸過程中會改變加密字符串的內(nèi)容,導(dǎo)致服務(wù)器解壓壓縮字符串發(fā)生異常:

java.util.zip.ZipException: Not in GZIP format

解決方法:

在字符串壓縮之后,將壓縮后的字符串BASE64加密,在使用的時候先BASE64解密再解壓即可。

以上就是使用Java怎么對字符串進行壓縮與解壓縮,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI