可以将Gzip字节数组转换为字符串吗?

huangapple go评论81阅读模式
英文:

is it possible to convert a Gzip byte array to String?

问题

我能够使用GZIP Java压缩方法压缩一个Base64字符串,因此可以在一个字符串中获取压缩后的值吗?

英文:

i am able to compress a base64 string areng the GZIP java compression method, so it is possible to get the compressed value in a st
ring?

答案1

得分: 1

可以使用以下方法将gzip解压缩为字符串:

public static String decompress(byte[] byteArray) throws Exception {
    if (byteArray == null ) {
        return null;
    }
    
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(byteArray));
    BufferedReader bfr = new BufferedReader(new InputStreamReader(gzis, "UTF-8"));
    String outputString = "";
    String line;
    while ((line=bfr.readLine())!=null) {
        outputString += line;
    }
    return outputString;
}
英文:

Yes, you can decompress gzip to string with this method.

public static String decompress(byte[] byteArray) throws Exception {
    if (byteArray == null ) {
        return null;
    }
    
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(byteArray));
    BufferedReader bfr = new BufferedReader(new InputStreamReader(gzis, "UTF-8"));
    String outputString = "";
    String line;
    while ((line=bfr.readLine())!=null) {
      outputString += line;
    }
    return outputString;
 }

huangapple
  • 本文由 发表于 2020年9月19日 23:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63970411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定