java压缩(gzip,Inflater,Deflater)⽂件与字符串压缩⽬标越⼤,压缩效果越明显!
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";
String comp = compress(issueIds);
String uncomp = uncompress(comp);
}
// 压缩
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.Bytes());
gzip.close();
String("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(
java stream
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ad(buffer)) >= 0) {
out.write(buffer, 0, n);
}
String();
}
public static byte[] encode(String srcString) throws UnsupportedEncodingException {
public static byte[] encode(String srcString) throws UnsupportedEncodingException { byte[] input = Bytes("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 {
}
}
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 (Remaining() == 0) {
flag = false;
}
} finally {
}
i++;
}
outputString = new String(result, 0, resultLength, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return outputString;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论