static int S_archive_file_open(lua_State* L) { struct zip** ar = check_archive(L, 1); const char* path = (lua_isnumber(L, 2)) ? NULL : luaL_checkstring(L, 2); int path_idx = (lua_isnumber(L, 2)) ? luaL_checkint(L, 2)-1 : -1; int flags = (lua_gettop(L) < 3) ? 0 : luaL_checkint(L, 3); struct zip_file** file = (struct zip_file**) lua_newuserdata(L, sizeof(struct zip_file*)); *file = NULL; if ( ! *ar ) return 0; if ( NULL == path ) { *file = zip_fopen_index(*ar, path_idx, flags); } else { *file = zip_fopen(*ar, path, flags); } if ( ! *file ) { lua_pushnil(L); lua_pushstring(L, zip_strerror(*ar)); return 2; } luaL_getmetatable(L, ARCHIVE_FILE_MT); assert(!lua_isnil(L, -1)/* ARCHIVE_FILE_MT found? */); lua_setmetatable(L, -2); S_archive_add_ref(L, 1, 1, -1); return 1; }
static struct zip_source* S_create_source_zip(lua_State* L, struct zip* ar) { struct zip** other_ar = check_archive(L, 4); int file_idx = luaL_checkint(L, 5); int flags = lua_gettop(L) < 6 ? 0 : luaL_checkint(L, 6); int start = lua_gettop(L) < 7 ? 0 : luaL_checkint(L, 7); int len = lua_gettop(L) < 8 ? -1 : luaL_checkint(L, 8); struct zip_source* src = NULL; if ( ! *other_ar ) return; lua_getfenv(L, 1); lua_pushvalue(L, 4); lua_rawget(L, -2); if ( ! lua_isnil(L, -1) ) { lua_pushliteral(L, "Circular reference of zip sources is not allowed"); lua_error(L); } lua_pop(L, 1); /* We store a strong reference to prevent the "other" archive * from getting prematurely gc'ed, we also have the other archive * store a weak reference so the __gc metamethod will be called * if the "other" archive is closed before we are closed. */ S_archive_add_ref(L, 0, 1, 4); S_archive_add_ref(L, 1, 4, 1); src = zip_source_zip(ar, *other_ar, file_idx-1, flags, start, len); if ( NULL != src ) return src; lua_pushstring(L, zip_strerror(ar)); lua_error(L); }
static int S_archive_get_num_files(lua_State* L) { struct zip** ar = check_archive(L, 1); if ( ! *ar ) return 0; lua_pushinteger(L, zip_get_num_files(*ar)); return 1; }
/* Try to revert all changes and close the archive since the archive * was not explicitly closed. */ static int S_archive_gc(lua_State* L) { struct zip* ar = *check_archive(L, 1); if ( ! ar ) return 0; S_archive_gc_refs(L, 1); zip_unchange_all(ar); zip_close(ar); return 0; }
static int S_archive_set_archive_comment(lua_State* L) { struct zip** ar = check_archive(L, 1); size_t comment_len = 0; const char* comment = lua_isnil(L, 2) ? NULL : luaL_checklstring(L, 2, &comment_len); if ( ! *ar ) return 0; if ( 0 != zip_set_archive_comment(*ar, comment, comment_len) ) { lua_pushstring(L, zip_strerror(*ar)); lua_error(L); } return 0; }
static int S_archive_stat(lua_State* L) { struct zip** ar = check_archive(L, 1); const char* path = (lua_isnumber(L, 2)) ? NULL : luaL_checkstring(L, 2); int path_idx = (lua_isnumber(L, 2)) ? luaL_checkint(L, 2)-1 : -1; int flags = (lua_gettop(L) < 3) ? 0 : luaL_checkint(L, 3); struct zip_stat stat; int result; if ( ! *ar ) return 0; if ( NULL == path ) { result = zip_stat_index(*ar, path_idx, flags, &stat); } else { result = zip_stat(*ar, path, flags, &stat); } if ( result != 0 ) { lua_pushnil(L); lua_pushstring(L, zip_strerror(*ar)); return 2; } lua_createtable(L, 0, 8); lua_pushstring(L, stat.name); lua_setfield(L, -2, "name"); lua_pushinteger(L, stat.index+1); lua_setfield(L, -2, "index"); lua_pushnumber(L, stat.crc); lua_setfield(L, -2, "crc"); lua_pushnumber(L, stat.size); lua_setfield(L, -2, "size"); lua_pushnumber(L, stat.mtime); lua_setfield(L, -2, "mtime"); lua_pushnumber(L, stat.comp_size); lua_setfield(L, -2, "comp_size"); lua_pushnumber(L, stat.comp_method); lua_setfield(L, -2, "comp_method"); lua_pushnumber(L, stat.encryption_method); lua_setfield(L, -2, "encryption_method"); return 1; }
/* Explicitly close the archive, throwing an error if there are any * problems. */ static int S_archive_close(lua_State* L) { struct zip* ar = *check_archive(L, 1); int err; if ( ! ar ) return 0; S_archive_gc_refs(L, 1); err = zip_close(ar); if ( S_push_error(L, err, errno) ) lua_error(L); return 0; }
static int S_archive_get_archive_comment(lua_State* L) { struct zip** ar = check_archive(L, 1); int flags = (lua_gettop(L) < 2) ? 0 : luaL_checkint(L, 2); const char* comment; int comment_len; if ( ! *ar ) return 0; comment = zip_get_archive_comment(*ar, &comment_len, flags); if ( NULL == comment ) { lua_pushnil(L); } else { lua_pushlstring(L, comment, comment_len); } return 1; }
static int S_archive_get_name(lua_State* L) { struct zip** ar = check_archive(L, 1); int path_idx = luaL_checkint(L, 2)-1; int flags = (lua_gettop(L) < 3) ? 0 : luaL_checkint(L, 3); const char* name; if ( ! *ar ) return 0; name = zip_get_name(*ar, path_idx, flags); if ( NULL == name ) { lua_pushnil(L); lua_pushstring(L, zip_strerror(*ar)); return 2; } lua_pushstring(L, name); return 1; }
static int S_archive_name_locate(lua_State* L) { struct zip** ar = check_archive(L, 1); const char* fname = luaL_checkstring(L, 2); int flags = (lua_gettop(L) < 3) ? 0 : luaL_checkint(L, 3); int idx; if ( ! *ar ) return 0; idx = zip_name_locate(*ar, fname, flags); if ( idx < 0 ) { lua_pushnil(L); lua_pushstring(L, zip_strerror(*ar)); return 2; } lua_pushinteger(L, idx+1); return 1; }
static int S_archive_add_dir(lua_State* L) { struct zip** ar = check_archive(L, 1); const char* path = luaL_checkstring(L, 2); int idx; if ( ! *ar ) return 0; idx = zip_add_dir(*ar, path) + 1; if ( 0 == idx ) { lua_pushstring(L, zip_strerror(*ar)); lua_error(L); return 0; } lua_pushinteger(L, idx); return 1; }
static int S_archive_replace(lua_State* L) { struct zip** ar = check_archive(L, 1); int idx = luaL_checkinteger(L, 2); struct zip_source* src = S_create_source(L, *ar); if ( ! *ar ) return 0; idx = zip_replace(*ar, idx-1, src) + 1; if ( 0 == idx ) { zip_source_free(src); lua_pushstring(L, zip_strerror(*ar)); lua_error(L); } S_archive_add_ref(L, 0, 1, 4); lua_pushinteger(L, idx); return 1; }
BOOL CALLBACK MainDialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: if (check_archive() == false) { MessageBox(hWnd,"自己展開書庫ではありません。","IMP Self Extract Archiver",MB_OK); EndDialog(hWnd,TRUE); return TRUE; } { char dir[MAX_PATH * 2]; GetCurrentDirectory(MAX_PATH * 2,dir); SetDlgItemText(hWnd,IDC_OUTDIR,dir); } return TRUE; case WM_SYSCOMMAND: if (extract_flag == false && wParam == SC_CLOSE) { EndDialog(hWnd,TRUE); return TRUE; } return FALSE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_FINDDIR: if (extract_flag == false) { char dir[MAX_PATH * 2]; if (DirectoryRefDialog(hWnd,dir) == true) { SetDlgItemText(hWnd,IDC_OUTDIR,dir); } } return TRUE; case IDC_EXTRACT: if (extract_flag == false) { extract_flag = true; EnableWindow(GetDlgItem(hWnd,IDC_FINDDIR),FALSE); EnableWindow(GetDlgItem(hWnd,IDC_EXTRACT),FALSE); _beginthread(extract_archive,0,(void*)hWnd); while (extract_flag == true) { MSG msg; while (PeekMessage(&msg,(HWND)NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } EnableWindow(GetDlgItem(hWnd,IDC_FINDDIR),TRUE); EnableWindow(GetDlgItem(hWnd,IDC_EXTRACT),TRUE); SetDlgItemText(hWnd,IDC_FILENAME,""); EndDialog(hWnd,TRUE); } return TRUE; } return FALSE; } return FALSE; }