diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-02-26 23:33:04 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:34:42 +0300 |
commit | 7b61b446b63a3628cbacd91aaa975fd6b85a874f (patch) | |
tree | d7a77b051708b035d50015bdcaa7b2735f4a16df /coding | |
parent | adcdbd28b1e5164e945ae7a67318212af1c77500 (diff) |
added progress tracking to UnzipFile.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/zip_reader.cpp | 13 | ||||
-rw-r--r-- | coding/zip_reader.hpp | 5 |
2 files changed, 16 insertions, 2 deletions
diff --git a/coding/zip_reader.cpp b/coding/zip_reader.cpp index 68530bdeba..fa22648bee 100644 --- a/coding/zip_reader.cpp +++ b/coding/zip_reader.cpp @@ -73,7 +73,7 @@ bool ZipFileReader::IsZip(string const & zipContainer) } void ZipFileReader::UnzipFile(string const & zipContainer, string const & fileInZip, - string const & outFilePath) + string const & outFilePath, ProgressFn progressFn) { unzFile zip = unzOpen64(zipContainer.c_str()); if (!zip) @@ -85,12 +85,18 @@ void ZipFileReader::UnzipFile(string const & zipContainer, string const & fileIn if (UNZ_OK != unzOpenCurrentFile(zip)) MYTHROW(LocateZipException, ("Can't open file inside zip", fileInZip)); + + unz_file_info64 fileInfo; + if (UNZ_OK != unzGetCurrentFileInfo64(zip, &fileInfo, NULL, 0, NULL, 0, NULL, 0)) + MYTHROW(LocateZipException, ("Can't get uncompressed file size inside zip", fileInZip)); + MY_SCOPE_GUARD(currentFileGuard, bind(&unzCloseCurrentFile, zip)); try { FileWriter outFile(outFilePath); + int pos = 0; int readBytes; static size_t const BUF_SIZE = 4096; char buf[BUF_SIZE]; @@ -103,6 +109,11 @@ void ZipFileReader::UnzipFile(string const & zipContainer, string const & fileIn MYTHROW(InvalidZipException, ("Error", readBytes, "while unzipping", fileInZip, "from", zipContainer)); else break; + + pos += readBytes; + + if (progressFn) + progressFn(fileInfo.uncompressed_size, pos); } } catch (Exception const & e) diff --git a/coding/zip_reader.hpp b/coding/zip_reader.hpp index 1e6237f297..94c345a002 100644 --- a/coding/zip_reader.hpp +++ b/coding/zip_reader.hpp @@ -17,6 +17,9 @@ private: uint64_t m_uncompressedFileSize; public: + + typedef void (*ProgressFn)(int, int); + DECLARE_EXCEPTION(OpenZipException, OpenException); DECLARE_EXCEPTION(LocateZipException, OpenException); DECLARE_EXCEPTION(InvalidZipException, OpenException); @@ -28,7 +31,7 @@ public: /// @warning Can also throw Writer::OpenException and Writer::WriteException static void UnzipFile(string const & zipContainer, string const & fileInZip, - string const & outFilePath); + string const & outFilePath, ProgressFn progressFn = 0); static vector<string> FilesList(string const & zipContainer); /// Quick version without exceptions |