Today, Corona Labs announced that Corona Plugins — code named “Project Gluon” — are live and available to Corona SDK Pro subscribers using Daily Build #1115 and above.
One of the new plugins handles the compression and un-com...
Today, Corona Labs announced that Corona Plugins — code named “Project Gluon” — are live and available to Corona SDK Pro subscribers using Daily Build #1115 and above.
One of the new plugins handles the compression and un-compression of files using the popular zip algorithm. For anybody who’s unfamiliar with zip files, the most common usage is when you need to group several related files into a “package” so it’s easier to move them around and share them with others. For example, if you contract a set of artwork from a designer, he/she can compress them into a zip archive and send them via email, Dropbox, etc. All of the artwork will be contained in that one file with the .zip extension, and when you open it locally, the file will “unzip” and all of the files will be available to you, exactly as the designer intended. Another use of zip files is to compress one large file into a smaller overall .zip file. The format and type of the original file will dictate the amount of compression achieved, but in virtually every instance, you’ll save valuable storage space.
Why is this important to mobile app developers? For one, you gain the ability to expand your app’s contents. You can create a zip file containing expansion art and sounds, download it into your app, unzip it, and use those assets. Secondly, you can now produce zip files which you can upload to web servers for distribution.
In today’s tutorial, let’s examine how to download a zip file from a web server, unzip the contents, and show a list of the files within.
Using Corona Plugins
Corona Plugins are “managed” from the build.settings file in your core project directory. This makes it easy to add just the specific plugins you need for an app, ultimately resulting in a lighter compiled binary.
To use a plugin, simply add the plugins table within settings and configure the zip plugin within:
settings =
{
orientation =
{
default = "portrait",
supported = { "portrait", "portraitUpsideDown" },
},
plugins =
{
["plugin.zip"] =
{
publisherId = "com.coronalabs",
},
},
}
Next, in your main project code, you must require the plugin, similar to how you’d require a core Corona library or external Lua module.
local zip = require( "plugin.zip" )
That’s it! Now you’re ready to use the zip plugin. Note that when you run a project in the Corona Simulator that requires a plugin, you’ll be prompted to download the plugin; this only happens once, when you first use the plugin.
Implementing .zip in Code
Like many API calls in Corona, a callback function is required so that you can determine when the process is complete and then take the appropriate action.
local function zipListener( event )
if ( event.isError ) then
print( "Unzip error" )
else
print( "event.name:" .. event.name )
print( "event.type:" .. event.type )
if ( event.response and type(event.response) == "table" ) then
for i = 1, #event.response do
print( event.response[i] )
end
end
end
end
Now, let’s use use the network.download API call to retrieve the zip file from a remote server and, in its callback listener, initiate the unzip process. The callback function will list all files that are successfully uncompressed.
local function networkListener( event )
if ( event.isError ) then
print( "Network error - download failed" )
elseif ( event.phase == "began" ) then
print( "Progress Phase: began" )
elseif ( event.phase == "ended" ) then
if ( math.floor(event.status/100) > 3 ) then
print( "Network error - download failed", event.status )
--NOTE: 404 errors (file not found) is actually a successful return,
--though you did not get a file, so trap for that
else
local options = {