Skip to content

Instantly share code, notes, and snippets.

@fengyuanyang
Created September 5, 2013 04:32
Show Gist options
  • Save fengyuanyang/6446147 to your computer and use it in GitHub Desktop.
Save fengyuanyang/6446147 to your computer and use it in GitHub Desktop.
local zip = require( "plugin.zip" ) -- 呼應build.settings裡的["plugin.zip"]
local function zipListener( event ) -- 解壓縮完成後呼叫此處
if ( event.isError ) then -- 若解壓有錯誤會跑至此處
print( "Unzip error" )
else
print( "event.name:" .. event.name ) -- event.name:zip
print( "event.type:" .. event.type ) -- event.type:uncompress
if ( event.response and type(event.response) == "table" ) then -- 這裡會印出所有在ZIP檔的檔名
for i = 1, #event.response do
print( event.response[i] )
end
end
end
end
local function networkListener( event ) -- 判斷ZIP下載成功或失敗
if ( event.isError ) then
print( "Network error - download failed" )
elseif ( event.phase == "began" ) then
print( "Progress Phase: began" )
elseif ( event.phase == "progress" ) then -- 當progress 設為download 時,下載階段將會回傳下載進度,讓使用者能知道進度
if event.bytesEstimated <= 0 then
print( "Download progress: " .. event.bytesTransferred )
else
print( "Download progress: " .. event.bytesTransferred .. " of estimated: " .. event.bytesEstimated )
end
elseif ( event.phase == "ended" ) then
if ( math.floor(event.status/100) > 3 ) then
--會得到一個網路的status code,3xx以上的代表下載失敗
--404代表找不到檔案
print( "Network error - download failed", event.status )
else -- 成功的話跑這裡
local options = { -- 設置壓縮或解壓的參數
zipFile = event.response.filename, -- 在這裡filename會是test.zip
zipBaseDir = event.response.baseDirectory, -- zip檔所在位置
dstBaseDir = system.DocumentsDirectory, -- 解壓縮後目標位置
listener = zipListener,
}
zip.uncompress( options ) -- 完成後會呼叫zipListener
end
end
end
local params = {}
params.progress = "download" -- 設progress 為"download"就會回傳下載進度喔!!
local URL = "http://coronaisland.s3.amazonaws.com/coronabook/coronaisland_ziptest.zip"
network.download( URL, "GET", networkListener, params, "test.zip", system.TemporaryDirectory ) -- 預設一個下載檔名後放在暫存資料夾來作處理
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment