Using zip to package a folder with subfolders

Trevor DeVore lists at mangomultimedia.com
Wed Aug 12 13:10:32 EDT 2009


On Aug 12, 2009, at 1:02 PM, Tereza Snyder wrote:

> I need to zip up a folder that contains files and subfolders. Do I  
> need to use a shell command, or can a sequence of revzip commands do  
> the trick? What script commands are needed?

You can use revZip if you want. Here are two (uncommented) handlers I  
use for zipping/unzipping folders. When zipping you can probably just  
concern yourself with the first 3 params.

Regards,

-- 
Trevor DeVore
Blue Mango Learning Systems
www.bluemangolearning.com    -    www.screensteps.com



-- Leave pFolderPath empty. It is used recursively
command zipAddFolderToArchive pZipArchivePath, pRootFolderPath,  
pIncludeRootFolderInArchiveItemNames, pFilesToExclude,  
pExtensionsThatArentCompressed, pFolderPath
     local theArchiveItemName,theCharNo,theError,theFile
     local theFiles,theFolder,theFolders

     put pIncludeRootFolderInArchiveItemNames is true into  
pIncludeRootFolderInArchiveItemNames
     set the wholematches to true

     replace comma with cr in pExtensionsThatArentCompressed

     if pFolderPath is empty then
         put pRootFolderPath into pFolderPath	
         if pIncludeRootFolderInArchiveItemNames then
             set the itemdel to slash	# we want folder to be reflected  
in archive item name
             delete item -1 of pRootFolderPath
         end if
     end if

     put fileFilesInFolder(pFolderPath, true, true) into theFiles
     filter theFiles without ".DS_Store"
     set the itemdel to "."

     repeat for each line theFile in theFiles
         if theFile is among the lines of pFilesToExclude then next  
repeat

         put theFile into theArchiveItemName
         put offset(pRootFolderPath, theArchiveItemName) into theCharNo
         if theCharNo is 0 then return "file is not in expected folder"

         delete char 1 to (the number of chars of pRootFolderPath + 1)  
of theArchiveItemName	# strip root folder up to slash (zip item names  
shouldn't start with a slash)

         if item -1 of theArchiveItemName is among the lines of  
pExtensionsThatArentCompressed then
             revZipAddUncompressedItemWithFile pZipArchivePath,  
theArchiveItemName, theFile
         else
             revZipAddItemWithFile pZipArchivePath,  
theArchiveItemName, theFile
         end if
         if the result is not empty then
             put the result into theError
             exit REPEAT
         end if
     end REPEAT

     if theError is empty then
         put fileFoldersInFolder(pFolderPath, true, true) into  
theFolders
         repeat for each line theFolder in theFolders
             zipAddFolderToArchive pZipArchivePath, pRootFolderPath,  
pIncludeRootFolderInArchiveItemNames, pFilesToExclude,  
pExtensionsThatArentCompressed, theFolder
             put the result into theError
             if theError is not empty then
                 exit REPEAT
             end if
         end REPEAT
     end if

     return theError
end zipAddFolderToArchive


command zipDecompressArchiveToFolder pZipArchivePath, pOutputFolder,  
pItemsToExclude, pCallbackHandler
     local theError

     if the last char of pOutputFolder is slash then delete the last  
char of pOutputFolder

     set the wholematches to true
     put the filetype into theFileType
     set the filetype to empty

     put revZipEnumerateItems(pZipArchivePath) into theArchiveItems
     put the number of lines of theArchiveItems into theItemCount

     repeat for each line theArchiveItem in theArchiveItems
         add 1 to i

         if theArchiveItem is among the lines of pItemsToExclude then  
next repeat

         put pOutputFolder & slash & theArchiveItem into theFilePath
         put fileExtractDirectory(theFilePath) into theDirectoryPath

         if theDirectoryPath is not pOutputFolder then
             fileCreateAllFoldersInPath theDirectoryPath, pOutputFolder
             put the result into theError
         end if

         if theError is empty then
             if pCallbackHandler is not empty then
                 put CardOf(the long id of the target) into theCard
                 put "send" && quote & pCallbackHandler && "i,  
theItemCount" & quote && "to theCard" into theDo
                 do theDo
             end if

             revZipExtractItemToFile pZipArchivePath, theArchiveItem,  
theFilePath
             put the result into theError
         end if

         if theError is not empty then exit REPEAT
     end REPEAT

     set the filetype to theFileType

     return theError
end zipDecompressArchiveToFolder



More information about the Use-livecode mailing list