How to extract contents of a zip file using processing?

edited November 2013 in How To...

Does anyone know of a library or way to use processing to extract the contents of a zip file? If there is a library that does this directly, that would be great, if I have to use in combination with another program that would be ok aswell, but not the preferred option. Hoping someone has an idea of how to achieve this.

Thanks in advance Scott

Tagged:

Answers

  • edited November 2013 Answer ✓

    Java is able to do that out of the box. Processing handles reading a compressed text file, for example (AFAIK). It uses the GZIPInputStream class for that. That's for .gz files, but for .zip files, there is the ZipInputStream class.

    Otherwise, perhaps there is an Apache Commons library to ease exploration of zip files, if you have complex needs.

  • Thank you very much PhiLho. This was just what I needed. Here is the code that I am now using which I put together from a combination of the following two sites:
    A. http://stackoverflow.com/questions/2063099/unzipping-a-file-from-inputstream-and-returning-another-inputstream

    and

    B. http://concretepage.com/java/read_zip_file_java

    Not sure if I need all of those try catch statements, but hey it seems to work fine. And the code:

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    
    void setup(){
      try{
        Unzip x = new Unzip();
        x.start();
      } catch (Exception e){
        println("Exception caught in Setup");
      }
    }
    
    void draw(){}
    
    
    public class Unzip{
      public void start(){
        String dir="C:/myDirectory/";
        byte[] buff = new byte[1024];
        try{
        FileInputStream fis = new FileInputStream(dir+"myZipfile.zip");
    
            // this is where you start, with an InputStream containing the bytes from the zip file
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry entry;
                // while there are entries I process them
            while ((entry = zis.getNextEntry()) != null)
            {
                System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
                        // consume all the data from this entry
                while (zis.available() > 0)
                     try {
                           FileOutputStream fos= new FileOutputStream(dir+entry.getName());
                           int l=0;
                           // write buffer to file
                            while((l=zis.read(buff))>0){
                              fos.write(buff,0, l);
                            }
                     } catch (Exception e){
                       println("Exception writing files to disk");
                     }
             }
        }
        catch (Exception e){
        println("Exception caught within Unzip Class");
      }
    }
    }
    
Sign In or Register to comment.