Create a zip file

Verified
Added by iNTERFACEWARE

How to create and unzip a zip file containing multiple files and directories, using filter.zip.deflate() and filter.zip.inflate()

Source Code
   -- create a zip file for the "test_zip" directory
   -- containing two files "a-test" and "b-test"

   -- construct hard-coded table to zip
   local  T = {['test_zip']={['a-test.txt']='a', ['b-test.txt']='b'}}
   trace(T)
   
   -- zip the file into a string
   local zip = filter.zip.deflate(T)
   trace(zip)
   
   -- unzip the file from the string
   local unzip = filter.zip.inflate(zip)
   trace(unzip)
   
   -- save the file to disk 
   local f = io.open('test_zip.zip', 'w')
   f:write(zip)
   f:close()
   
   -- go to the Iguana home directory and run 'test_zip.zip'
   -- NOTE: If you change the contents of the files "a-test" and "b-test"
   --       they will be reflected in the new zip file created 
   
   -- create the zip file using content from the test_zip directory
   
   -- open the files
   local fa = io.open('test_zip/a-test.txt', 'r')
   local fb = io.open('test_zip/b-test.txt', 'r')

   -- construct table to zip using file content
   -- NOTE: We also added an (empty) subdirectory 'test_sub'
   local  TT = {['test_zip2']={['a-test.txt']=fa:read(), ['b-test.txt']=fb:read(),['test_sub']={}}}
   trace(TT)

   -- zip the file into a string
   local zip = filter.zip.deflate(TT)
   trace(zip)
   
   -- unzip the file from the string
   local unzip = filter.zip.inflate(zip)
   trace(unzip)

   -- save the second zip file to disk 
   local f = io.open('test_zip2.zip', 'w')
   f:write(zip)
   f:close()
   
   -- go to the Iguana home directory and run 'test_zip2.zip'
   -- view the directory contents to confirm the 'test_sub' subdirectory was created
Description
How to create and unzip a zip file containing multiple files and directories, using filter.zip.deflate() and filter.zip.inflate()
Usage Details

Demonstrates how to convert the contents of an Iguana table into a zip file, using filter.zip.deflate(). It also shows how to unzip the file into a table using filter.zip.inflate(). The second example includes the addition of an (empty) subdirectory test_sub.

How to use the snippet:

  • Paste the code into your script
  • Unzip the test_zip.zip file that is created in the Iguana install directory
  • Inspect the annotations to see how it works

Note: The second portion of the code (that creates test_zip2.zip) will fail until you unzip the first file (test_zip.zip).