An example of unzip

Discussions

EJB design: An example of unzip

  1. An example of unzip (1 messages)

    hi all, anyone has another example of unzip with source codes to demo here? This set of unzip codes don't work. Thanks!

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;


    public class Unzip {

      public static final void copyInputStream(InputStream in, OutputStream out)
      throws IOException
      {
        byte[] buffer = new byte[1024];
        int len;

        while((len = in.read(buffer)) >= 0)
          out.write(buffer, 0, len);

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

      public static final void main(String[] args) {
        Enumeration entries;
        ZipFile zipFile;

      try {
          zipFile = new ZipFile("C:\\Temp
    FolderZiper.zip");

          entries = zipFile.entries();

          while(entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry)entries.nextElement();

            if(entry.isDirectory()) {
              // Assume directories are stored parents first then children.
              System.err.println("Extracting directory: " + entry.getName());
              // This is not robust, just for demonstration purposes.
              (new File(entry.getName())).mkdir();
              continue;
            }

            System.err.println("Extracting file: " + entry.getName());
            copyInputStream(zipFile.getInputStream(entry),
               new BufferedOutputStream(new FileOutputStream(entry.getName())));
          }

          zipFile.close();
        } catch (IOException ioe) {
          System.err.println("Unhandled exception:");
          ioe.printStackTrace();
          return;
        }
      }

    }
  2. This code does not work if u use a compressed archive. If you had control on how the jar file is built, then u could make a jar without compression and this program will work just fine!