我使用下面的代码来压缩和解压缩字符串。但是在解压缩后,我得到了不同长度的字符串,解压缩字符串中也少了几个字符。
压缩:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream zos = new GZIPOutputStream(baos);
zos.write(text.getBytes());
zos.finish();
zos.flush();
byte[] udpBuffer = baos.toByteArray();
System.out.println("Compressed length: " + udpBuffer.length);减压:
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
BufferedReader br = new BufferedReader(new InputStreamReader(gis));
StringBuilder sb = new StringBuilder();
while (br.readLine()!= null) {
sb.append(br.readLine());
}
System.err.println(sb.toString());文本原始长度: 45627字节
文本压缩长度: 3732字节
文本未压缩长度: 20328字节(应等于原始长度)
我的原文是这样的:
<html>
<head>
<title></title>
</head>
<body>
<p><span class="preheader" style="display:none!important;mso-hide:all">Hey wazzup? </span></p>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="640">
<tbody>
<tr>
<td align="center" height="30" style="font-size:11px;color:#333;font-family:Verdana,Geneva,sans-serif">
.
.
.
</tbody>
</body>
</html>而我的解压缩文本就像(请参阅开始标记丢失了,还有
<title> tag, similarly many tags and other parts are missing from my uncompressed text:
<head></head><p><span class="preheader" style="display:none!important;mso-hide:all">
.
.
.有人能指出错误吗?或者这是预期的行为?
发布于 2014-04-07 19:02:39
变化
while (br.readLine()!= null) {
sb.append(br.readLine());
}至
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}此外,应该在zos.flush()之前调用zos.finish()。
https://stackoverflow.com/questions/22920592
复制相似问题