Java.util.zip.ZipException: excess dynamic bit lengths
I compress a string using gzip and then decompress the result, however I got the following exception: why?
output: Exception in thread "main" java.util.zip.ZipException: oversubscribed dynamic bit lengths tree at java.util.zip.InflaterInputStream.read (Unknown Source) at java.util.zip.GZIPInputStream.read (Unknown Source) at Test.main (Test.java:25)
public class Test {
public static void main(String[]args) throws IOException{
String s="helloworldthisisatestandtestonlydsafsdafdsfdsafadsfdsfsdfdsfdsfdsfdsfsadfdsfdasfassdfdfdsfdsdssdfdsfdsfd";
byte[]bs=s.getBytes();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(outstream);
gzipOut.write(bs);
gzipOut.finish();
String out=outstream.toString();
System.out.println(out);
System.out.println(out.length());
ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes());
GZIPInputStream gzipIn=new GZIPInputStream(in);
byte[]uncompressed = new byte[100000];
int len=10, offset=0, totalLen=0;
while((len = gzipIn.read(uncompressed, offset, len)) >0){ // this line
offset+=len;
totalLen+=len;
}
String uncompressedString=new String(uncompressed,0,totalLen);
System.out.println(uncompressedString);
}
}
+1
user157195
source
to share
1 answer
According to this page , the likely reason is that the ZIP file you are trying to read is corrupted. (I know this doesn't fit your circumstances ... but I'm pretty sure the exception message is indicative.)
In your case, the problem is that you are converting a gzip stream from a byte array to a string and then back to a byte array. This is almost certainly a lossy operation that corrupts the GZIP stream.
If you want to convert an arbitrary byte array to and from string form, you will need to use one of the string encoding formats for this purpose; for example base64.
Alternatively, just change this:
String out = outstream.toString();
ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes());
:
byte[] out = outstream.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(out);
+8
Stephen C
source
to share