首页 / JAVA / java 压缩与解压
java 压缩与解压
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 压缩与解压,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2230字,纯文字阅读大概需要4分钟。
内容图文
![java 压缩与解压](/upload/InfoBanner/zyjiaocheng/740/cabcf2b41ee74e90bf522cc11675b237.jpg)
/**
* 使用gzip进行压缩
*/
public static String gzip(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip=null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(primStr.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(gzip!=null){
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}
/**
*
* <p>Description:使用gzip进行解压缩</p>
* @param compressedStr
* @return
*/
public static String gunzip(String compressedStr){
if(compressedStr==null){
return null;
}
ByteArrayOutputStream out= new ByteArrayOutputStream();
ByteArrayInputStream in=null;
GZIPInputStream ginzip=null;
byte[] compressed=null;
String decompressed = null;
try {
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in=new ByteArrayInputStream(compressed);
ginzip=new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed=out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ginzip != null) {
try {
ginzip.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}
内容总结
以上是互联网集市为您收集整理的java 压缩与解压全部内容,希望文章能够帮你解决java 压缩与解压所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。