Sunday, September 20, 2015

Extract zip archive with folders in Livecode

Ever tried to extract a zip archive with revZip that contains folders? It's not so easy. The example from Livecode states it is not possible. But I got it to work the following way below. I'm sure that the code still can be optimized, but at least it's a start.
command ee.zip.extractArchive pArchive, pWhere
   local tZipContents
   local tFolderPath
   
   -- open archive and read items
   revZipOpenArchive pArchive, "read"
   put revZipEnumerateItems(pArchive) into tZipContents
   
   -- get rid of the last slash if there is one
   if the last char of pWhere is slash then
      delete the last char of pWhere
   end if
   
   -- loop through the zip content, create folders and files
   repeat for each line ll in tZipContents
      if the last char of ll is slash then --> the last item is a directory
         put ll into tFolderPath
         -- create folders
         _ee.zip.createFolders tFolderPath, pWhere
      else --> the last item is a file
         set the itemDel to slash
         put item 1 to -2 of ll into tFolderPath
         -- create folders
         _ee.zip.createFolders tFolderPath, pWhere
         -- extract file
         revZipExtractItemToFile pArchive, ll, (pWhere & slash & ll)
      end if
   end repeat
end ee.zip.extractArchive

private command _ee.zip.createFolders  pPath, pWhere
   local tFullPath
   
   split pPath by slash
   put empty into tFullPath
   repeat for each element ee in pPath
      put ee & slash after tFullPath
      create folder (pWhere & slash & tFullPath)
   end repeat
end _ee.zip.createFolders

1 comment: