Working with Zip Files

zipfileZip files are a common file format for sharing collections of files or for compressing large files. I’m going to take a look at how JSL can be used to handle these files without first manually unzipping the file.

The Zip Archive Object

Version 12 of JMP introduced the Zip Archive object for manipulating zip files. A zip archive object is created using the Open function with the zip keyword:

Interrogating the Zip File

The first thing that we might want to do is to determine the contents of the zip file.  The dir message can be sent to a zip archive object to product a directory list of the contents:

dir-msg

Opening the First JMP Table

To read the contents of the first JMP table within the zip file I can send the read message to the zip archive object:

The JMP file is in a binary format, not straight text. I deal with that by adding ‘format(blob)’ to the message.  The data that describes the contents of this file is contained in a variable that I have named blobdata.

To create the physical file I need to save this data:

Now I can access the JMP data in the usual way:

Processing the Entire Zip File

Now that I have established the code framework for handling the contents of the zip file, I can iterative over all of the contained files:

two-files

An Extended Example

Let’s say that I want to perform the following tasks:

  1. Allow the user to select the zip file
  2. Present the user a list of files contained within the zip file
  3. Allow the user to select a file
  4. Open the selected file

Here is the code:

 

Leave a Reply

Your email address will not be published.