Page 1 of 2

Unzip file and show it.

Posted: Fri Feb 08, 2013 11:03 am
by ChrisMukrow
Dear LiveCoders,

I'm trying to download a zipfile and unzip it on a iOS device. The zipfile contains 50± images, I want to load them into background images. The download part works (I think), and the unzipping part also (I think :P). But the strange thing is, it doesn't show an image.

Here is my code:

Code on card 1, download the zip + unzip it

Code: Select all

on downloadFiles
   libUrlDownloadToFile "URL to zipfile", specialfolderpath ("Documents") & "/imagesApp.zip", "unZip" 
end downloadFiles

command unZip
   put specialFolderPath("documents") & "/imagesApp.zip" into TempFile
   put specialFolderPath("documents") & "/images/" into TargetFile
   
   revZipOpenArchive TempFile, "read"
   put RevZipEnumerateItems(TempFile) into tList
   
   repeat for each line i in tList
      
      if i = empty then
         next repeat
      end if
      
      revZipExtractItemToFile TempFile, i, TargetFile & "/" & i
      
   end repeat
      
   revZipCloseArchive TempFile
   delete file TempFile
end unZip
Code on card 2, load the image

Code: Select all

on preOpenCard
   if the environment is not "mobile" then
      exit preOpenCard
   end if

   set the filename of image "ImageHolder" to specialFolderPath("documents") & "/images/1.jpg"
end preOpenCard
What am I doing wrong, any ideas?

Kind regards,

Chris

Re: Unzip file and show it.

Posted: Fri Feb 08, 2013 11:14 am
by Klaus
Hi Chris,

try with parenthesis:
...
set the filename of image "ImageHolder" to (specialFolderPath("documents") & "/images/1.jpg")
...


Best

Klaus

Re: Unzip file and show it.

Posted: Fri Feb 08, 2013 11:50 am
by ChrisMukrow
Hi Klaus,

Unfortunately no luck, it doesn't work.

Kind regards,

Chris

Re: Unzip file and show it.

Posted: Fri Feb 08, 2013 2:11 pm
by endernafi
Hi Chris,

Do you have opportunity to try it on the simulator?
That way, you can actually see whether the download and / or unzipping is successful.
Here is your apps' filepath:
/Users/yourUserName/Library/Application Support/iPhone Simulator/6.0/Applications/

If you must run it on a physical device, then you can use the answer method 8)

Code: Select all

