Java GZIP 压缩字符串
在 Java 中,可以使用 java.util.zip 包中的类来压缩和解压缩字符串,以下是完整的示例代码:

压缩字符串为 GZIP 格式
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.GZIPOutputStream;
public class GzipStringCompressor {
/**
* 将字符串压缩为 GZIP 格式的字节数组
* @param str 要压缩的字符串
* @return 压缩后的字节数组
* @throws IOException 如果发生 I/O 错误
*/
public static byte[] compress(String str) throws IOException {
if (str == null || str.isEmpty()) {
return new byte[0];
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
gzipOutputStream.write(str.getBytes(StandardCharsets.UTF_8));
}
return byteArrayOutputStream.toByteArray();
}
/**
* 将压缩后的字节数组转换为 Base64 编码字符串(便于传输和存储)
* @param compressedBytes 压缩后的字节数组
* @return Base64 编码的字符串
*/
public static String toBase64(byte[] compressedBytes) {
return Base64.getEncoder().encodeToString(compressedBytes);
}
public static void main(String[] args) {
String originalString = "这是一个需要压缩的长字符串,可以包含很多内容...";
try {
// 压缩字符串
byte[] compressedBytes = compress(originalString);
System.out.println("原始字符串长度: " + originalString.length());
System.out.println("压缩后字节数组长度: " + compressedBytes.length);
// 转换为 Base64 字符串
String base64String = toBase64(compressedBytes);
System.out.println("Base64 编码后的字符串: " + base64String);
} catch (IOException e) {
e.printStackTrace();
}
}
}
解压缩 GZIP 格式的字符串
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
public class GzipStringDecompressor {
/**
* 从 Base64 编码的字符串解压缩
* @param base64String Base64 编码的压缩字符串
* @return 解压缩后的原始字符串
* @throws IOException 如果发生 I/O 错误
*/
public static String decompressFromBase64(String base64String) throws IOException {
if (base64String == null || base64String.isEmpty()) {
return "";
}
byte[] compressedBytes = Base64.getDecoder().decode(base64String);
return decompress(compressedBytes);
}
/**
* 从字节数组解压缩
* @param compressedBytes 压缩的字节数组
* @return 解压缩后的原始字符串
* @throws IOException 如果发生 I/O 错误
*/
public static String decompress(byte[] compressedBytes) throws IOException {
if (compressedBytes == null || compressedBytes.length == 0) {
return "";
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
byte[] buffer = new byte[1024];
int len;
while ((len = gzipInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, len);
}
}
return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());
}
public static void main(String[] args) {
String originalString = "这是一个需要压缩的长字符串,可以包含很多内容...";
try {
// 压缩字符串
byte[] compressedBytes = GzipStringCompressor.compress(originalString);
String base64String = GzipStringCompressor.toBase64(compressedBytes);
// 解压缩字符串
String decompressedString = decompressFromBase64(base64String);
System.out.println("解压缩后的字符串: " + decompressedString);
System.out.println("解压后是否与原始字符串一致: " + originalString.equals(decompressedString));
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用说明
-
压缩过程:
- 将字符串转换为字节数组(使用 UTF-8 编码)
- 使用
GZIPOutputStream将字节数组压缩 - 可选:将压缩后的字节数组转换为 Base64 字符串(便于传输和存储)
-
解压过程:
- 如果是 Base64 编码的字符串,先解码为字节数组
- 使用
GZIPInputStream将字节数组解压 - 将解压后的字节数组转换回字符串
-
注意事项:
- 确保使用相同的字符编码(示例中使用 UTF-8)
- 处理 I/O 异常
- 对于空字符串或 null 值需要特殊处理
- 压缩后的数据通常比原始数据小,但对于非常短的字符串可能会变大
这种方法适用于需要将字符串压缩后存储或传输的场景,如网络通信、文件存储等。


