Deleting a compressed zip file using the Java ZipFile class

I have a problem compressing a ZIP file using Java. The method is shown below.

The file structure is correct after deleting the file, which means the directories are in the zip file, but the output files are zero length.

I checked the ZIP file to make sure the compression is correct, everything is correct.

Please, if anyone sees something that I missed ...

public static void unzip ( File zipfile, File directory ) throws IOException {
  ZipFile zip = new ZipFile ( zipfile );
  Enumeration<? extends ZipEntry> entries = zip.entries ();

  while ( entries.hasMoreElements () ) {
    ZipEntry entry = entries.nextElement ();
    File file = new File ( directory, entry.getName () );

    if ( entry.isDirectory () ) {
      file.mkdirs ();
    }
    else {
      file.getParentFile ().mkdirs ();

      ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );
      OutputStream out = new FileOutputStream ( file );
      byte[] buffer = new byte[4096];
      int readed = 0;

      while ( ( readed = in.read ( buffer ) ) > 0 ) {
        out.write ( buffer, 0, readed );
        out.flush ();
      }

      out.close ();
      in.close ();
    }
  }

  zip.close ();
}

      

Something else ... Obviously getInputStream (write) method returns null bytes, not sure why exactly.

+3


source to share


2 answers


ZipFile already unpacks record data, no need to use ZipInputStream

.

Instead:

ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );

      



Using:

InputStream in = zip.getInputStream ( entry );

      

ZipInputStream can be used to decompress ZIP files. The reason you are getting zero length streams is because with ZipInputStream you need to call getNextEntry () to read the first file into ZIP.

+2


source


The following code works:

package so;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class TestZip {

    public static void main(String[] args) {
        String path = "C:" + File.separator + "tmp" + File.separator;
        String nom = "demo.zip";
        File zipfile = new File(path + nom);
        File directory = new File(path);
        TestZip m = new TestZip();
        try {
            m.unzip(zipfile, directory);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public static void unzip ( File zipfile, File directory ) throws IOException {
        System.out.println(zipfile.toString());
        System.out.println(directory.toString());
          ZipFile zip = new ZipFile (  zipfile );
          System.out.println("1");
          Enumeration<? extends ZipEntry> entries = zip.entries ();
          System.out.println("2");

          while ( entries.hasMoreElements () ) {
              System.out.println("3");
            ZipEntry entry = entries.nextElement ();
            File file = new File ( directory, entry.getName () );

            if ( entry.isDirectory () ) {
              file.mkdirs ();
            }
            else {
              file.getParentFile ().mkdirs ();

              ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );
              OutputStream out = new FileOutputStream ( file );
              byte[] buffer = new byte[4096];
              int readed = 0;

              while ( ( readed = in.read ( buffer ) ) > 0 ) {
                out.write ( buffer, 0, readed );
                out.flush ();
              }

              out.close ();
              in.close ();
            }
          }

          zip.close ();
        }


}

      



So I guess the problem is with the parameters you are passing. Create a "zipfile" argument with "new file (complete_path + filename)". If you just create with the filename it won't work.

-1


source







All Articles