示例#1
0
文件: utils.cpp 项目: kangaroo/moon
bool
UnzipByteArrayToDir (GByteArray *array, const char *dir, CanonMode mode)
{
	zlib_filefunc_def zfuncs;
	memzip_ctx_t ctx;
	unzFile zipfile;
	bool rv;
	
	ctx.array = array;
	ctx.pos = 0;
	
	zfuncs.zopen_file = memzip_open;
	zfuncs.zread_file = memzip_read;
	zfuncs.zwrite_file = memzip_write;
	zfuncs.ztell_file = memzip_tell;
	zfuncs.zseek_file = memzip_seek;
	zfuncs.zclose_file = memzip_close;
	zfuncs.zerror_file = memzip_error;
	zfuncs.opaque = (void *) &ctx;
	
	if (!(zipfile = unzOpen2 (NULL, &zfuncs)))
		return false;
	
	rv = ExtractAll (zipfile, dir, mode);
	
	unzClose (zipfile);
	
	return rv;
}
示例#2
0
CClientExtractor::CClientExtractor(QWidget* parent, Qt::WindowFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);

	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	connect(ui.selectDir, SIGNAL(clicked()), this, SLOT(SelectDir()));
	connect(ui.ok, SIGNAL(clicked()), this, SLOT(ExtractAll()));
}
示例#3
0
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	TVINSERTSTRUCT tins; HTREEITEM hti; RECT rect; int i; char *s, *s2; SYSTEMTIME t;
	switch(uMsg)
	{
		case WM_CREATE:
			hstatus = CreateWindowEx(0, STATUSCLASSNAME, "Click on File then on Open... to open a file.", WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0, hWnd, (HMENU)1000, hinst, 0);
			GetClientRect(hstatus, &rect);
			statusheight = rect.bottom;
			GetClientRect(hWnd, &rect);
			htree = CreateWindowEx(0, WC_TREEVIEW, "Tree View", WS_VISIBLE | WS_CHILD | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS,
					0, 0, rect.right, rect.bottom-statusheight, hWnd, NULL, hinst, NULL);
			himl = ImageList_LoadImage(hinst, MAKEINTRESOURCE(IDB_ICONS), 16, 5, 0xFF00FF, IMAGE_BITMAP, LR_CREATEDIBSECTION);
			TreeView_SetImageList(htree, himl, TVSIL_NORMAL);
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDM_OPEN:
					if(GetOpenFileName(&ofn))
						LoadBCP(ofnname);
					break;
				case IDM_CLOSE:
					CloseBCP(); break;
				case IDM_EXTRACTALL:
					i = MessageBox(hwnd, "Are you sure you want to extract all the files from this archive to the bcpxtract directory?\nThis can take a lot of time depending on the size of the archive.", title, 48 | MB_YESNO);
					if(i == IDYES)
					{
						ExtractAll();
						MessageBox(hwnd, "All files were extracted to the bcpxtract directory.", title, 64);
					}
					break;
				case IDM_ABOUT:
					MessageBox(hWnd, "BCP Viewer\nVersion: " VERSION "\n(C) 2015-2017 AdrienTD\nLicensed under the MIT license.", title, 64); break;
				case IDM_QUIT:
					DestroyWindow(hWnd); break;
			} break;
		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->code)
			{
				case NM_DBLCLK:
					if(((LPNMHDR)lParam)->hwndFrom == htree)
					{
						fti.mask = TVIF_PARAM | TVIF_TEXT;
						fti.hItem = TreeView_GetSelection(htree);
						if(!fti.hItem) break;
						fti.pszText = cfname; fti.cchTextMax = 255;
						TreeView_GetItem(htree, &fti);
						if(fti.lParam == -1) break;
						ExtractFile(fti.lParam, fti.pszText);
						MessageBox(hWnd, "File extracted!", title, 64);
					}
					break;
				case TVN_SELCHANGED:
					if(((LPNMHDR)lParam)->hwndFrom == htree)
					{
						i = ((LPNMTREEVIEW)lParam)->itemNew.lParam;
						if(i == -1) {SetWindowText(hstatus, "Directory"); break;}
						FileTimeToSystemTime(&(fetime[i]), &t);
						s = malloc(256); s2 = malloc(256); s[255] = s2[255] = 0;
						GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &t, NULL, s, 255);
						GetTimeFormat(LOCALE_USER_DEFAULT, 0, &t, NULL, s2, 255);
						_snprintf(ubuf, 256, "Offset: 0x%08X, Archived size: %u, Uncompressed size: %u, Format: %u, Date: %s, Time: %s", fent[i].offset, fent[i].endos - fent[i].offset, fent[i].size, fent[i].form, s, s2);
						SetWindowText(hstatus, ubuf);
						free(s); free(s2);
					}
					break;
				case NM_RCLICK:
					if(((LPNMHDR)lParam)->hwndFrom == htree)
					{
						hti = TreeView_GetSelection(htree);
						if(!hti) break;
						s = GetItemPath(hti);
						if(s) CopyText(s);
					}
					break;
			}
			break;
