Java GZIP压缩与解压缩代码实例
作者:那些年的代码 时间:2023-11-20 15:57:17
这篇文章主要介绍了Java GZIP压缩与解压缩代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.GZIP压缩
public static byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch ( Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
2.GZIP解压缩
public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
3.工具代码集合
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPUtils {
public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";
public static byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch ( Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
public static byte[] compress(String str) throws IOException {
return compress(str, GZIP_ENCODE_UTF_8);
}
public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
public static String uncompressToString(byte[] bytes, String encoding) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(encoding);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String uncompressToString(byte[] bytes) {
return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
}
public static void main(String[] args) throws IOException {
String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println("字符串长度:"+s.length());
System.out.println("压缩后::"+compress(s).length);
System.out.println("解压后:"+uncompress(compress(s)).length);
System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());
}
}
来源:https://www.cnblogs.com/zhuyeshen/p/12160741.html
标签:java,GZIP,压缩
0
投稿
猜你喜欢
C语言程序设计50例(经典收藏)
2023-07-10 08:33:19
通过与Java功能上的对比来学习Go语言
2023-02-18 02:04:53
Java基础之关键字final详解
2022-01-28 00:55:31
UnityUI中绘制线状统计图
2022-12-03 14:30:43
Java正则之贪婪匹配、惰性匹配
2022-08-14 16:41:23
Java基础之详解HashSet的使用方法
2023-08-04 20:18:59
Java BigDecimal使用方法详解
2022-03-28 03:45:23
C#编写游戏客户端的实现代码
2021-08-28 06:24:58
java后端+前端使用WebSocket实现消息推送的详细流程
2022-04-24 12:34:53
Java多线程死锁示例
2022-09-17 15:05:25
vscode使用官方C/C++插件无法进行代码格式化问题
2022-07-13 06:24:47
Java Swing实现JTable检测单元格数据变更事件的方法示例
2022-10-16 19:49:29
详解Java如何实现一个像String一样不可变的类
2022-01-12 16:17:11
Spring Security OAuth过期的解决方法
2023-05-26 22:30:01
浅谈springboot一个service内组件的加载顺序
2023-12-10 12:47:12
Spring实战之SpEl语法实例详解
2023-09-18 07:56:03
openFeign服务之间调用保持请求头信息处理方式
2022-11-07 23:45:21
Android刮刮卡功能具体实现代码
2021-08-06 04:12:24
Android启动画面的实现方法
2023-12-27 23:22:11
Java中二叉树数据结构的实现示例
2023-08-07 03:10:58