// // Locates the requested resource and loads the file into memory. A pointer to the // file in memory is returned. The calling function is responsible for deleting the // memory. // char * DLLResourceManager::UnZipResourceMemberIntoMemory(std::wstring sZippedFileToRetrieve) { ZIPENTRY ze; int i; char *ibuf = 0; try { const wchar_t* szFileName = sZippedFileToRetrieve.c_str(); FindZipItem(hz,szFileName,true,&i,&ze); // - unzip to a membuffer ibuf = new char[ze.unc_size + 2]; UnzipItem(hz,i, ibuf, ze.unc_size); // cut off the buffer at the ze.unc_size ibuf[ze.unc_size] = EOF; // This was '\0', then '\n' (that didn't work well for the schema id list ibuf[ze.unc_size + 1] = EOF; } catch (...) { SgGenerateException("test", "UnZipResourceMemberIntoMemory"); } return ibuf; }
void CPropertiesFiles::OnBnClickedPropPreview() { // TODO: 在此加入控制項告知處理常式程式碼 CListBox* listBox=(CListBox*)this->GetDlgItem(IDC_PROP_FILELIST); int curSel=listBox->GetCurSel(); if (curSel>=0) { LPTSTR pszFile=(LPTSTR)malloc(listBox->GetTextLen(curSel)+1); HZIP hZip; ZIPENTRY ze; CARMDlg* dlg=(CARMDlg*)this->GetParentOwner()->GetParent(); arfile* arFile=(arfile*)dlg->m_lstFiles.GetItemDataPtr(dlg->m_lstFiles.GetCurSel()); listBox->GetText(curSel,pszFile); hZip=OpenZip(arFile->fileName,0,ZIP_FILENAME); if (hZip) { int nIndex; if (FindZipItem(hZip,pszFile,TRUE,&nIndex,&ze)!=ZR_ARGS) { LPBYTE pszContent=(LPBYTE)malloc(ze.unc_size); UnzipItem(hZip,nIndex,pszContent,ze.unc_size,ZIP_MEMORY); bitmapInfo=&ze; DialogBoxParam(theApp.m_hInstance,MAKEINTRESOURCE(IDD_PREVIEW),this->GetSafeHwnd(),(DLGPROC)CPropertiesFiles::previewWndProc,(LPARAM)pszContent); free(pszContent); } CloseZip(hZip); } free(pszFile); } }
PBYTE WINAPI duWindowManager::GetZipResource(LPCTSTR lpszResName, HANDLE &hMem, IStream *&pStream) { if (lpszResName == NULL || m_hZip == NULL) return NULL; TCHAR szDir[1024]; _tcsncpy(szDir, lpszResName, MAX_PATH); TCHAR *lpStartChar = szDir; TCHAR *lpChar = _tcschr(lpStartChar, '\\'); while (lpChar) { *lpChar = 0; if (SetUnzipBaseDir(m_hZip, lpStartChar) != ZR_OK) return NULL; lpChar++; lpStartChar = lpChar; lpChar = _tcschr(lpStartChar, '\\'); } _tcsncpy(szDir, lpszResName, MAX_PATH); lpChar = szDir; while (*lpChar) { if (*lpChar == '\\') *lpChar = '/'; lpChar++; } int nIndex = 0; ZIPENTRY ze; if (FindZipItem(m_hZip, szDir, true, &nIndex, &ze) != ZR_OK || nIndex == -1) return NULL; hMem = ::GlobalAlloc(GMEM_FIXED, ze.unc_size); ::CreateStreamOnHGlobal(hMem, FALSE, &pStream); if (pStream == NULL) { ::GlobalFree(hMem); hMem = NULL; return NULL; } PBYTE pByte = (PBYTE)::GlobalLock(hMem); if (UnzipItem(m_hZip, nIndex, pByte, ze.unc_size) != ZR_OK) { pStream->Release(); pStream = NULL; ::GlobalFree(hMem); hMem = NULL; return NULL; } SetUnzipBaseDir(m_hZip, _T("")); return pByte; }
QString ClassSpaceChecker::unzipFile(const QString &jarPath, const ClassFileContext *ctx) { QString output; bool ok = false; #if defined(Q_WS_WIN) QString f = jarPath; HZIP hz = OpenZip( (void *)f.toStdWString().c_str(), 0, ZIP_FILENAME ); if( !hz ) { QMessageBox::warning(this, "", tr("Jar file not found.")); ui.comboBox_JarFile->setFocus(); return ""; } do{ ZIPENTRYW ze; ZRESULT zr = GetZipItem( hz, -1, &ze ); if( zr != ZR_OK ) break; int i; zr = FindZipItem(hz, ctx->filePath.toStdWString().c_str(), true, &i, &ze); if( zr != ZR_OK ) break; //QString output = generateFileTempPath() + "Temp.class"; output = generateFileTempPath() + ctx->fullClassNameForKey + (ctx->javaFileFlag ? ".java" : ".class"); zr = UnzipItem(hz, i, (void*)output.toStdWString().c_str(), 0, ZIP_FILENAME); if( zr != ZR_OK ) break; ok = true; } while( false ); CloseZip(hz); #else // TODO : other platform(MacOS) unzip patch file return false; #endif if(ok) return output; return ""; }
void test_unzip() { wstring sFilePath; sFilePath = L"c:\\1.zip"; HZIP hz = OpenZip((void *)sFilePath.c_str(), 0, 2); if( hz == NULL ) { cout<<"Error opening zip file"<<endl; return; } ZIPENTRY ze; int i; if( FindZipItem(hz, L"1.txt", true, &i, &ze) != 0 ) { cout<<"Could not find ziped file"<<endl; return; } DWORD dwSize = ze.unc_size; if( dwSize == 0 ) { cout<<"File is empty"; return; } if ( dwSize > 4096*1024 ) { cout<<"File too large"; return; } BYTE* pByte = new BYTE[ dwSize + 1 ]; ZeroMemory(pByte, dwSize + 1); int res = UnzipItem(hz, i, pByte, dwSize, 3); if( res != 0x00000000 && res != 0x00000600) { delete[] pByte; CloseZip(hz); cout<<"Could not unzip file"; return; } CloseZip(hz); cout<<pByte<<endl; delete[] pByte; }
bool CZip::extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName, const QString &password) { HZIP hz = OpenZip(filePath.toStdString().c_str(), password.isEmpty() ? 0 : password.toStdString().c_str()); if(!hz) { return false; } SetUnzipBaseDir(hz,extDirPath.toStdString().c_str()); if (!singleFileName.isEmpty()) { ZIPENTRY ze; int i; FindZipItem(hz, singleFileName.toStdString().c_str(), true, &i, &ze); UnzipItem(hz, i, ze.name); CloseZip(hz); return true; } for (int i = 0; ;i++) { ZIPENTRY ze; ZRESULT zr = GetZipItem(hz, i, &ze); if (zr != ZR_OK) { if(zr == ZR_FAILED || zr == ZR_NOTFOUND) { CloseZip(hz); return false; } break; // no more } UnzipItem(hz, i, ze.name); } CloseZip(hz); return true; }
std::pair<size_t, std::unique_ptr<T[]>> OpticPack::fetchFile(HZIP handle, const std::string& file, bool text) { int index = -1; ZIPENTRY entry; ZRESULT zr = FindZipItem(handle, file.c_str(), true, &index, &entry); if(zr != ZR_OK) { throw OpticPackException(); } size_t bufferLen = text? entry.unc_size + 1 : entry.unc_size; std::unique_ptr<T[]> buffer(new T[bufferLen]); memset(buffer.get(), 0, bufferLen); zr = UnzipItem(handle, index, buffer.get(), entry.unc_size, ZIP_MEMORY); if(zr != ZR_OK && zr != ZR_MORE) { //Xzip doesn't work properly - this means nothing throw OpticPackException(); } return std::make_pair(bufferLen, std::move(buffer)); }
/////////////////////////////////////////////////////////////////////////////// // VerifyZip void CXZipTestDlg::VerifyZip(HZIP hz, LPCTSTR lpszFile) { #ifdef _UNICODE ZIPENTRYW ze; #else ZIPENTRY ze; #endif memset(&ze, 0, sizeof(ze)); int index = -1; ZRESULT zr = 0; zr = FindZipItem(hz, lpszFile, TRUE, &index, &ze); TRACE(_T("index=%d\n"), index); m_List.Printf(CXListBox::Black, CXListBox::White, 0, _T(" === Checking contents of zip entry %s ==="), lpszFile); if (zr == ZR_OK) m_List.Printf(CXListBox::Green, CXListBox::White, 0, _T(" FindZipItem returned OK")); else m_List.Printf(CXListBox::Red, CXListBox::White, 0, _T(" FindZipItem failed")); if (_tcscmp(lpszFile, ze.name) == 0) m_List.Printf(CXListBox::Green, CXListBox::White, 0, _T(" FindZipItem found name ==> OK")); else m_List.Printf(CXListBox::Red, CXListBox::White, 0, _T(" FindZipItem failed to find name")); TCHAR targetname[MAX_PATH]; _tcscpy(targetname, _T("_unzip")); _tcscat(targetname, lpszFile); // delete target file if it exists ::DeleteFile(targetname); zr = UnzipItem(hz, index, targetname, 0, ZIP_FILENAME); if (zr == ZR_OK) m_List.Printf(CXListBox::Green, CXListBox::White, 0, _T(" UnzipItem returned OK")); else m_List.Printf(CXListBox::Red, CXListBox::White, 0, _T(" UnzipItem failed")); if (_taccess(targetname, 04) == 0) m_List.Printf(CXListBox::Green, CXListBox::White, 0, _T(" Target file created OK")); else m_List.Printf(CXListBox::Red, CXListBox::White, 0, _T(" UnzipItem failed to create target file")); BOOL bResult = FALSE; BOOL bRet = Compare(lpszFile, targetname, &bResult); if (bRet && bResult) m_List.Printf(CXListBox::Green, CXListBox::White, 0, _T(" Target file matches original file ==> OK")); else m_List.Printf(CXListBox::Red, CXListBox::White, 0, _T(" Target file does not match original file")); // the target file is not deleted - you can inspect it after running test //::DeleteFile(targetname); }
int GetFileList(CString sZipName, std::map<std::string, std::string>& file_list) { strconv_t strconv; HZIP hz = OpenZip(sZipName, NULL); if(hz==NULL) return 1; int index = -1; ZIPENTRY ze; ZRESULT zr = FindZipItem(hz, _T("crashrpt.xml"), false, &index, &ze); if(zr!=ZR_OK) { CloseZip(hz); return 2; } CString sTempFileName = Utility::getTempFileName(); zr = UnzipItem(hz, index, sTempFileName); if(zr!=ZR_OK) { CloseZip(hz); return 2; } CString sTempDir = Utility::getTempFileName(); DeleteFile(sTempDir); BOOL bCreateDir = CreateDirectory(sTempDir, NULL); ATLASSERT(bCreateDir); bCreateDir; LPCSTR lpszTempFileName = strconv.t2a(sTempFileName.GetBuffer(0)); TiXmlDocument doc; bool bLoad = doc.LoadFile(lpszTempFileName); if(!bLoad) { CloseZip(hz); return 3; } TiXmlHandle hRoot = doc.FirstChild("CrashRpt"); if(hRoot.ToElement()==NULL) { CloseZip(hz); return 4; } TiXmlHandle fl = hRoot.FirstChild("FileList"); if(fl.ToElement()==0) { CloseZip(hz); return 5; } TiXmlHandle fi = fl.FirstChild("FileItem"); while(fi.ToElement()!=0) { const char* pszName = fi.ToElement()->Attribute("name"); const char* pszDesc = fi.ToElement()->Attribute("description"); if(pszName!=NULL && pszDesc!=NULL) { CString sFileName = pszName; CString sFilePathName = sTempDir + _T("\\") + CString(pszName); int index = -1; ZIPENTRY ze; ZRESULT zr = FindZipItem(hz, sFileName, false, &index, &ze); zr = UnzipItem(hz, index, sFilePathName); LPCSTR pszFilePathName = strconv.t2a(sFilePathName.GetBuffer(0)); file_list[pszFilePathName]=pszDesc; } fi = fi.ToElement()->NextSibling("FileItem"); } CloseZip(hz); return 0; }
void main() { CreateFiles(); HZIP hz; DWORD writ; // EXAMPLE 1 - create a zipfile from existing files hz = CreateZip(_T("\\simple1.zip"),0); ZipAdd(hz,_T("znsimple.bmp"), _T("\\simple.bmp")); ZipAdd(hz,_T("znsimple.txt"), _T("\\simple.txt")); CloseZip(hz); _tprintf(_T("Created '\\simple1.zip'\n")); // EXAMPLE 2 - unzip it with the names suggested in the zip hz = OpenZip(_T("\\simple1.zip"),0); SetUnzipBaseDir(hz,_T("\\")); ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index; for (int zi=0; zi<numitems; zi++) { GetZipItem(hz,zi,&ze); UnzipItem(hz,zi,ze.name); } CloseZip(hz); _tprintf(_T("Unzipped 'znsimple.bmp' and 'znsimple.txt' from 'simple1.zip'\n")); // EXAMPLE 3 - create an auto-allocated pagefile-based zip file from various sources // the second argument says how much address space to reserve for it. We can // afford to be generous: no address-space is allocated unless it's actually needed. hz = CreateZip(0,100000,"password"); // adding a conventional file... ZipAdd(hz,_T("znsimple.txt"), _T("\\simple.txt")); // adding something from memory... char buf[1000]; for (int zj=0; zj<1000; zj++) buf[zj]=(char)(zj&0x7F); ZipAdd(hz,_T("simple.dat"), buf,1000); // adding something from a pipe... #ifndef UNDER_CE HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0); DWORD WINAPI Ex3ThreadFunc(void *dat); HANDLE hthread = CreateThread(0,0,Ex3ThreadFunc,(void*)hwrite,0,0); ZipAddHandle(hz,_T("simple3.dat"), hread,1000); // the '1000' is optional, but it makes for nicer zip files if sizes are known in advance. WaitForSingleObject(hthread,INFINITE); CloseHandle(hthread); CloseHandle(hread); // the thread will close hwrite #endif // // and now that the zip is created in pagefile, let's do something with it: void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen); HANDLE hfz = CreateFile(_T("\\simple3 - pwd is 'password'.zip"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hfz,zbuf,zlen,&writ,NULL); CloseHandle(hfz); CloseZip(hz); _tprintf(_T("Created 'simple3.zip' via pagefile\n")); // EXAMPLE 4 - unzip directly from resource into a file // resource RT_RCDATA/#1 happens to be a zip file... HINSTANCE hInstance=GetModuleHandle(0); HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA); HANDLE hglob = LoadResource(hInstance,hrsrc); void *zipbuf=LockResource(hglob); unsigned int ziplen=SizeofResource(hInstance,hrsrc); hz = OpenZip(zipbuf, ziplen, 0); SetUnzipBaseDir(hz,_T("\\")); int i; FindZipItem(hz,_T("simple.jpg"),true,&i,&ze); // - unzip to a file - UnzipItem(hz,i,ze.name); // - unzip to a membuffer - char *ibuf = new char[ze.unc_size]; UnzipItem(hz,i, ibuf, ze.unc_size); delete[] ibuf; // - unzip to a fixed membuff, bit by bit - char fbuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0; while (zr==ZR_MORE) { zr = UnzipItem(hz,i, fbuf,1024); unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize; // now do something with this chunk which we just unzipped... totsize+=bufsize; } // - finished - CloseZip(hz); // note: no need to free resources obtained through Find/Load/LockResource _tprintf(_T("Unzipped 'simple.jpg' from resource zipfile\n")); DeleteFile(_T("\\simple.txt")); DeleteFile(_T("\\simple.bmp")); _tprintf(_T("Deleted 'simple.txt' and 'simple.bmp'\n")); }