ZIP Compressing Folder Vs. List Of Files - Possible?

Hi,

I’ve seen that the ZIP Plugin api states that this is the way to compress the files into zip archive:

local options = { zipFile = "mytest.zip", zipBaseDir = system.DocumentsDirectory, srcBaseDir = system.DocumentsDirectory, srcFiles = { "space.jpg", "space1.jpg", "space2.jpg"}, listener = zipListener } zip.compress(options);

However, this API can’t help if I have entire folder that I want to zip recursively.
For example I may be having following file/folder structure:

my_files/

   folder_1/
      file_1a.txt
      file_1b.txt
   folder_2/
      file_2a.txt
   folder_3/
         folder_33/
            file_33a.txt

Now I already know it’s not *unfortunately* possible to do this:

srcFiles={ "my\_files" }

and let the zip compress all the files recursively.

However, even if I manually list all the files in these folders

srcFiles = { "my\_files/folder\_1/file\_1a.txt", "my\_files/folder\_1/file\_2a.txt", ... etc }

the resulting zip archive will contain all the files I’ve listed, BUT their folder structure will not be preserved - ie. everything will be packed in such way that it’s unpacked to root folder (in this case will be mytest/ folder after unpacking the files).

So is there a way to compress the files in order to preserve the folder structure?
Thanks
N.

I also need a solution for this. My server api expects the zipped files to be contained in specific folders so it can catalog them accordingly (based on the folder name.) If there are two files with the same name under different folders, the zip pluggin will store them together causing an error when extracting them. Example:

Order1/Audio/Rec1.mp3

Order1/Image/Picture1.jpg

Order1/Image/Picture2.jpg

Order1/Video/Movie1.mov

Order2/Audio/Rec1.mp3

Order2/Image/Picture1.jpg

Order2/Video/Movie1.mov

Without the ability to store the directory structure in the zipped file, the zip plugin, as it is, will force me to rewrite my server api and corona mobile app to combine the folder name into the file name, while at the same time maintaing the existing logic to support existing non-corona mobile apps.

Should an issue/bug be created for Corona to address this?

I’ll check with Engineering.

While I await to hear from them, have you tried:

srcFiles = { "my\_files", "my\_files/folder\_1", "my\_files/folder\_1/file\_1a.txt", "my\_files/folder\_1/file\_2a.txt", ... etc }

Hi Rob, Yes, I tried that too. It flattens the files. Thanks!

What OS are you building for?  Is this happening on device or on the sim?  What version of Corona SDK are you using?

Simulator running on Mac OSX Mavericks. Corona SDK 2013.1202. Here’s the code:

local zip = require( “plugin.zip” )

local function zipListener( event )

        if ( event.isError ) then

                print( “Unzip Error”)

        else

        --Example response

                print ( "event.name: " … event.name )

                print ( "event.type: " … event.type )

        --event.response = {

        –  [1] = “space.jpg”,

        –  [2] = “space1.jpg”,

        --}

        end

end

local options = 

    zipFile    =“test.zip”,

    zipBaseDir = system.TemporaryDirectory,

    srcBaseDir = system.DocumentsDirectory,

    srcFiles = { ‘test/files/abc.txt’,‘test/files/def.txt’},

    listener = zipListener

}

zip.compress(options);

Here’s the output (stdout):

2013-11-08 08:42:06.414 Corona Simulator[1143:507] 

Copyright © 2009-2013  C o r o n a   L a b s   I n c .

2013-11-08 08:42:06.414 Corona Simulator[1143:507]     Version: 2.0.0

2013-11-08 08:42:06.414 Corona Simulator[1143:507]     Build: 2013.1202

2013-11-08 08:42:06.430 Corona Simulator[1143:507] The file sandbox for this project is located at the following folder:

    (/Users/msantos/Library/Application Support/Corona Simulator/pz4u-2F21643D6AE2DA3D74B0E91C1B66343C)

2013-11-08 08:42:08.152 Corona Simulator[1143:507] event.name: zip

2013-11-08 08:42:08.152 Corona Simulator[1143:507] event.type: compress

Unzipping the file (zip file contents):

maumacbook15:tmp msantos$ pwd

/Users/msantos/Library/Application Support/Corona Simulator/pz4u-2F21643D6AE2DA3D74B0E91C1B66343C/tmp

maumacbook15:tmp msantos$ ls -l

total 8

-rw-r–r--  1 msantos  staff  206 Nov  8 08:42 test.zip

maumacbook15:tmp msantos$ unzip test.zip 

Archive:  test.zip

  inflating: abc.txt                 

  inflating: def.txt                 

maumacbook15:tmp msantos$ ls

abc.txt        def.txt        test.zip

maumacbook15:tmp msantos$

This is the plugin I have:

maumacbook15:Plugins msantos$ pwd

/Users/msantos/Library/Application Support/Corona/Simulator/Plugins

maumacbook15:Plugins msantos$ ls -l

total 312

-rwxr-xr-x@ 1 msantos  staff  155732 Nov  7 06:07 plugin_zip.dylib

maumacbook15:Plugins msantos$

I also need a solution for this. My server api expects the zipped files to be contained in specific folders so it can catalog them accordingly (based on the folder name.) If there are two files with the same name under different folders, the zip pluggin will store them together causing an error when extracting them. Example:

Order1/Audio/Rec1.mp3

Order1/Image/Picture1.jpg

