6 Replies Latest reply: Jan 21, 2009 7:18 AM by JoachimSauer RSS

    java.util.zip.ZipException: invalid bit length repeat EXCEPTION.

    807588
      My requirement is I need to compress a Big String and send it from server to Client. At the client end I need to decompress it back to get the original String

      The size of my compressed String is 30447

      When I uncompress it. I get this error


      java.util.zip.ZipException: invalid bit length repeat

      The following below is the code used for Compressing and Uncompressing a String._
      private static void decompressString(byte[] baFileContentCompressed) {
                ByteArrayOutputStream baos;
                ByteArrayInputStream bais = new ByteArrayInputStream(
                          baFileContentCompressed);
                GZIPInputStream zis = null;
                byte[] buffer = new byte[8192];
                // the result
                byte[] baFileContentDecompressed = null;
      
                baos = new ByteArrayOutputStream();
                try {
      
                     buffer = new byte[1024];
                     try {
                          zis = new GZIPInputStream(bais);
                          for (int len; (len = zis.read(buffer, 0, 1024)) != -1;) {
                               baos.write(buffer, 0, len);
                          }
      
                          zis.close();
                          bais.close();
                          baos.close();
                          baFileContentDecompressed = baos.toByteArray();
                     } catch (IOException e) {
                          e.printStackTrace();
                     }
      
                     System.out.println(new String(baFileContentDecompressed));
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }
      
           private static byte[] compressInputString(String testData) {
                byte[] baFileContent = testData.getBytes();
      
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                GZIPOutputStream zos;
                try {
                     zos = new GZIPOutputStream(baos);
                     zos.write(baFileContent);
                     zos.close();
                     baos.close();
                } catch (IOException e) {
                     e.printStackTrace();
                }
                byte[] baFileContentCompressed = baos.toByteArray();
                return baFileContentCompressed;
           }
      The exception is thrown when
      len = zis.read(buffer, 0, 1024)
      is invoked

      Need some help here.

      Regards,
      Bob

      Edited by: hemanthjava on Jul 2, 2008 6:21 AM
        • 1. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
          807588
          There is probably something wrong between your sending and receiving. As a test, try compressing and decompressing the same string on the server.
          • 2. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
            807588
            hemanthjava wrote:
            My requirement is I need to compress a Big String and send it from server to Client. At the client end I need to decompress it back to get the original String

            The size of my compressed String is 30447

            When I uncompress it. I get this error


            java.util.zip.ZipException: invalid bit length repeat
            As far as I understand, you have alredy tested compress and decompress functions on localhost. I mean just try to make a *.zip archive on the same PC and the unpack it and compare strings. But once again, I suppose you have alredy done it, so let look at fucntions for sending and reciving?
            • 3. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
              807588
              I have tested your compress-decompress methods on local and they are doing fine.

              I think the problem is at your sending or receiving code.
              How do you send the objects through network?
              sharing code from that part may help.
              • 4. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
                807588
                Well, I know this is an old post, but I'm having the same problem and I'm sure that's about sending-receiving method (as fmucar said); in fact I have tested on local and it works. This is the bean I use for uploading files to a tomcat server:
                public class FileUploadBean {
                
                  private String  filepath, filename, contentType;
                  private Dictionary fields;
                
                  public  String savePath="c:\\";
                  
                  public String getFilename() {
                    return filename;
                  }
                
                  public String getFilepath() {
                    return filepath;
                  }
                
                  public void setSavePath(String path) {
                    this.savePath = path;
                  }
                
                  public String getContentType() {
                    return contentType;
                  }
                
                  public String getFieldValue(String fieldName) {
                    if (fields == null || fieldName == null)
                      return null;
                    return (String) fields.get(fieldName);
                  }
                
                  private void setFilename(String s) {
                    if (s==null)
                      return;
                
                    int pos = s.indexOf("filename=\"");
                    if (pos != -1) {
                      filepath = s.substring(pos+10, s.length()-1);
                      // Windows browsers include the full path on the client
                      // But Linux/Unix and Mac browsers only send the filename
                      // test if this is from a Windows browser
                      pos = filepath.lastIndexOf("\\");
                      if (pos != -1)
                        filename = filepath.substring(pos + 1);
                      else
                        filename = filepath;
                    }
                  }
                private void setContentType(String s) {
                    if (s==null)
                      return;
                
                    int pos = s.indexOf(": ");
                    if (pos != -1)
                      contentType = s.substring(pos+2, s.length());
                  }
                
                  public void doUpload(HttpServletRequest request) throws IOException {
                    ServletInputStream in = request.getInputStream();
                
                    byte[] line = new byte[128];
                    int i = in.readLine(line, 0, 128);
                    if (i < 3)
                      return;
                    int boundaryLength = i - 2;
                
                    String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
                    fields = new Hashtable();
                
                    while (i != -1) {
                      String newLine = new String(line, 0, i);
                      if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
                        if (newLine.indexOf("filename=\"") != -1) {
                          setFilename(new String(line, 0, i-2));
                          if (filename==null)
                            return;
                          //this is the file content
                          i = in.readLine(line, 0, 128);
                          setContentType(new String(line, 0, i-2));
                          i = in.readLine(line, 0, 128);
                          // blank line
                          i = in.readLine(line, 0, 128);
                          newLine = new String(line, 0, i);
                          PrintWriter pw = new PrintWriter(new BufferedWriter(new
                            FileWriter((savePath==null? "" : savePath) + filename)));
                          while (i != -1 && !newLine.startsWith(boundary)) {
                            // the problem is the last line of the file content
                            // contains the new line character.
                            // So, we need to check if the current line is
                            // the last line.
                            i = in.readLine(line, 0, 128);
                            if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
                              && (new String(line, 0, i).startsWith(boundary)))
                              pw.print(newLine.substring(0, newLine.length()-2));
                            else
                              pw.print(newLine);
                            newLine = new String(line, 0, i);
                
                          }
                          pw.close();
                
                        }
                        else {
                          //this is a field
                          // get the field name
                          int pos = newLine.indexOf("name=\"");
                          String fieldName = newLine.substring(pos+6, newLine.length()-3);
                          //System.out.println("fieldName:" + fieldName);
                          // blank line
                          i = in.readLine(line, 0, 128);
                          i = in.readLine(line, 0, 128);
                          newLine = new String(line, 0, i);
                          StringBuffer fieldValue = new StringBuffer(128);
                          while (i != -1 && !newLine.startsWith(boundary)) {
                            // The last line of the field
                            // contains the new line character.
                            // So, we need to check if the current line is
                            // the last line.
                            i = in.readLine(line, 0, 128);
                            if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
                              && (new String(line, 0, i).startsWith(boundary)))
                              fieldValue.append(newLine.substring(0, newLine.length()-2));
                            else
                              fieldValue.append(newLine);
                            newLine = new String(line, 0, i);
                          }
                          //System.out.println("fieldValue:" + fieldValue.toString());
                          fields.put(fieldName, fieldValue.toString());
                        }
                      }
                      i = in.readLine(line, 0, 128);
                
                    } // end while
                  }
                
                
                
                }
                The uploaded file throws the exception when processed, but the original file works properly.
                Any advice? many thanks
                • 5. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
                  807588
                  I resolved using jakarta commons fileupload. The problem is surely in the class I posted. I hope this will help.
                  • 6. Re: java.util.zip.ZipException: invalid bit length repeat EXCEPTION.
                    JoachimSauer
                    I see "new String(someBinaryData)" in your code

                    That's clearly a recipe for distaster. String data (i.e. String and char[]) and binary data (byte[]) are two completely separate worlds in Java and should not be mixed (unless you know very well what you do).

                    If you use String, char[], any Reader or any Writer anywhere near your data, you can be pretty sure that you have a bug.

                    Only let byte[], InputStream and OutputStream near your data and you should be fine.