I have some code to uncompress gzip a compressedString as below:
public static String decompress(String compressedString) throws IOException {
byte[] byteCompressed = compressedString.getBytes(StandardCharsets.UTF_8)
final StringBuilder outStr = new StringBuilder();
if ((byteCompressed == null) || (byteCompressed.length == 0)) {
return "";
}
if (isCompressed(byteCompressed)) {
final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(byteCompressed));
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
outStr.append(line);
}
} else {
outStr.append(byteCompressed);
}
return outStr.toString();
}
public static boolean isCompressed(final byte[] compressed) {
return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}
I use this code to uncompress a String as below: H4sIAAAAAAAAAHNJLQtJLS4BALwLiloHAAAA
But this code uncompress a unexpected String although I can uncompress online normally in the web
Anyone can help me give the right uncompress code? Thanks
Your string is base64 encoded gzip data, so you need to base64 decode it, instead of trying to encode it as UTF-8 bytes.
String input = "H4sIAAAAAAAAAHNJLQtJLS4BALwLiloHAAAA";
byte[] byteCompressed = Base64.getDecoder().decode(input);
// ... rest of your code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.