void DocXDocumentStore::GetDocumentTexts(const CStdString& sFileName, std::vector<std::string>& vDocumentTexts) const { vDocumentTexts.clear(); CZipArchive zipArchive; zipArchive.Open(sFileName, CZipArchive::zipOpenReadOnly); if( !zipArchive.IsClosed() ) { CZipWordArray ar; zipArchive.FindMatches( L"word\\\\*.xml", ar ); for( int uIndex = 0; uIndex < ar.GetSize(); uIndex++ ) { CZipFileHeader fhInfo; if( zipArchive.GetFileInfo( fhInfo, ar[uIndex] ) ) { const CZipString fileName( fhInfo.GetFileName() ); if( fileName.find_first_of( '\\' ) == fileName.find_last_of( '\\' ) ) { C2007DocFile mf; zipArchive.ExtractFile( ar[uIndex], mf ); const CStdStringA sDocText = mf.GetWTInnerText(); if( sDocText.size() > 0 ) vDocumentTexts.push_back( sDocText ); } } } zipArchive.Flush(); zipArchive.Close(); } }
bool CSimpleZip::Add( const void* src, const unsigned int& srcSize, void* dst, unsigned int& dstSize, std::string& szErrmsg) { if ( NULL==src ) { szErrmsg = "CSimpleZip: Invalid source."; return false; } bool bRet = false; CZipMemFile mfIn; CZipMemFile mfOut; CZipArchive zip; try { mfIn.Write(src, srcSize); zip.Open(mfOut, CZipArchive::zipCreate); zip.AddNewFile(mfIn, _T("temp.txt")); zip.Close(); int nLen = (int)mfOut.GetLength(); if ( NULL==dst || dstSize<nLen ) { dstSize = nLen; szErrmsg = "CSimpleZip: The size of destination buffer is too small."; return false; } BYTE* b = mfOut.Detach(); memcpy_s(dst, dstSize, b, nLen); dstSize = nLen; free(b); bRet = true; } catch(CZipException& e) { if (!zip.IsClosed()) zip.Close(); szErrmsg = e.GetErrorDescription().c_str(); } catch(...) { if (!zip.IsClosed()) zip.Close(); szErrmsg = "CSimpleZip: failed to add, catch exception."; } return bRet; }
bool CSimpleZip::Extract( const void* src, const unsigned int& srcSize, void* dst, unsigned int& dstSize, std::string& szErrmsg) { if ( NULL==src ) { szErrmsg = "CSimpleZip: Invalid source."; return false; } bool bRet = false; CZipMemFile mfIn; CZipMemFile mfOut; CZipArchive zip; CZipFileHeader fhInfo; try { mfIn.Write(src, srcSize); zip.Open(mfIn, CZipArchive::zipOpen); if ( !zip.GetFileInfo(fhInfo, 0) ) { szErrmsg = "CSimpleZip: Failed to GetFileInfo"; zip.Close(); return false; } if ( NULL==dst || dstSize<fhInfo.m_uUncomprSize ) { dstSize = fhInfo.m_uUncomprSize; szErrmsg = "CSimpleZip: The size of destination buffer is too small."; return false; } mfOut.SetLength( fhInfo.m_uUncomprSize ); ((CZipAbstractFile*)&mfOut)->SeekToBegin(); // may be needed when mfOut was used previously if ( !zip.ExtractFile(0, mfOut) ) { szErrmsg = "CSimpleZip: Failed to ExtractFile"; zip.Close(); return false; } zip.Close(); BYTE* b = mfOut.Detach(); //int nLen = (int)mfOut.GetLength(); // this is an error length value memcpy_s(dst, dstSize, b, fhInfo.m_uUncomprSize); dstSize = fhInfo.m_uUncomprSize; free(b); bRet = true; } catch(CZipException& e) { if (!zip.IsClosed()) zip.Close(); szErrmsg = e.GetErrorDescription().c_str(); } catch(...) { if (!zip.IsClosed()) zip.Close(); szErrmsg = "CSimpleZip: Failed to Extract, catch exception."; } return bRet; }