一、应用场景
在一些开发应用场景中
1.比如有网络传输大文本、文件,如果数据体积太大,传输的效率会下降,请求响应速度变慢
2.文本需要记录在Redis缓存中,数据量大,会占用我们的内存,对服务器造成压力。
此时,就需要对数据进行压缩存储、传输。
二、工具类简介
GZIP 是JDK类库下java.util.zip的工具类,GZIPOutputStream 和 GZIPInputStream用于创建和读取GZIP格式的压缩文件。
简单讲就是将数据压缩成我们常见的 .zip文件。
这里我还提供了一种压缩文本的工具类(代码实现-2),即文本压缩后还是文本,只是长度压缩了,是基于Base64进行压缩的。亲测效果,大概是原文本长度的10倍,不一定准确,数据量和字符类型没有做海量测试分析对比,但是还是值得使用的,毕竟开发成本非常低。
三、代码实现
以下是我封装的一个工具类,提供给大家做一个参考。
1.压缩解压文件
首先创建一个Maven工程,引用JDK1.8。引入依赖,压缩解压本身不需要用,是JDK原生的工具类,这里的依赖是Base64需要用到。
复制一个文本和图片,到resources下
到这里准备工作就完成了,下面是工具类源码
/**
* 压缩
* */
public static void compress(String filePath, String compressFilePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(compressFilePath);
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis)) {
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
gzipOS.write(buffer, 0, len);
}
}
}
/**
* 解压
* */
public static void decompress(String compressFilePath, String decompressFilePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(decompressFilePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
FileInputStream fis = new FileInputStream(compressFilePath);
GZIPInputStream gzipIS = new GZIPInputStream(fis)) {
byte[] buffer = new byte[1024];
int len;
while ((len = gzipIS.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
}
}
/**
* 测试
* */
public static void main(String[] args) {
// 项目根路径
URL rootPath = GzipExample.class.getResource("/");
if (rootPath == null) {
System.out.println("根路径不存在");
return;
}
String sourceFileName = "myText.txt";//原文本名
String compressedFileName = "myText_compress.gz";//压缩后的文件名
String decompressedFileName = "myText_depressed.txt";//解压后的文件名
// String sourceFileName = "photo1.jpeg";
// String compressedFileName = "photo1_compressed.gz";
// String decompressedFileName = "photo1_depressed.jpeg";
String filePath = rootPath.getPath() + sourceFileName; // 原文件路径
String compressFilePath = rootPath.getPath() + compressedFileName; // 压缩后文件路径
String decompressFilePath = rootPath.getPath() + decompressedFileName; // 解压后文件路径
try {
// 压缩文件
compress(filePath, compressFilePath);
System.out.println("压缩完成");
// 解压文件
decompress(compressFilePath, decompressFilePath);
System.out.println("解压完成");
} catch (IOException e) {
e.printStackTrace();
}
}
运行结果如下
如果有发现文件/目录找不到的情况,请检查自己的文件路径是否填对,以及是否编译到class文件
2.压缩解压文本
注意这里的压缩、解压,输入输出都是字符串类型,只是长度被压缩了
/**
* 压缩文本
* */
public static String compress(String str) {
if (str == null || str.isEmpty()) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
System.out.println("GZipUtils.compress error" + e);
} finally {
if (gzip != null) {
try {
gzip.close();
} catch (Exception e) {
System.out.println("GZipUtils.compress gzip close error" +
e);
}
}
}
return new String(Base64.encodeBase64(out.toByteArray()), StandardCharsets.UTF_8);
}
/**
* 解压文本
* */
public static String decompress(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
GZIPInputStream gZipIns = null;
String decompressedStr = null;
try {
out = new ByteArrayOutputStream();
byte[] compressed = Base64.decodeBase64(compressedStr);
in = new ByteArrayInputStream(compressed);
gZipIns = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset;
while ((offset = gZipIns.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressedStr = out.toString();
} catch (Exception e) {
System.out.println("GZipUtils.decompress error" + e);
} finally {
if (gZipIns != null) {
try {
gZipIns.close();
} catch (Exception e) {
System.out.println("GZipUtils.decompress gZipIns close error" + e);
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
System.out.println("GZipUtils.decompress in close error" + e);
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
System.out.println("GZipUtils.decompress out close error" + e);
}
}
}
return decompressedStr;
}
/**
* 测试
* */
public static void main(String[] args) {
//需要压缩的文本
String sourceStr = "This is the data to compress zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" +
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
System.out.println("原文本:");
System.out.println(sourceStr);
System.out.println("压缩前的原文本长度: " + sourceStr.getBytes().length);
String compressedData = compress(sourceStr);
System.out.println("压缩后长度: " + compressedData.getBytes().length + "\n");
String decompressedData = decompress(compressedData);
System.out.println("解压后长度: " + decompressedData.getBytes().length);
System.out.println("还原后的文本:");
System.out.println(decompressedData);
}
运行结果如下,这里我们看到压缩后,文本降低到将近原来的10倍,效果还是比较显著的。
=========================================================================创作不易,请勿直接盗用,使用请标明转载出处。
喜欢的话,一键三连,您的支持是我一直坚持高质量创作的原动力。