java gzip 解压 字符串
时间: 2023-08-10 22:04:15 浏览: 48
你可以使用 Java 的 GZIPInputStream 类来解压缩字符串。以下是一个示例代码:
```java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
public class GzipUtils {
public static String decompress(byte[] compressedData) throws IOException {
InputStream inputStream = new ByteArrayInputStream(compressedData);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
byte[] buffer = new byte[1024];
int length;
while ((length = gzipInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
gzipInputStream.close();
outputStream.close();
return outputStream.toString("UTF-8");
}
}
```
你可以使用以下代码来调用它:
```java
String compressedString = "H4sIAAAAAAAA...";
byte[] compressedData = Base64.getDecoder().decode(compressedString);
String uncompressedString = GzipUtils.decompress(compressedData);
System.out.println(uncompressedString);
```
其中 `compressedString` 是经过 GZIP 压缩后的字符串,你需要先将其解码成字节数组,然后调用 `decompress` 方法来解压缩。