static int lmz_zip_pusherror(lua_State *L, mz_zip_archive *za, const char *prefix) { mz_zip_error err = mz_zip_get_last_error(za); const char *emsg = mz_zip_get_error_string(err); lua_pushnil(L); if (prefix == NULL) lua_pushstring(L, emsg); else lua_pushfstring(L, "%s: %s", prefix, emsg); return 2; }
static int lmz_reader_init(lua_State* L) { const char* path = luaL_checkstring(L, 1); mz_uint32 flags = luaL_optint(L, 2, 0); mz_uint64 size; lmz_file_t* zip = lua_newuserdata(L, sizeof(*zip)); mz_zip_archive* archive = &(zip->archive); luaL_getmetatable(L, "miniz_reader"); lua_setmetatable(L, -2); memset(archive, 0, sizeof(*archive)); zip->loop = uv_default_loop(); zip->fd = uv_fs_open(zip->loop, &(zip->req), path, O_RDONLY, 0644, NULL); uv_fs_fstat(zip->loop, &(zip->req), zip->fd, NULL); size = zip->req.statbuf.st_size; archive->m_pRead = lmz_file_read; archive->m_pIO_opaque = zip; if (!mz_zip_reader_init(archive, size, flags)) { lua_pushnil(L); lua_pushfstring(L, "read %s fail because of %s", path, mz_zip_get_error_string(mz_zip_get_last_error(archive))); return 2; } return 1; }