校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫 > Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾

Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾

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

在使用 quick-cocos2d-x 做項(xiàng)目熱更新的時(shí)候,我需要建立臨時(shí)文件夾以保存下載的更新包。在更新完成后,我需要?jiǎng)h除這些臨時(shí)文件和文件夾。

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

1. 使用C++

在 cocos2d-x 2.x 中的 AssetsManager 包中提供了一個(gè) CreateDirectory 方法。這個(gè)方法可以跨平臺(tái)支持創(chuàng)建文件夾。在實(shí)際項(xiàng)目中運(yùn)行沒有問題。

復(fù)制代碼 代碼如下:

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 范例中提供了一個(gè) reset 方法,這個(gè)方法使用系統(tǒng)命令遞歸刪除文件夾。
復(fù)制代碼 代碼如下:

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();
}


但是,這個(gè) reset 在 ios 模擬器中運(yùn)行的時(shí)候,xcode會(huì)報(bào)這樣的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.

因此,我轉(zhuǎn)而考慮另一個(gè)方案。

2. 純lua

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

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

讓我們擴(kuò)展一下 os 包。

復(fù)制代碼 代碼如下:

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 真機(jī)上測(cè)試成功。Windows系統(tǒng)、Mac OSX 以及 iOS 真機(jī)還沒有測(cè)試。我測(cè)試后會(huì)立即更新。

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

標(biāo)簽:德宏 儋州 宣城 金昌 臺(tái)灣 天門 天門 濰坊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾》,本文關(guān)鍵詞  Lua,中,實(shí)現(xiàn),遞歸,刪除,一個(gè),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾》相關(guān)的同類信息!
  • 本頁收集關(guān)于Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 荃湾区| 南宫市| 尖扎县| 西和县| 五指山市| 和平区| 清水河县| 揭东县| 松原市| 城固县| 奎屯市| 湘乡市| 颍上县| 睢宁县| 宜章县| 罗田县| 新民市| 东安县| 莱芜市| 大名县| 沁水县| 南乐县| 同江市| 琼海市| 建德市| 巴中市| 潮安县| 海盐县| 高要市| 信宜市| 仙居县| 祁阳县| 正宁县| 永丰县| 平泉县| 紫金县| 福贡县| 潍坊市| 正定县| 盈江县| 沛县|