6 Replies Latest reply: Jun 12, 2008 8:23 AM by 800308 RSS

    Unexpected end of ZLIB input stream

    807601
      Hi Experts,

      I have to write a simple code that unziped the files within folders.
      While running the code - folders are created and some files unziped properly. but latter on it gives an error: Unexpected end of ZLIB input stream

      Below is my code. Please tell me whats wrong with this?
      Please reply as soon as possible, its urgent.
      <%@ page language="java" import="java.io.File,java.io.FileWriter,java.util.*,java.io.*,java.util.zip.*"%>
      <%
      
          String xlsname="",fname="";
          String path="D:/mike/source";
          
          try
          {
              int BUFFER = 2048;
              BufferedOutputStream dest = null;
              FileInputStream fis = new FileInputStream("D:/mike/source/new200.zip");
              ZipInputStream zip = new ZipInputStream(new BufferedInputStream(fis));
              ZipEntry entry = zip.getNextEntry();
              
              while((entry = zip.getNextEntry()) != null) 
              {
                  System.out.println("Extracting: " +entry);
                  if ((entry.isDirectory()))
                  {
                      fname=entry.getName();
                      (new File(path+"/"+entry.getName())).mkdir();
                      continue;
                  }
                  else
                  {
                      int countm;
                      byte datam[] = new byte[BUFFER];
                      // write the files to the disk
                      FileOutputStream fosm = new FileOutputStream(path+"/"+entry.getName());
                      dest = new  BufferedOutputStream(fosm, BUFFER);
                      while ((countm = zip.read(datam, 0, BUFFER)) > -1)
                      {
                         dest.write(datam, 0, countm);
                      }
                      dest.flush();
                      dest.close();
                  }
              }
                 // zip.closeEntry(); 
                  zip.close();
              
            } catch(Exception zip) {
              System.out.println("Error "+zip);
            }
          
      %>
        • 1. Re: Unexpected end of ZLIB input stream
          EJP
          Your input file is corrupt.
          • 2. Re: Unexpected end of ZLIB input stream
            807601
            Thanks for reply,
            But I dont think it's corrupt.
            Coz its unziped using WInzip properly.
            • 3. Re: Unexpected end of ZLIB input stream
              807601
              Please reply as soon as possible, its urgent
              Oops, it might be too late then.
              • 4. Re: Unexpected end of ZLIB input stream
                807601
                ???
                • 5. Re: Unexpected end of ZLIB input stream
                  EJP
                  Well presumably it's in a format that WinZip understands and ZipInputStream doesn't. For example, ZipInputStream doesn't handle ZIP files beyond a certain size, or password protection.

                  BTW:
                  ZipEntry entry = zip.getNextEntry();
                  while((entry = zip.getNextEntry()) != null) 
                  Here you are skipping the first entry. Is that really what you want?
                  • 6. Re: Unexpected end of ZLIB input stream
                    800308
                    mike.

                    Don't put this code straight in the JSP page. Stick it down in a library, for reuse everywhere. I suggest leave the main method, as it's also useable from the command line.... and dude, watch your cleanup on exception.

                    Here's my version, which seems to work.

                    I have no idea why you where getting a unexpexted end of stream... a close in the wrong place? missing a flush? Dunno.
                    package krc.utilz.zip;
                    
                    // http://java.sun.com/developer/technicalArticles/Programming/compression/
                    
                    // @TODO
                    // 1. Optimize the buffer size for performance on your machine. Most machines these days seem to do best with four or even eight K buffers.
                    // 2. Verbose mode - optionally print the filenames and directories as each is processed, as well as the time taken.
                    
                    import java.io.File;
                    import java.io.FileInputStream;
                    import java.io.BufferedInputStream;
                    import java.io.FileOutputStream;
                    import java.io.BufferedOutputStream;
                    import java.io.FileNotFoundException;
                    import java.io.IOException;
                    
                    import java.util.zip.ZipInputStream;
                    import java.util.zip.ZipEntry;
                    
                    public abstract class UnZip
                    {
                      private static final int BUFSIZE = 2048;
                      private static final char SLASH = File.separatorChar;
                    
                      public static void main(String argv[]) {
                        try {
                          unzip(argv[0], argv[1]);
                        } catch(Exception e) {
                          e.printStackTrace();
                        }
                      }
                    
                      public static void unzip(String targetDir, String zipFilename) throws FileNotFoundException, IOException {
                        makePath(targetDir);
                        ZipInputStream archive = null;
                        try {
                          archive = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilename)));
                          ZipEntry entry;
                          while ( (entry=archive.getNextEntry()) != null ) {
                            String fullname = targetDir + SLASH + entry.getName();
                            if ( entry.isDirectory() ) {
                              makePath(fullname);
                            } else {
                              unzipFile(archive, fullname);
                            }
                          }
                        } finally {
                          if(archive!=null) archive.close();
                        }
                      }
                    
                      private static void makePath(String targetDir) throws FileNotFoundException {
                        File dir = new File(targetDir);
                        if (!dir.exists()) {
                          if (!dir.mkdirs()) {
                            throw new FileNotFoundException("Unable to make directory: "+targetDir);
                          }
                        }
                      }
                    
                      private static void unzipFile(ZipInputStream archive, String outputFilename) throws IOException {
                        BufferedOutputStream output = null;
                        try {
                          int n;
                          byte buf[] = new byte[BUFSIZE];
                          output = new BufferedOutputStream(new FileOutputStream(outputFilename), BUFSIZE);
                          while ( (n=archive.read(buf,0,BUFSIZE)) != -1 ) {
                            output.write(buf, 0, n);
                          }
                          output.flush();
                        } finally {
                          if(output!=null) output.close();
                        }
                      }
                    
                    }