Created
September 24, 2011 13:09
-
-
Save ggd543/1239312 to your computer and use it in GitHub Desktop.
java.io.EOFException: Unexpected end of ZLIB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.travelsky.esb.emd.utils; | |
import org.mule.util.IOUtils; | |
import java.io.*; | |
import java.util.zip.GZIPInputStream; | |
import java.util.zip.GZIPOutputStream; | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Administrator | |
* Date: 11-9-20 | |
* Time: 下午4:16 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class IO { | |
private static String DEFAULT_CHARSET = "utf-8"; | |
public static byte[] gZipCompress(String source) throws IOException { | |
return gZipCompress(source, DEFAULT_CHARSET); | |
} | |
public static byte[] gZipCompress(String source, String charset) throws IOException { | |
return gZipCompress(source.getBytes(charset)); | |
} | |
public static byte[] gZipCompress(byte[] bytes) throws IOException { | |
return gZipCompress(new ByteArrayInputStream(bytes)); | |
} | |
public static byte[] gZipCompress(InputStream is) throws IOException { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
GZIPOutputStream gzip_os = new GZIPOutputStream(bos); | |
// IOUtils.copy(is, gzip_os); | |
copy(is, gzip_os); | |
byte[] compressedBytes = bos.toByteArray(); | |
is.close(); | |
gzip_os.close(); | |
return compressedBytes; | |
} | |
public static byte[] gZipUncompress(String s) throws IOException { | |
return gZipUncompress(s, DEFAULT_CHARSET); | |
} | |
public static byte[] gZipUncompress(String s, String charset) throws IOException { | |
return gZipUncompress(s.getBytes(charset)); | |
} | |
public static byte[] gZipUncompress(byte[] bytes) throws IOException { | |
return gZipUncompress(new ByteArrayInputStream(bytes)); | |
} | |
public static byte[] gZipUncompress(InputStream is) throws IOException { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
InputStream gzip_is = new GZIPInputStream(is); | |
// IOUtils.copy(gzip_is, bos); // Refer to http://hi.baidu.com/bobylou/blog/item/1f8600e91de9ee3cb90e2de6.html | |
copy(gzip_is,bos); | |
byte[] bytes = bos.toByteArray(); | |
gzip_is.close(); | |
bos.close(); | |
return bytes; | |
} | |
private static final int BYTE_BLOCK_LENGTH = 1024; | |
/** | |
* The input and output stream won't be closed after the method invocation | |
* | |
* @param is | |
* @param os | |
*/ | |
public static void copy(InputStream is, OutputStream os) throws IOException { | |
byte[] buf = new byte[BYTE_BLOCK_LENGTH]; | |
int len = -1; | |
while ((len = is.read(buf, 0, buf.length)) != -1) { | |
os.write(buf, 0, len); | |
} | |
} | |
public static String read(InputStream is, String encoding) throws IOException { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTE_BLOCK_LENGTH); | |
copy(is, baos); | |
String res = new String(baos.toByteArray(), encoding); | |
is.close();; | |
baos.close(); | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.travelsky.esb.emd.utils | |
import java.lang.String | |
import org.junit.{Ignore, Assert, Test} | |
/** | |
* Created by IntelliJ IDEA. | |
* User: ggd543 | |
* Date: 11-9-22 | |
* Time: 下午3:48 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
class IOTest { | |
@Ignore | |
@Test | |
def testGZipCompressAndUncompress() { | |
try { | |
val str = "I want to go home"; | |
println("compress this string : " + str) | |
val bytes = str.getBytes(); | |
val compressedBytes = IO.gZipCompress(bytes) | |
val compressedStr = new String(compressedBytes) | |
println("The length of original byte array : " + bytes.length) | |
println("The length of compressed byte array : " + compressedBytes.length) | |
println("The length of original string : " + str.length) | |
println("The length of compressed string : " + compressedStr.length) | |
Assert.assertTrue(bytes.length >= compressedBytes.length) | |
val uncompressedBytes = IO.gZipUncompress(compressedBytes); | |
val uncompressedStr = new String(uncompressedBytes); | |
println("The length of uncompress byte array : " + uncompressedBytes.length) | |
println("The uncompress string : " + uncompressedStr) | |
Assert.assertTrue(compressedBytes.length <= uncompressedBytes.length) | |
Assert.assertEquals(str, uncompressedStr) | |
} catch { | |
case e: Exception => e.printStackTrace(); Assert.fail(); | |
} | |
} | |
} |
I have found the solution :
Make sure to flush the compressed bytes into array by using the finish()
or close()
method of java.util.zip.GZIPOutputStream
, otherwise you will lose some bytes and get java.io.EOFException: Unexpected end of ZLIB input stream
error when decompressing .
I have discussed an interesting phenomemon that the length of compressed bytes array is larger than the original one when the length of the string to be compressed is too small. That is because a gzip header will be added to the compressed bytes array. There is no need to use gzip algorithm if the string is short
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compress this string : I want to go home
The length of original byte array : 17
The length of compressed byte array : 10
The length of original string : 17
The length of compressed string : 10
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:223)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:141)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:92)
at com.travelsky.esb.emd.utils.IO.copy(IO.java:78)
at com.travelsky.esb.emd.utils.IO.gZipUncompress(IO.java:61)
at com.travelsky.esb.emd.utils.IO.gZipUncompress(IO.java:53)
at com.travelsky.esb.emd.utils.IOTest.testGZipCompressAndUncompress(IOTest.scala:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at com.intellij.junit4.JUnit4TestRunnerUtil$IgnoreIgnoredTestJUnit4ClassRunner.runChild(JUnit4TestRunnerUtil.java:211)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.fail(Assert.java:98)
at com.travelsky.esb.emd.utils.IOTest.testGZipCompressAndUncompress(IOTest.scala:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at com.intellij.junit4.JUnit4TestRunnerUtil$IgnoreIgnoredTestJUnit4ClassRunner.runChild(JUnit4TestRunnerUtil.java:211)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)