ZipXtra
The ZipXtra was added to the Xtras/Media Support/ folder in Director 8.0. It can be used to compress files and folders and save the result as a ZIP file.
put xtra("ZipXtra").interface() -- "xtra ZipXtra -- version 8.0.0.r178 ... version 10.1.0.r11 new object me -- create a new child instance ZipFiles object me, string zipDirectory, string zipFileName, any fileList -- create zip file. CabFiles object me, string zipDirectory, string zipFileName, any fileList -- create zip file. JarFiles object me, string zipDirectory, string zipFileName, any fileList -- create zip file. "
<zipDirectory> should be a path to the folder that contains the files to compress. <zipFileName> should be a string such as "Archive.zip". This is the name of the ZIP file which will be created. It will be created inside the folder defined by <zipDirectory> <fileList> should be a linear list of file names, relative to <zipDirectory>
No result is returned by any of these commands, and no error or status information is available to let you know whether the command succeeded.
While the ZipFiles() command will create a ZIP file, no compression is applied, so the resulting file is actually larger than the original file(s). The JarFiles() command will create a compressed file.
Contents
Example Code
on ZipIt() ----------------------------------------------------------- -- ACTION: Zips a file chosen by the user. The Zip file will be -- saved in the same folder as the chosen file. -- OUTPUT: Returns 0 if no error has been detected, or #userCanceled -- if the user cancels the Open File or Save File As dialog. --------------------------------------------------------------------
vFileIO = xtra("FileIO").new()
vFileToZip = vFileIO.displayOpen() -- includes folder path case vFileToZip of VOID, "": return #userCanceled end case
vZipFile = vFileIO.displaySave("Save ZIP file as:","Archive.zip") -- A full folder path is returned, but only the file name itself -- will be used. The ZipFile will be saved in the folder that -- FileToZip is stored in, regardless of the folder chosen here by -- the user.
case vZipFile of VOID, "": return #userCanceled end case
-- Separate the folder and file names. vDirectory = vFileToZip
vDelimiter = the last char of the applicationPath -- ":" | "\" vTemp = the itemDelimiter the itemDelimiter = vDelimiter vFileToZip = the last item of vFileToZip -- short file name vZipFile = the last item of vZipFile -- short file name delete the last item of vDirectory -- folder path only put vDelimiter after vDirectory -- with delimiter the itemDelimiter = vTemp
put vDirectory -- "Mac HD:Users:username:Desktop:" put vZipFile -- "Archive.zip" put vFileToZip -- "Big File.ext"
vZipXtra = xtra("ZipXtra").new() put vZipXtra.JarFiles(vDirectory, vZipFile, [vFileToZip]) -- <Void>
return 0 -- apparently no error end ZipIt
Caution
Director MX and MX 2004 will crash if the file to zip is currently in use by another application. Director 8.0 will simply create an empty ZIP file in the same circumstances. Attempting to Zip read-only files can also be hazardous.
See Also
Gary Smith has written free BudZip and BudUnZip xtras (Windows only). You can download the latest beta version from here.
MacOSX users can try the cli zip utility in conjunction with the shell xtra (no man page - enter zip -h for details. There *is* a man page in OSX 10.4.3: type man zip).
Another useful zipping xtra is zlib: you can choose a compression level and a protecting password too.
Download
You can find wrapper scripts for ZipXtra and the BudZip xtra here.