Order1/Image/Picture2.jpg

Order1/Video/Movie1.mov

Order2/Audio/Rec1.mp3

Order2/Image/Picture1.jpg

Order2/Video/Movie1.mov

Without the ability to store the directory structure in the zipped file, the zip plugin, as it is, will force me to rewrite my server api and corona mobile app to combine the folder name into the file name, while at the same time maintaing the existing logic to support existing non-corona mobile apps.

Should an issue/bug be created for Corona to address this?

I’ll check with Engineering.

While I await to hear from them, have you tried:

srcFiles = { "my\_files", "my\_files/folder\_1", "my\_files/folder\_1/file\_1a.txt", "my\_files/folder\_1/file\_2a.txt", ... etc }

Hi Rob, Yes, I tried that too. It flattens the files. Thanks!

What OS are you building for?  Is this happening on device or on the sim?  What version of Corona SDK are you using?

Simulator running on Mac OSX Mavericks. Corona SDK 2013.1202. Here’s the code:

local zip = require( “plugin.zip” )

local function zipListener( event )

        if ( event.isError ) then

                print( “Unzip Error”)

        else

        --Example response

                print ( "event.name: " … event.name )

                print ( "event.type: " … event.type )

        --event.response = {

        –  [1] = “space.jpg”,

        –  [2] = “space1.jpg”,

        --}

        end

end

local options = 

    zipFile    =“test.zip”,

    zipBaseDir = system.TemporaryDirectory,

    srcBaseDir = system.DocumentsDirectory,

    srcFiles = { ‘test/files/abc.txt’,‘test/files/def.txt’},

    listener = zipListener

}

zip.compress(options);

Here’s the output (stdout):

2013-11-08 08:42:06.414 Corona Simulator[1143:507] 

Copyright © 2009-2013  C o r o n a   L a b s   I n c .

2013-11-08 08:42:06.414 Corona Simulator[1143:507]     Version: 2.0.0

2013-11-08 08:42:06.414 Corona Simulator[1143:507]     Build: 2013.1202

2013-11-08 08:42:06.430 Corona Simulator[1143:507] The file sandbox for this project is located at the following folder:

    (/Users/msantos/Library/Application Support/Corona Simulator/pz4u-2F21643D6AE2DA3D74B0E91C1B66343C)

2013-11-08 08:42:08.152 Corona Simulator[1143:507] event.name: zip

2013-11-08 08:42:08.152 Corona Simulator[1143:507] event.type: compress

Unzipping the file (zip file contents):

maumacbook15:tmp msantos$ pwd

/Users/msantos/Library/Application Support/Corona Simulator/pz4u-2F21643D6AE2DA3D74B0E91C1B66343C/tmp

maumacbook15:tmp msantos$ ls -l

total 8

-rw-r–r--  1 msantos  staff  206 Nov  8 08:42 test.zip

maumacbook15:tmp msantos$ unzip test.zip 

Archive:  test.zip

  inflating: abc.txt                 

  inflating: def.txt                 

maumacbook15:tmp msantos$ ls

abc.txt        def.txt        test.zip

maumacbook15:tmp msantos$

This is the plugin I have:

maumacbook15:Plugins msantos$ pwd

/Users/msantos/Library/Application Support/Corona/Simulator/Plugins

maumacbook15:Plugins msantos$ ls -l

total 312

-rwxr-xr-x@ 1 msantos  staff  155732 Nov  7 06:07 plugin_zip.dylib

maumacbook15:Plugins msantos$

Hello,

was there any progress on this? I have the same problem.

Niklas

Well, it’s been a while since I last used Corona, but I remember there was a sort of workaround to this.
Actually not a workaround. The Corona zip plugin will work asynchronously, meaning your code will continue to execute while Corona unpacks zip in the background. What you must take care is NOT TO ACCESS ANY FILE  SYSTEM FUNCTION while zip plugin is not finished. If you do anything (like change active folder etc), the zip plugin will continue unpacking the stuff into that folder that you’ve just operated on, not the original folder.
Something like that. So I needed to find a way to kinda “wait” until plugin is done (which I don’t remember how I did, or if I did), before you continue using file system functions.

Yes - I have a simple test where nothing is done until the zip operation finishes with the callback. But still I cannot find any combination of paths/filenames that preserve the folder structure in the zip file.

Hello,

was there any progress on this? I have the same problem.

Niklas

Well, it’s been a while since I last used Corona, but I remember there was a sort of workaround to this.
Actually not a workaround. The Corona zip plugin will work asynchronously, meaning your code will continue to execute while Corona unpacks zip in the background. What you must take care is NOT TO ACCESS ANY FILE  SYSTEM FUNCTION while zip plugin is not finished. If you do anything (like change active folder etc), the zip plugin will continue unpacking the stuff into that folder that you’ve just operated on, not the original folder.
Something like that. So I needed to find a way to kinda “wait” until plugin is done (which I don’t remember how I did, or if I did), before you continue using file system functions.

Yes - I have a simple test where nothing is done until the zip operation finishes with the callback. But still I cannot find any combination of paths/filenames that preserve the folder structure in the zip file.

Hi everyone!

In my application, system.DocumentsDirectory stores custom .png and .json files both in the root of system.DocumentsDirectory and in subfolders.

Is there a way to compress or zip these files and folders while maintaining the folder structure so that I can unpack the same way?