answer "folder exists?" && (there is a folder (specialFolderPath("documents") & "/images"))
answer "file exists?" && (there is a file (specialFolderPath("documents") & "/images/1.jpg))
Just trying to reduce the number of possible sources of errors...

Hope it helps...

Best,

~ Ender Nafi

Re: Unzip file and show it.

Posted: Fri Feb 08, 2013 2:34 pm
by Klaus
Hi Chris,

looks like you have one SLASH too much!

Code: Select all

command unZip
   put specialFolderPath("documents") & "/imagesApp.zip" into TempFile
   put specialFolderPath("documents") & "/images/" into TargetFile
   
   revZipOpenArchive TempFile, "read"
   put RevZipEnumerateItems(TempFile) into tList
   
   repeat for each line i in tList    
      if i = empty then
         next repeat
      end if
      
     ## revZipExtractItemToFile TempFile, i, TargetFile & "/" & i
     ## TARGETFILE already ENDS with a SLASH, see line 2!
     ## So this may give an invalid pathname in the end!?

      revZipExtractItemToFile TempFile, i, (TargetFile & i)
 ...
Best

Klaus

Re: Unzip file and show it.

Posted: Mon Feb 18, 2013 10:33 am
by ChrisMukrow
Hi Guys,

Sorry for my late reply, it was Carnaval in the Netherlands :D

The slash wasn't a fix. But when I tried the method of Ender, it was pretty interesting. The zip gets downloaded, but there is something wrong in the unzipping part. It creates an empty file (zero bytes) with the same the name of the zip without the extension.

Already thanks for your help!

Best,

Chris

Re: Unzip file and show it.

Posted: Mon Feb 18, 2013 9:11 pm
by Simon
Hi Chris,

Code: Select all

 libUrlDownloadToFile "URL to zipfile", specialfolderpath ("Documents")
put specialFolderPath("documents") & "/imagesApp.zip" into TempFile
I'm not sure if this still holds true but iOS is case sensitive so you should change the first one to ("documents").
The dictionary shows all lowercase names.

Simon
EDIT: for debugging sake you should add a fld

Code: Select all

put RevZipEnumerateItems(TempFile) into tList
put tList into fld "testField"

Re: Unzip file and show it.

Posted: Tue Feb 19, 2013 9:15 am
by ChrisMukrow
Hi Simon,

Thanks for your help! The documents is not a problem (still changed it though), because when I download the file and unzip it, I see the files in the documents map of the simulator.

The tList I just added and it gives this output:

imagesApp/
imagesApp/1.jpg
_MACOSX/
_MACOSX/imagesApp/._1.jpg
imagesApp/2.jpg
_MACOSX/imagesApp/._2.jpg
imagesApp/3.jpg
_MACOSX/imagesApp/._3.jpg

Best,

Chris

Re: Unzip file and show it.

Posted: Tue Feb 19, 2013 9:40 am
by Simon
Just a start...
the folder is imagesApp/ not images/, so I think the location ends up images/imagesApp/1.jpg
I would also "filter tList without "_MACOSX/"" (that may not be needed).

Right now I forget if revZipExtractItemToFile creates directories, but I think it does.

Simon

Re: Unzip file and show it.

Posted: Tue Feb 19, 2013 10:22 am
by ChrisMukrow
Hi,

It works now! Thanks to everyone who helped! :D Especially Simon with the final solution!

The problem was that the zip was a map, so when you unzip it. It creates a map in a map: /images/imagesApp/

Final code if someone needs it:

Code: Select all

on downloadFiles
   libUrlDownloadToFile "URL TO ZIP", specialfolderpath ("documents") & "/imagesApp.zip"
   send unZip to me in 0 milliseconds
 end downloadFiles

command unZip
   
   put specialFolderPath("documents") & "/imagesApp.zip" into TempFile
   put specialFolderPath("documents") & "/" into TargetFile -- Problem part, previous one was linked to a map in a map, because the zip is a map. 
   
   revZipOpenArchive TempFile, "read"
   put RevZipEnumerateItems(TempFile) into tList
   
   repeat for each line i in tList
      
      if i = empty then
         next repeat
      end if
      
      revZipExtractItemToFile TempFile, i, (TargetFile & i)
      
   end repeat
   
   revZipCloseArchive TempFile
   delete file TempFile
   
end unZip
Best,

Chris

Re: Unzip file and show it.

Posted: Tue Feb 19, 2013 10:38 am
by Simon
Glad it worked!
Debugging code is easier when you can see what the code is doing so drop fields and answer commands everywhere.
It's better then beating your head against a wall. :lol:

Simon

Re: Unzip file and show it.

Posted: Tue Feb 26, 2013 8:09 pm
by FireWorx
Hi,
I am attempting to download a zipped folder containing 50 pfd files to the documents folder of my app. I have been successful getting the zip file to the documents folder but so far can't unzip the file. Can someone look at this and tell me if i'm on the right track? I simply zipped the entire folder and loaded it onto my web server. Tried using some code I found on this thread but without success.

on mouseup
downloadFiles
end mouseup

on downloadFiles
   libUrlDownloadToFile "http://myserver.on-rev.com/Maps/ALSFD1.zip", specialfolderpath ("documents") & "/ALSFD1.zip"
   answer "file exists?" && (there is a file (specialFolderPath("documents") & "/ALSFD1.zip")) ## good so far to here
put specialFolderPath("documents") & "/ALSFD1.zip" into TempFile
   put specialFolderPath("documents") & "/" into TargetFile
   revZipOpenArchive TempFile, "read"
revZipExtractItemToFile TempFile,specialFolderPath("documents") & "/ALSFD1", specialFolderPath("documents") & "/ALSFD1"

revZipCloseArchive TempFile
  put RevZipEnumerateItems(TempFile) into tList
    repeat for each line i in tList
      if i = empty then
         next repeat
      end if
      revZipExtractItemToFile TempFile, i, (TargetFile & i)
      end repeat
  revZipCloseArchive TempFile
   delete file TempFile
end downloadFiles

Any help would be appreciated.
Dave

Re: Unzip file and show it.

Posted: Wed Feb 27, 2013 12:35 am
by Simon
Hi Dave,
Why do you have that revZipCloseArchive TempFile the 9th line down? The revZipEnumerateItems will never work.

Simon

Re: Unzip file and show it.

Posted: Wed Feb 27, 2013 8:27 pm
by FireWorx
Thanks Simon,
I removed that line and still not working. I am able to download both a zipped file and a zipped folder to the documents folder. The zipped folder has 50 or so .pdf files within it that were not individually zipped.

1)I just need the correct code to unzip a file sitting in the special path documents folder on the iPad.
And
2) if it's possible the correct code to unzip a zipped folder full of .pdf files sitting in the special folder path documents folder.

Can you help me with this?

Thanks,
Dave

Re: Unzip file and show it.

Posted: Wed Feb 27, 2013 9:17 pm
by Simon
Hi Dave,
Why don't you just use the above code from Chris?
Worked for him.

Simon