[feat] Add COfficeUtils::GetFilesSize to determine commpressed/uncommpressed size of archive

This commit is contained in:
Sergey Konovalov
2018-02-20 14:54:31 +03:00
parent 26a6a77b8d
commit 486685e4b4
4 changed files with 48 additions and 1 deletions

View File

@ -151,3 +151,15 @@ HRESULT COfficeUtils::CompressFilesFromMemory(const std::wstring& zipFile, const
*result = ZLibZipUtils::CompressFiles(zipFile.c_str(), data_source, pParam, compression_level) ? true : false;
return S_OK;
}
HRESULT COfficeUtils::GetFilesSize(const std::wstring& zipFile, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed)
{
if (ZLibZipUtils::GetFilesSize(zipFile.c_str(), searchPattern, nCommpressed, nUncommpressed))
{
return S_OK;
}
else
{
return S_FALSE;
}
}

View File

@ -52,6 +52,7 @@ public:
HRESULT LoadFileFromArchive (const std::wstring& zipFile, const std::wstring& filePath, BYTE** fileInBytes, ULONG& nFileSize);
HRESULT ExtractFilesToMemory (const std::wstring& zipFile, const ExtractedFileCallback& data_receiver, void* pParam, bool* result);
HRESULT CompressFilesFromMemory (const std::wstring& zipFile, const RequestFileCallback& data_source, void* pParam, short compression_level, bool* result);
HRESULT GetFilesSize (const std::wstring& zipFile, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed);
};

View File

@ -102,7 +102,9 @@ namespace ZLibZipUtils
int nBufferSize = MultiByteToWideChar( CP_OEMCP, 0, sVal, -1, NULL, 0 );
wchar_t* pBuffer = new wchar_t[nBufferSize];
MultiByteToWideChar( CP_OEMCP, 0, sVal, -1, pBuffer, nBufferSize );
std::wstring sRes(pBuffer, nBufferSize);
//If this parameter is -1, the function processes the entire input string, including the terminating null character.
//Therefore, the resulting Unicode string has a terminating null character, and the length returned by the function includes this character.
std::wstring sRes(pBuffer, nBufferSize - 1);
RELEASEARRAYOBJECTS(pBuffer);
return sRes;
#else
@ -953,6 +955,37 @@ int ZipDir( const WCHAR* dir, const WCHAR* outputFile, const OnProgressCallback*
return false;
}
bool GetFilesSize(const WCHAR* zip_file_path, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed)
{
nCommpressed = 0;
nUncommpressed = 0;
unzFile unzip_file_handle = unzOpenHelp(zip_file_path);
if (unzip_file_handle != NULL)
{
//todo implement true pattern
std::wstring searchExt = searchPattern.substr(2);
bool isEmptyPattern = 0 == searchExt.length();
do
{
char filename_inzip[256];
unz_file_info file_info;
unzGetCurrentFileInfo(unzip_file_handle, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
std::wstring filenameW = codepage_issue_fixFromOEM(filename_inzip);
std::transform(filenameW.begin(), filenameW.end(), filenameW.begin(), ::tolower);
if (isEmptyPattern || NSFile::GetFileExtention(filenameW) == searchExt)
{
nCommpressed += file_info.compressed_size;
nUncommpressed += file_info.uncompressed_size;
}
// else just skip the erroneous file
} while (UNZ_OK == unzGoToNextFile(unzip_file_handle));
unzClose (unzip_file_handle);
return true;
}
return false;
}
/*========================================================================================================*/
static unsigned int get_files_count( const WCHAR* dirname )

View File

@ -65,4 +65,5 @@ namespace ZLibZipUtils
bool LoadFileFromArchive(const WCHAR* zipFile, const WCHAR* filePathInZip, BYTE** fileInBytes, ULONG& nFileSize);
bool ExtractFiles(const WCHAR* zip_file_path, const ExtractedFileCallback& callback, void* pParam);
bool CompressFiles(const WCHAR* zip_file_path, const RequestFileCallback& callback, void* pParam, int compression_level);
bool GetFilesSize(const WCHAR* zip_file_path, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed);
}