added progress tracking to UnzipFile.

This commit is contained in:
rachytski 2012-02-26 23:33:04 +04:00 committed by Alex Zolotarev
parent adcdbd28b1
commit 7b61b446b6
2 changed files with 16 additions and 2 deletions

View file

@ -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)

View file

@ -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