开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 1 天,点击查看活动详情
1、MySQL自带函数compress,uncompress
-
数据库压缩的话需要牺牲部分cpu运算能力
-
字段越大,压缩率越高
SELECT COMPRESS(b.text) from business b WHERE b.businessId=1;
2、服务端代码压缩字符串
- Gzip:压缩率高 GzipInputStream
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@Slf4j
public class GZipUtil {
public static byte[] compress(String str) {
if (StringUtils.isEmpty(str)) return null;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out)) {
gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close();
return out.toByteArray();
} catch (Exception e) {
log.error("gzip压缩失败", e);
return new byte[]{};
}
}
public static String uncompress(byte[] bytes) {
if (Objects.isNull(bytes)) return null;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes)){
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return new String(out.toByteArray(), StandardCharsets.UTF_8);
} catch (Exception e) {
log.error("gzip解压失败", e);
return new String();
}
}
public static void main(String[] args) {
String org = "selct **";
byte[] encode = GZipUtil.compress(org);
System.out.println("压缩前字符串:" + org);
System.out.println("压缩前长度:" + org.length());
System.out.println("压缩后长度:" + encode.length);
System.out.println("解压后字符串:" + GZipUtil.uncompress(encode));
}
}