#ifdef VIP_SHORTCUT
		case WM_CHAR:
			if(tolower(wParam) == 'l')
				LoadBCP("C:\\Program Files (x86)\\Empire Interactive\\Warrior Kings - Battles\\data_o.bcp");
			if(tolower(wParam) == 'x')
				ExtractAll();
			break;
#endif
		case WM_SIZE:
			MoveWindow(htree, 0, 0, LOWORD(lParam), HIWORD(lParam)-statusheight, FALSE);
			SendMessage(hstatus, WM_SIZE, 0, 0);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
		default:
			return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
	return 0;
}
示例#4
0
//FIXME: nuke this!
char *
Application::GetResourceAsPath (const Uri *resourceBase, const Uri *uri)
{
	const char *filename;
	char *dirname, *path;
	char *canonicalized_filename;
	ManagedStreamCallbacks stream;
	unzFile zipfile;
	struct stat st;
	char buf[4096];
	int nread;
	int fd;
	
	if (!get_resource_cb || !uri || uri->IsAbsolute ())
		return NULL;
	
	// construct the path name for this resource
	filename = uri->ToString ();
	canonicalized_filename = Deployment::GetCurrent ()->CanonicalizeFileName (filename, false);
	
	path = g_build_filename (GetResourceRoot(), canonicalized_filename, NULL);
	g_free (canonicalized_filename);
	
	if (g_stat (path, &st) != -1) {
		// path exists, we're done
		return path;
	}
	
	// create the directory for our resource (keeping the relative path intact)
	dirname = g_path_get_dirname (path);
	if (g_mkdir_with_parents (dirname, 0700) == -1 && errno != EEXIST) {
		g_free (dirname);
		g_free (path);
		return NULL;
	}
	
	g_free (dirname);
	
	stream = get_resource_cb (resourceBase, uri);
	
	if (!stream.handle) {
		g_free (path);
		return NULL;
	}
	
	// reset the stream to 0
	if (stream.CanSeek (stream.handle))
		stream.Seek (stream.handle, 0, SEEK_SET);
	
	// create and save the buffer to disk
	if ((fd = g_open (path, O_WRONLY | O_CREAT | O_EXCL, 0600)) == -1) {
		stream.Close (stream.handle);
		g_free (path);
		return NULL;
	}
	
	// write the stream to disk
	do {
		if ((nread = stream.Read (stream.handle, buf, 0, sizeof (buf))) <= 0)
			break;
		
		if (write_all (fd, buf, (size_t) nread) == -1) {
			stream.Close (stream.handle);
			g_unlink (path);
			g_free (path);
			close (fd);
			
			return NULL;
		}
	} while (true);
	
	stream.Close (stream.handle);
	close (fd);
	
	// check to see if the resource is zipped
	if (!(zipfile = unzOpen (path))) {
		// nope, not zipped...
		return path;
	}
	
	// create a directory to contain our unzipped content
	if (!(dirname = CreateTempDir (path))) {
		unzClose (zipfile);
		g_free (dirname);
		g_unlink (path);
		g_free (path);
		return NULL;
	}
	
	// unzip the contents
	if (!ExtractAll (zipfile, dirname, CanonModeResource)) {
		RemoveDir (dirname);
		unzClose (zipfile);
		g_free (dirname);
		g_unlink (path);
		g_free (path);
		return NULL;
	}
	
	unzClose (zipfile);
	g_unlink (path);
	
	if (g_rename (dirname, path) == -1) {
		RemoveDir (dirname);
		g_free (dirname);
		g_free (path);
		return NULL;
	}
	
	g_free (dirname);
	
	return path;
}