压缩目标越大,压缩效果越明显!
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class ByteTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String issueIds = "10068111,10068112,10068113,10068114,10068115,10068116,"
+ "10068117,10068118,10068119,10068120,10068121,10068122,10068123,"
+ "10068124,10068125,10068126,10068127,10068128,10068129,10068130,"
+ "10068131,10068132,10068133,10068134,10068135,10068136,10068137,"
+ "10068138,10068139,10068140,10068141,10068142,10068143,10068144,"
+ "10068145,10068146,10068147,10068148,10068149,10068150,10068151";
System.err.println("彩期数:" + (issueIds.split(",")).length);
System.err.println("源字符串长度:" + issueIds.length());
String comp = compress(issueIds);
System.err.println("压缩后字符串长度:" + comp.length());
String uncomp = uncompress(comp);
System.err.println("解压后字符串长度:" + uncomp.length() );
System.err.println("解压是否丢失:" + !issueIds.equals(uncomp));
}
// 压缩
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
// 解压缩
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(
str.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString();
}
public static byte[] encode(String srcString) throws UnsupportedEncodingException {
byte[] input = srcString.getBytes("UTF-8");
byte[] output = new byte[input.length * 2];
Deflater compresser = new Deflater();
try {
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
byte[] contentByte = new byte[compressedDataLength];
System.arraycopy(output, 0, contentByte, 0, compressedDataLength);
return contentByte;
} finally {
compresser.end();
}
}
public static String decode(byte[] encodeByte) {
String outputString = "";
try {
boolean flag = true;
int resultLength = 0;
byte[] result = null;
int i = 1;
while (flag) {
Inflater decompresser = new Inflater();
try {
result = new byte[encodeByte.length * 20 * i];
decompresser.setInput(encodeByte, 0, encodeByte.length);
resultLength = decompresser.inflate(result);
if (decompresser.getRemaining() == 0) {
flag = false;
}
} finally {
decompresser.end();
}
i++;
}
outputString = new String(result, 0, resultLength, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return outputString;
}
}
分享到:
相关推荐
两种方法 对字符串进行压缩和解压缩,inflater和deflater。
上面源码的文件,iteye要求要压缩 请您解压一下 博文链接:https://yangxiutian.iteye.com/blog/1659893
字符串的压缩和解压
tinyorm-plugin-boolean-inflater-deflater 待定 概要 待定 描述 待定 作者 moznion ( ) 执照 The MIT License (MIT) Copyright :copyright: 2015 moznion, http://moznion.net/ <moznion> Permission is hereby ...
转载的三种inflater的用法,主要是dialog、fragment会用到。
Java堆外内存泄露场景总结,包含几个常用的可能,如:JNI,NIO,AWT/Swing,Inflater&Deflater;
在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。这篇文章主要介绍了Android inflater 用法,需要的朋友可以参考下
Inflater Inflater is used to decompress data that has been compressed according to the "deflate" standard described in rfc1950. InflaterInputStream This filter stream is used to decompress data ...
NULL 博文链接:https://andli.iteye.com/blog/1181080
主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
JS找齐了,源码通过精易模块调用JS代码,内附JS原文件地址,需要的回复下载。
android LayouInflater 页面跳转 button点击
安卓Android单任务多线程任意断点下载【源码】,包括了一个可复用的文件下载服务类用于获取实时更新每条线程已经下载的文件长度,可实现多任务下载 1.创建XML文件,将要生成的View配置好 2.获取系统服务...
java反编译源码简单易用的 Android 反编译器 为什么 一站式商店 我已经厌倦了用一堆我必须一直记住的步骤来反编译 Android 应用程序。 涉及到大量的apktool、dex2jar、jd-gui; 它仍然让我感到困惑。 源文件的搭配 ...
LayoutInflater inflater = context.getLayoutInflater(); View view=inflater.inflate(R.layout.toast_info, null); 然后找到里面的控件,以单纯的textview为例 TextView txt=(TextView) vie
1.创建一个StaticFragment.java文件继承Fragment类和一个static_fragment.xml文件完成fragment的布局。在StaticFragment.java中重载onCreateView(……)方法,通过调用inflater.inflate(……)方法并传入布局资源ID...
用java实现的俄罗斯方块,实现基本功能,以及等级分数系统 操作: 左:a 右:d 下:s 暂停/开始:x 重玩:c
android layout_Gravity,Bundle,ContentResolver,cursor,Fragments ,Fragments,inflater ,Fragment,数据查询query函数参数解析, 【Cursor】 类的介绍,android 获得通话记录,Handler ,Android四大基本组件介绍与生命...