本帖最后由 adamjy 于 2014-4-22 12:40 编辑
通过java原生API将字符串进行压缩和解压缩,报异常了(Unexpected end of ZLIB input stream)。
在解压缩函数的gunzip.read(buffer)出了问题,跟踪进去看,异常由InflaterInputStream类的fill函数抛出。
InflaterInputStream类的fill()方法代码如下,
- protected void fill() throws IOException {
- ensureOpen();
- len = in.read(buf, 0, buf.length);
- if (len == -1) {
- throw new EOFException("Unexpected end of ZLIB input stream");
- }
- inf.setInput(buf, 0, len);
- }
复制代码
压缩解压缩代码如下,
- public static String GetDeCompress(String src)
- {
- if (src == null || src.isEmpty()) {
- return src;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- GZIPInputStream gunzip = null;
- String des = null;
- try {
- ByteArrayInputStream in = new ByteArrayInputStream(
- new BASE64Decoder().decodeBuffer(src));
- gunzip = new GZIPInputStream(in);
- byte[] buffer = new byte[1024];
- int n;
- n = gunzip.read(buffer);
- while ((n = gunzip.read(buffer)) >= 0) {
- out.write(buffer, 0, n);
- }
- des = out.toString();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(gunzip!=null)
- {
- try {
- gunzip.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return des;
- }
复制代码 |
|