ZipFile.frink

View or download ZipFile.frink in plain text format


/** This file demonstrates reading the contents of a zip file.   It contains
   a full implementation of a zip file decompressor in a few lines of code. */


/** Unzip the specified file to the specified target directory.

    args:
     [filename, targetDir, verbose]

    filename:  a string filename or java.io.File object indicating the file to
               be unzipped

    targetDir: a string filename or java.io.File object indicating the target
               directory to unzip to.

    verbose:  a boolean flag.  If this is true, the filenames and sizes will
              be printed.
*/

unzipFile[filename, targetDir=".", verbose=false] :=
{
   zip = newJava["java.util.zip.ZipFile", filename]
   
   for zipEntry = zip.entries[]
   {
      // Print size and filename if requested
      if verbose
         println[zipEntry.getSize[] + "\t" + zipEntry.getName[]]

      file = newJava["java.io.File", [targetDir, zipEntry.getName[]]]

      if zipEntry.isDirectory[]
         file.mkdirs[]   // Create even empty directories
      else
      {  // This is probably an ordinary file
         parent = file.getParentFile[]
         if parent != undef
            parent.mkdirs[]  // Create all parent directories
         copyStream[zip.getInputStream[zipEntry],
         newJava["java.io.FileOutputStream", [file]]]
      }
   }

   zip.close[]
}

// Sample usage (note that a jar file is a zip file.)
//unzipFile["../jar/frink.jar", "out", true]


View or download ZipFile.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 18206 days, 21 hours, 24 minutes ago.