婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Lua中實現遞歸刪除一個文件夾

Lua中實現遞歸刪除一個文件夾

熱門標簽:新岸線智能電銷機器人 地圖標注大廈 清朝地圖標注哈爾濱 冀州市地圖標注 武漢外呼防封系統多少錢 百度地圖標注早餐區域 漳州智云呼電話機器人 個人怎么在地圖標注需要的店鋪 怎么去除地圖標注

在使用 quick-cocos2d-x 做項目熱更新的時候,我需要建立臨時文件夾以保存下載的更新包。在更新完成后,我需要刪除這些臨時文件和文件夾。

cocos2d-x 和 quick-cocos2d-x 都沒有提供刪除文件夾功能。我做了如下2個嘗試:

1. 使用C++

在 cocos2d-x 2.x 中的 AssetsManager 包中提供了一個 CreateDirectory 方法。這個方法可以跨平臺支持創建文件夾。在實際項目中運行沒有問題。

復制代碼 代碼如下:

bool AssetsManager::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    mode_t processMask = umask(0);
    int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
    umask(processMask);
    if (ret != 0 (errno != EEXIST))
    {
        return false;
    }

    return true;
#else
    BOOL ret = CreateDirectoryA(path, NULL);
if (!ret ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
    return true;
#endif
}


在 cocos2d-x 2.x 的 AssetsManager sample 范例中提供了一個 reset 方法,這個方法使用系統命令遞歸刪除文件夾。
復制代碼 代碼如下:

void UpdateLayer::reset(cocos2d::CCObject *pSender)
{
    pProgressLabel->setString(" ");

    // Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    string command = "rm -r ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#else
    string command = "rd /s /q ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#endif
    // Delete recorded version codes.
    getAssetsManager()->deleteVersion();

    createDownloadedDir();
}


但是,這個 reset 在 ios 模擬器中運行的時候,xcode會報這樣的warinng:

The iOS Simulator libSystem was initialized out of order. This is most often caused by running host executables or inserting host dylibs. In the future, this will cause an abort.

因此,我轉而考慮另一個方案。

2. 純lua

純 lua 其實是個噱頭。這里還是要依賴 lfs(lua file sytem),好在 quick-cocos2d-x 已經包含了這個庫。

lfs.rmdir 命令 和 os.remove 命令一樣,只能刪除空文件夾。因此實現類似 rm -rf 的功能, 必須要遞歸刪除文件夾中所有的文件和子文件夾。

讓我們擴展一下 os 包。

復制代碼 代碼如下:

require("lfs")

function os.exists(path)
    return CCFileUtils:sharedFileUtils():isFileExist(path)
end

function os.mkdir(path)
    if not os.exists(path) then
        return lfs.mkdir(path)
    end
    return true
end

function os.rmdir(path)
    print("os.rmdir:", path)
    if os.exists(path) then
        local function _rmdir(path)
            local iter, dir_obj = lfs.dir(path)
            while true do
                local dir = iter(dir_obj)
                if dir == nil then break end
                if dir ~= "." and dir ~= ".." then
                    local curDir = path..dir
                    local mode = lfs.attributes(curDir, "mode")
                    if mode == "directory" then
                        _rmdir(curDir.."/")
                    elseif mode == "file" then
                        os.remove(curDir)
                    end
                end
            end
            local succ, des = os.remove(path)
            if des then print(des) end
            return succ
        end
        _rmdir(path)
    end
    return true
end


上面的代碼在 iOS 模擬器和 Android 真機上測試成功。Windows系統、Mac OSX 以及 iOS 真機還沒有測試。我測試后會立即更新。

您可能感興趣的文章:
  • Lua中的基本語法、控制語句總結
  • Lua簡介、編譯安裝教程及變量等語法介紹
  • LUA中的閉包(closure)淺析
  • 安裝Nginx+Lua開發環境
  • lua實現的2048小游戲
  • Lua教程(二):語法約定
  • Lua教程(三):值與類型介紹
  • ubuntu 14.04下熟悉lua的語法

標簽:德宏 儋州 宣城 金昌 臺灣 天門 天門 濰坊

巨人網絡通訊聲明:本文標題《Lua中實現遞歸刪除一個文件夾》,本文關鍵詞  Lua,中,實現,遞歸,刪除,一個,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua中實現遞歸刪除一個文件夾》相關的同類信息!
  • 本頁收集關于Lua中實現遞歸刪除一個文件夾的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 开化县| 垣曲县| 克山县| 荆州市| 渑池县| 木里| 宾川县| 灵武市| 深圳市| 离岛区| 商都县| 雅安市| 冷水江市| 房产| 南部县| 青河县| 刚察县| 攀枝花市| 武强县| 松江区| 湘潭县| 崇礼县| 天津市| 海原县| 水城县| 宁德市| 湖州市| 曲沃县| 昭通市| 招远市| 甘肃省| 南靖县| 秦皇岛市| 太和县| 鸡东县| 山丹县| 伊金霍洛旗| 望谟县| 百色市| 东乡县| 扎兰屯市|