From 486685e4b43de3000b7c04a1de6d89125a5b2989 Mon Sep 17 00:00:00 2001 From: Sergey Konovalov Date: Tue, 20 Feb 2018 14:54:31 +0300 Subject: [PATCH] [feat] Add COfficeUtils::GetFilesSize to determine commpressed/uncommpressed size of archive --- OfficeUtils/src/OfficeUtils.cpp | 12 +++++++++++ OfficeUtils/src/OfficeUtils.h | 1 + OfficeUtils/src/ZipUtilsCP.cpp | 35 ++++++++++++++++++++++++++++++++- OfficeUtils/src/ZipUtilsCP.h | 1 + 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/OfficeUtils/src/OfficeUtils.cpp b/OfficeUtils/src/OfficeUtils.cpp index f47a7ef784..0648afd8f4 100644 --- a/OfficeUtils/src/OfficeUtils.cpp +++ b/OfficeUtils/src/OfficeUtils.cpp @@ -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; + } +} diff --git a/OfficeUtils/src/OfficeUtils.h b/OfficeUtils/src/OfficeUtils.h index 9ba8156e7c..b5eb4917f7 100644 --- a/OfficeUtils/src/OfficeUtils.h +++ b/OfficeUtils/src/OfficeUtils.h @@ -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); }; diff --git a/OfficeUtils/src/ZipUtilsCP.cpp b/OfficeUtils/src/ZipUtilsCP.cpp index a42c8abe1e..ff81ecd819 100644 --- a/OfficeUtils/src/ZipUtilsCP.cpp +++ b/OfficeUtils/src/ZipUtilsCP.cpp @@ -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 ) diff --git a/OfficeUtils/src/ZipUtilsCP.h b/OfficeUtils/src/ZipUtilsCP.h index b719219c30..b32016125e 100644 --- a/OfficeUtils/src/ZipUtilsCP.h +++ b/OfficeUtils/src/ZipUtilsCP.h @@ -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); }