Tag Archives: Scripts

Zip files and folders (including empty folders) with LiveCode

/*
iZip 1.0.1
Make a zip file recursively without the
need for a callback message. This script includes empty folders
if there are any.
—————————————————————-
Usage: iZip path[,true|false]

Parameters:
The first parameter is a string containing the path to a file or
folder. The zip file will have the same name as the original
file or folder, appended by “.zip”, and is created in the same
directory as the original.
The second parameter is a boolean excluding “.DS_Store” files if
true and includes them if false or empty.

Error messages:
If the zip external encounters a problem, an  error message is
returned of the form “error: file not open”.

Comment:
You need to write your own script to check that the revZip
external is available.

Copyright © 2011 by Economy-x-Talk
http://www.economy-x-talk
This script is free to use. Please give credit if you do. Always
include this copyright notice in a visible place in your
software.
*/
on iZip theFile,theExcludeDsStore
     if not (there is a file theFile or there is a folder theFile) then
          return “error: file not found”
     end if
     set the itemDel to slash
     put number of items of theFile into myAbsolutPathLength
     put theFile into myFolderlist
     put theFile & “.zip” into myZipFile
     // need to zip the whole thing recursively
     // item names should probably be the relative folder paths
     put the directory into myOldDir
     put 0 into myCounter
     revZipOpenArchive myZipFile,”write”
     put the result into rslt
     if rslt is not empty then
          set the itemDel to comma
          return “error:” && item 2 of rslt
     end if
     if there is a file theFile then
          put last item of theFile into myItemName
          revZipAddItemWithFile myZipFile,myItemName,theFile
          put the result into rslt
          if rslt is not empty then
               set the itemDel to comma
               return “error:” && item 2 of rslt
          end if
     else // folder
          repeat forever with messages
               add 1 to myCounter
               put line 1 of myFolderList into myCurrentFolder
               // add folder
               put item myAbsolutPathLength to -1 of
                      (myCurrentFolder & “//”) into myItemName
               revZipAddItemWithFile myZipFile,myItemName,””
               put the result into rslt
               if rslt is not empty then
                    set the itemDel to comma
                    return “error:” && item 2 of rslt
               end if
               set the directory to myCurrentFolder
               put the files into myFileList
               // zip the files
               repeat for each line myFile in myFileList with messages
                    if (myFile is “.” or myFile is “..”) or
                           theExcludeDsStore is true and myFile is
                           “.DS_Store” then next repeat
                    put item myAbsolutPathLength to -1 of
                           (myCurrentFolder & slash & myFile) into
                           myItemName
                    if char -4 to -1 of myFile is “.zip” then
                         revZipAddUncompressedItemWithFile
                                myZipFile,myItemName,myFile
                    else
                         revZipAddItemWithFile myZipFile,myItemName,myFile
                    end if
                    wait 0 millisecs with messages
               end repeat
               put the folders into myTempFolderList
               repeat for each line myFolder in myTempFolderList
                    if myFolder is “.” or myFolder is “..” then next repeat
                    put cr & myCurrentFolder & slash & myFolder after
                           myFolderList
               end repeat
               delete line 1 of myFolderList
               if number of lines of myFolderList is 0 then exit repeat
               wait 0 millisecs with messages
          end repeat
     end if
     revZipCloseArchive myZipFile
     put the result into rslt
     if rslt is not empty then
          set the itemDel to comma
          return “error:” && item 2 of rslt
     end if
     set the directory to myOldDir
end iZip