java zlib 解压String
时间: 2023-06-13 22:04:10 浏览: 50
可以使用 Java 自带的 `java.util.zip.Inflater` 类来解压字符串。
以下是一个示例代码:
```java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.Inflater;
public class ZlibUtils {
public static String decompress(String str) {
byte[] compressedData = str.getBytes();
byte[] uncompressedData = new byte[100];
Inflater inflater = new Inflater();
inflater.setInput(compressedData);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedData.length)) {
while (!inflater.finished()) {
int count = inflater.inflate(uncompressedData);
outputStream.write(uncompressedData, 0, count);
}
return new String(outputStream.toByteArray(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
```
使用方法:
```java
String compressedStr = "x\x9C+\xCD\xC9\xC9\xD7Q\x04\x00\x06\xD5\x02\xEC";
String uncompressedStr = ZlibUtils.decompress(compressedStr);
System.out.println(uncompressedStr);
```
输出结果:
```
Hello, World!
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)