Refactoring

This commit is contained in:
Oleg Korshul
2023-07-17 16:03:01 +03:00
parent 26f344ab99
commit 1aa1d63f61
3 changed files with 305 additions and 305 deletions

View File

@ -42,11 +42,11 @@ COfficeUtils::COfficeUtils(OnProgressCallback* fCallback)
HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& _zipFile, const std::wstring& _unzipDir, wchar_t* password, SHORT extract_without_path)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring unzipDir = CorrectPathW(_unzipDir);
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring unzipDir = CorrectPathW(_unzipDir);
#else
std::wstring zipFile = _zipFile;
std::wstring unzipDir = _unzipDir;
std::wstring zipFile = _zipFile;
std::wstring unzipDir = _unzipDir;
#endif
if( ZLibZipUtils::UnzipToDir( zipFile.c_str(), unzipDir.c_str(), m_fCallback, password, ( extract_without_path > 0 ) ? (true) : (false) ) == 0 )
@ -63,17 +63,17 @@ HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& _zipFile, const std
HRESULT COfficeUtils::CompressFileOrDirectory(const std::wstring& _name, const std::wstring& _outputFile, bool bSorted, int method, short level, bool bDateTime)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring name = _name;//CorrectPathW(_name);
std::wstring outputFile = CorrectPathW(_outputFile);
std::wstring name = _name;//CorrectPathW(_name);
std::wstring outputFile = CorrectPathW(_outputFile);
#else
std::wstring name = _name;
std::wstring outputFile = _outputFile;
std::wstring name = _name;
std::wstring outputFile = _outputFile;
#endif
HRESULT result = S_FALSE;
if(NSDirectory::Exists(name))
{
if ( ZLibZipUtils::ZipDir( name.c_str(), outputFile.c_str(), m_fCallback, bSorted, method, level, bDateTime ) == 0 )
if ( ZLibZipUtils::ZipDir( name.c_str(), outputFile.c_str(), m_fCallback, bSorted, method, level, bDateTime ) == 0 )
{
result = S_OK;
}
@ -84,126 +84,126 @@ HRESULT COfficeUtils::CompressFileOrDirectory(const std::wstring& _name, const s
}
else if(NSFile::CFileBinary::Exists(name))
{
if ( ZLibZipUtils::ZipFile( name.c_str(), outputFile.c_str(), method, level, bDateTime ) == 0 )
if ( ZLibZipUtils::ZipFile( name.c_str(), outputFile.c_str(), method, level, bDateTime ) == 0 )
{
result = S_OK;
}
else
{
result = S_FALSE;
}
}
}
return result;
}
HRESULT COfficeUtils::Uncompress(BYTE* destBuf, ULONG* destSize, BYTE* sourceBuf, ULONG sourceSize)
{
if ( ZLibZipUtils::UncompressBytes( destBuf, destSize, sourceBuf, sourceSize ) == Z_OK )
{
return S_OK;
}
else
{
return S_FALSE;
}
if ( ZLibZipUtils::UncompressBytes( destBuf, destSize, sourceBuf, sourceSize ) == Z_OK )
{
return S_OK;
}
else
{
return S_FALSE;
}
}
HRESULT COfficeUtils::Compress(BYTE* destBuf, ULONG* destSize, BYTE* sourceBuf, ULONG sourceSize, short level)
{
if ( ZLibZipUtils::CompressBytes( destBuf, destSize, sourceBuf, sourceSize, level ) == Z_OK )
{
return S_OK;
}
else
{
return S_FALSE;
}
if ( ZLibZipUtils::CompressBytes( destBuf, destSize, sourceBuf, sourceSize, level ) == Z_OK )
{
return S_OK;
}
else
{
return S_FALSE;
}
}
HRESULT COfficeUtils::IsArchive(const std::wstring& _filename)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring filename = CorrectPathW(_filename);
std::wstring filename = CorrectPathW(_filename);
#else
std::wstring filename = _filename;
std::wstring filename = _filename;
#endif
if( ZLibZipUtils::IsArchive(filename.c_str()) )
{
return S_OK;
}
else
{
return S_FALSE;
}
if( ZLibZipUtils::IsArchive(filename.c_str()) )
{
return S_OK;
}
else
{
return S_FALSE;
}
}
HRESULT COfficeUtils::IsFileExistInArchive(const std::wstring& _zipFile, const std::wstring& filePath)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring zipFile = CorrectPathW(_zipFile);
#else
std::wstring zipFile = _zipFile;
std::wstring zipFile = _zipFile;
#endif
if( ZLibZipUtils::IsFileExistInArchive( zipFile.c_str(), filePath.c_str()) )
{
return S_OK;
}
else
{
return S_FALSE;
}
if( ZLibZipUtils::IsFileExistInArchive( zipFile.c_str(), filePath.c_str()) )
{
return S_OK;
}
else
{
return S_FALSE;
}
}
HRESULT COfficeUtils::LoadFileFromArchive(const std::wstring& _zipFile, const std::wstring& filePath, BYTE** fileInBytes, ULONG& nFileSize)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring zipFile = CorrectPathW(_zipFile);
#else
std::wstring zipFile = _zipFile;
std::wstring zipFile = _zipFile;
#endif
if( ZLibZipUtils::LoadFileFromArchive( zipFile.c_str(), filePath.c_str(), fileInBytes, nFileSize))
{
return S_OK;
}
else
{
return S_FALSE;
}
if( ZLibZipUtils::LoadFileFromArchive( zipFile.c_str(), filePath.c_str(), fileInBytes, nFileSize))
{
return S_OK;
}
else
{
return S_FALSE;
}
}
HRESULT COfficeUtils::ExtractFilesToMemory(const std::wstring& _zipFile, const ExtractedFileCallback& data_receiver, void* pParam, bool* result)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring zipFile = CorrectPathW(_zipFile);
#else
std::wstring zipFile = _zipFile;
std::wstring zipFile = _zipFile;
#endif
*result = ZLibZipUtils::ExtractFiles(zipFile.c_str(), data_receiver, pParam) ? true : false;
*result = ZLibZipUtils::ExtractFiles(zipFile.c_str(), data_receiver, pParam) ? true : false;
return S_OK;
}
HRESULT COfficeUtils::CompressFilesFromMemory(const std::wstring& _zipFile, const RequestFileCallback& data_source, void* pParam, SHORT compression_level, bool* result)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring zipFile = CorrectPathW(_zipFile);
#else
std::wstring zipFile = _zipFile;
std::wstring zipFile = _zipFile;
#endif
*result = ZLibZipUtils::CompressFiles(zipFile.c_str(), data_source, pParam, compression_level) ? true : false;
*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, ULONG64& nCommpressed, ULONG64& nUncommpressed)
{
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64)
std::wstring zipFile = CorrectPathW(_zipFile);
std::wstring zipFile = CorrectPathW(_zipFile);
#else
std::wstring zipFile = _zipFile;
std::wstring zipFile = _zipFile;
#endif
if (ZLibZipUtils::GetFilesSize(zipFile.c_str(), searchPattern, nCommpressed, nUncommpressed))
@ -218,166 +218,166 @@ HRESULT COfficeUtils::GetFilesSize(const std::wstring& _zipFile, const std::wstr
int COfficeUtils::GetAddonFlag()
{
return zlip_get_addition_flag();
return zlip_get_addition_flag();
}
void COfficeUtils::SetAddonFlag(int flag)
{
zlip_set_addition_flag(flag);
zlip_set_addition_flag(flag);
}
class CDeflate_private
{
public:
z_stream m_stream;
z_stream m_stream;
public:
CDeflate_private()
{
memset(&m_stream, 0x00, sizeof(z_stream));
}
CDeflate_private()
{
memset(&m_stream, 0x00, sizeof(z_stream));
}
};
CDeflate::CDeflate()
{
m_internal = new CDeflate_private();
m_internal = new CDeflate_private();
}
CDeflate::~CDeflate()
{
delete m_internal;
delete m_internal;
}
void CDeflate::SetIn(BYTE* next_in, UINT avail_in, ULONG total_in)
{
m_internal->m_stream.next_in = next_in;
m_internal->m_stream.avail_in = avail_in;
m_internal->m_stream.next_in = next_in;
m_internal->m_stream.avail_in = avail_in;
if (-1 != total_in)
m_internal->m_stream.total_in = total_in;
if (-1 != total_in)
m_internal->m_stream.total_in = total_in;
}
void CDeflate::SetOut(BYTE* next_out, UINT avail_out, ULONG total_out)
{
m_internal->m_stream.next_out = next_out;
m_internal->m_stream.avail_out = avail_out;
m_internal->m_stream.next_out = next_out;
m_internal->m_stream.avail_out = avail_out;
if (-1 != total_out)
m_internal->m_stream.total_out = total_out;
if (-1 != total_out)
m_internal->m_stream.total_out = total_out;
}
UINT CDeflate::GetAvailIn()
{
return m_internal->m_stream.avail_in;
return m_internal->m_stream.avail_in;
}
UINT CDeflate::GetAvailOut()
{
return m_internal->m_stream.avail_out;
return m_internal->m_stream.avail_out;
}
void CDeflate::Init(int level, int stream_size)
{
deflateInit_(&m_internal->m_stream, level, ZLIB_VERSION, (stream_size == -1) ? sizeof(z_stream) : stream_size);
deflateInit_(&m_internal->m_stream, level, ZLIB_VERSION, (stream_size == -1) ? sizeof(z_stream) : stream_size);
}
int CDeflate::Process(int flush)
{
return deflate(&m_internal->m_stream, flush);
return deflate(&m_internal->m_stream, flush);
}
void CDeflate::End()
{
deflateEnd(&m_internal->m_stream);
deflateEnd(&m_internal->m_stream);
}
///////////////////////////////////////////
class CInflate_private
{
public:
z_stream m_stream;
z_stream m_stream;
public:
CInflate_private()
{
memset(&m_stream, 0x00, sizeof(z_stream));
}
CInflate_private()
{
memset(&m_stream, 0x00, sizeof(z_stream));
}
};
CInflate::CInflate()
{
m_internal = new CInflate_private();
m_internal = new CInflate_private();
}
CInflate::~CInflate()
{
delete m_internal;
delete m_internal;
}
void CInflate::SetIn(BYTE* next_in, UINT avail_in, ULONG total_in)
{
m_internal->m_stream.next_in = next_in;
m_internal->m_stream.avail_in = avail_in;
m_internal->m_stream.next_in = next_in;
m_internal->m_stream.avail_in = avail_in;
if (-1 != total_in)
m_internal->m_stream.total_in = total_in;
if (-1 != total_in)
m_internal->m_stream.total_in = total_in;
}
void CInflate::SetOut(BYTE* next_out, UINT avail_out, ULONG total_out)
{
m_internal->m_stream.next_out = next_out;
m_internal->m_stream.avail_out = avail_out;
m_internal->m_stream.next_out = next_out;
m_internal->m_stream.avail_out = avail_out;
if (-1 != total_out)
m_internal->m_stream.total_out = total_out;
if (-1 != total_out)
m_internal->m_stream.total_out = total_out;
}
UINT CInflate::GetAvailIn()
{
return m_internal->m_stream.avail_in;
return m_internal->m_stream.avail_in;
}
UINT CInflate::GetAvailOut()
{
return m_internal->m_stream.avail_out;
return m_internal->m_stream.avail_out;
}
void CInflate::Init()
{
inflateInit(&m_internal->m_stream);
inflateInit(&m_internal->m_stream);
}
void CInflate::Init2()
{
inflateInit2(&m_internal->m_stream,-MAX_WBITS);
inflateInit2(&m_internal->m_stream,-MAX_WBITS);
}
int CInflate::Process(int flush)
{
return inflate(&m_internal->m_stream, flush);
return inflate(&m_internal->m_stream, flush);
}
void CInflate::End()
{
inflateEnd(&m_internal->m_stream);
inflateEnd(&m_internal->m_stream);
}
void CInflate::ClearFuncs()
{
m_internal->m_stream.zalloc = Z_NULL;
m_internal->m_stream.zfree = Z_NULL;
m_internal->m_stream.opaque = Z_NULL;
m_internal->m_stream.zalloc = Z_NULL;
m_internal->m_stream.zfree = Z_NULL;
m_internal->m_stream.opaque = Z_NULL;
}
namespace NSZip
{
bool Decompress(const BYTE* pSrcBuffer, const ULONG& lSrcBufferLen, BYTE* pDstBuffer, ULONG& lDstBufferLen)
{
try
{
if (Z_OK == uncompress(pDstBuffer, &lDstBufferLen, pSrcBuffer, lSrcBufferLen))
{
return true;
}
}
catch(...)
{
}
return false;
}
bool Decompress(const BYTE* pSrcBuffer, const ULONG& lSrcBufferLen, BYTE* pDstBuffer, ULONG& lDstBufferLen)
{
try
{
if (Z_OK == uncompress(pDstBuffer, &lDstBufferLen, pSrcBuffer, lSrcBufferLen))
{
return true;
}
}
catch(...)
{
}
return false;
}
}

View File

@ -40,7 +40,7 @@
#define ZLIB_ADDON_FLAG_WINDOWS_SHARED_WRITE 2
#ifndef Z_DEFLATED
#define Z_DEFLATED 8
#define Z_DEFLATED 8
#endif
class KERNEL_DECL COfficeUtils
@ -51,7 +51,7 @@ public:
COfficeUtils(OnProgressCallback* bCallback = NULL);
HRESULT ExtractToDirectory (const std::wstring& zipFile, const std::wstring& unzipDir, wchar_t* password, short extract_without_path);
HRESULT CompressFileOrDirectory (const std::wstring& name, const std::wstring& outputFile, bool bSorted = false, int method = Z_DEFLATED, short level = -1, bool bDateTime = false);
HRESULT CompressFileOrDirectory (const std::wstring& name, const std::wstring& outputFile, bool bSorted = false, int method = Z_DEFLATED, short level = -1, bool bDateTime = false);
HRESULT Uncompress (BYTE* destBuf, ULONG* destSize, BYTE* sourceBuf, ULONG sourceSize);
HRESULT Compress (BYTE* destBuf, ULONG* destSize, BYTE* sourceBuf, ULONG sourceSize, short level = -1);
@ -63,8 +63,8 @@ public:
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, ULONG64& nCommpressed, ULONG64& nUncommpressed);
static int GetAddonFlag();
static void SetAddonFlag(int flag);
static int GetAddonFlag();
static void SetAddonFlag(int flag);
};
#define DEFLATE_NO_FLUSH 0
@ -93,52 +93,52 @@ class CDeflate_private;
class KERNEL_DECL CDeflate
{
private:
CDeflate_private* m_internal;
CDeflate_private* m_internal;
public:
CDeflate();
~CDeflate();
CDeflate();
~CDeflate();
public:
void SetIn(BYTE* next_in, UINT avail_in, ULONG total_in = -1);
void SetOut(BYTE* next_out, UINT avail_out, ULONG total_out = -1);
void SetIn(BYTE* next_in, UINT avail_in, ULONG total_in = -1);
void SetOut(BYTE* next_out, UINT avail_out, ULONG total_out = -1);
UINT GetAvailIn();
UINT GetAvailOut();
UINT GetAvailIn();
UINT GetAvailOut();
void Init(int level, int stream_size = -1);
int Process(int flush);
void End();
void Init(int level, int stream_size = -1);
int Process(int flush);
void End();
};
class CInflate_private;
class KERNEL_DECL CInflate
{
private:
CInflate_private* m_internal;
CInflate_private* m_internal;
public:
CInflate();
~CInflate();
CInflate();
~CInflate();
public:
void SetIn(BYTE* next_in, UINT avail_in, ULONG total_in = -1);
void SetOut(BYTE* next_out, UINT avail_out, ULONG total_out = -1);
void SetIn(BYTE* next_in, UINT avail_in, ULONG total_in = -1);
void SetOut(BYTE* next_out, UINT avail_out, ULONG total_out = -1);
UINT GetAvailIn();
UINT GetAvailOut();
UINT GetAvailIn();
UINT GetAvailOut();
void ClearFuncs();
void ClearFuncs();
void Init();
void Init2();
int Process(int flush);
void End();
void Init();
void Init2();
int Process(int flush);
void End();
};
namespace NSZip
{
bool KERNEL_DECL Decompress(const BYTE* pSrcBuffer, const ULONG& lSrcBufferLen, BYTE* pDstBuffer, ULONG& lDstBufferLen);
bool KERNEL_DECL Decompress(const BYTE* pSrcBuffer, const ULONG& lSrcBufferLen, BYTE* pDstBuffer, ULONG& lDstBufferLen);
}
#endif

View File

@ -37,229 +37,229 @@
unzFile unzOpenHelp(BUFFER_IO* buffer)
{
zlib_filefunc_def ffunc;
fill_buffer_filefunc(&ffunc, buffer);
return unzOpen2(NULL, &ffunc);
zlib_filefunc_def ffunc;
fill_buffer_filefunc(&ffunc, buffer);
return unzOpen2(NULL, &ffunc);
}
zipFile zipOpenHelp(BUFFER_IO* buffer)
{
zlib_filefunc_def ffunc;
fill_buffer_filefunc(&ffunc, buffer);
return zipOpen2(NULL, APPEND_STATUS_CREATE, NULL, &ffunc);
zlib_filefunc_def ffunc;
fill_buffer_filefunc(&ffunc, buffer);
return zipOpen2(NULL, APPEND_STATUS_CREATE, NULL, &ffunc);
}
// begin from (ZipUtilsCP.cpp)
bool current_file_is_find(unzFile uf, const char* filename)
{
char filename_inzip[256];
int err = UNZ_OK;
char filename_inzip[256];
int err = UNZ_OK;
unz_file_info file_info;
unz_file_info file_info;
err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
return false;
err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
return false;
std::string name(filename_inzip);
//todooo есть ли необходимость свести все к нижнему ???
return strcmp(filename, name.c_str()) == 0;
std::string name(filename_inzip);
//todooo есть ли необходимость свести все к нижнему ???
return strcmp(filename, name.c_str()) == 0;
}
bool get_file(unzFile unzip_file_handle, BYTE* arr, uInt array_size)
{
if (UNZ_OK == unzOpenCurrentFile(unzip_file_handle))
{
int data_read_size = unzReadCurrentFile(unzip_file_handle, arr, array_size);
if (UNZ_OK == unzOpenCurrentFile(unzip_file_handle))
{
int data_read_size = unzReadCurrentFile(unzip_file_handle, arr, array_size);
unzCloseCurrentFile(unzip_file_handle);
return data_read_size == array_size;
}
return false;
unzCloseCurrentFile(unzip_file_handle);
return data_read_size == array_size;
}
return false;
}
static const std::string get_filename_from_unzfile(unzFile unzip_file_handle)
{
char filename_inzip[256];
char filename_inzip[256];
if (UNZ_OK == unzGetCurrentFileInfo(unzip_file_handle, NULL, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0))
return std::string(filename_inzip);
if (UNZ_OK == unzGetCurrentFileInfo(unzip_file_handle, NULL, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0))
return std::string(filename_inzip);
return "";
return "";
}
bool get_file_in_archive(unzFile unzip_file_handle, const char* filePathInZip, BYTE** fileInBytes, ULONG& nFileSize)
{
if (NULL == fileInBytes)
return false;
(*fileInBytes) = NULL;
do
{
if (current_file_is_find(unzip_file_handle, filePathInZip) == true)
{
unz_file_info file_info;
unzGetCurrentFileInfo(unzip_file_handle, &file_info, NULL, 0, NULL, 0, NULL, 0);
if (NULL == fileInBytes)
return false;
(*fileInBytes) = NULL;
do
{
if (current_file_is_find(unzip_file_handle, filePathInZip) == true)
{
unz_file_info file_info;
unzGetCurrentFileInfo(unzip_file_handle, &file_info, NULL, 0, NULL, 0, NULL, 0);
if (nFileSize > 0)
nFileSize = (std::min)(nFileSize, file_info.uncompressed_size);
else
nFileSize = file_info.uncompressed_size;
if (nFileSize > 0)
nFileSize = (std::min)(nFileSize, file_info.uncompressed_size);
else
nFileSize = file_info.uncompressed_size;
(*fileInBytes) = new BYTE[nFileSize];
get_file(unzip_file_handle, (*fileInBytes), nFileSize);
return true;
}
// else just skip the erroneous file
} while (UNZ_OK == unzGoToNextFile(unzip_file_handle));
(*fileInBytes) = new BYTE[nFileSize];
get_file(unzip_file_handle, (*fileInBytes), nFileSize);
return true;
}
// else just skip the erroneous file
} while (UNZ_OK == unzGoToNextFile(unzip_file_handle));
return false;
return false;
}
// end from (ZipUtilsCP.cpp)
// Создает архив в памяти
void CZipBuffer::create()
{
m_zipFile = NULL;
m_sizeZip = 0;
m_zipFile = NULL;
m_sizeZip = 0;
}
// Открывает архив в память, переданные данные необходимо освободить
void CZipBuffer::open(BYTE* buffer, DWORD size)
{
m_zipFile = new BYTE[size];
memcpy(m_zipFile, buffer, size);
m_sizeZip = size;
m_zipFile = new BYTE[size];
memcpy(m_zipFile, buffer, size);
m_sizeZip = size;
// Получаем пути в архиве
BUFFER_IO* buf = new BUFFER_IO;
buf->buffer = m_zipFile;
buf->nSize = m_sizeZip;
unzFile uf = unzOpenHelp(buf);
// Получаем пути в архиве
BUFFER_IO* buf = new BUFFER_IO;
buf->buffer = m_zipFile;
buf->nSize = m_sizeZip;
unzFile uf = unzOpenHelp(buf);
do
{
unz_file_info file_info;
unzGetCurrentFileInfo(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
if (file_info.uncompressed_size != 0)
m_arrFiles.push_back(CFile(get_filename_from_unzfile(uf), NULL, 0));
} while (UNZ_OK == unzGoToNextFile(uf));
unzClose(uf);
RELEASEOBJECT(buf);
do
{
unz_file_info file_info;
unzGetCurrentFileInfo(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
if (file_info.uncompressed_size != 0)
m_arrFiles.push_back(CFile(get_filename_from_unzfile(uf), NULL, 0));
} while (UNZ_OK == unzGoToNextFile(uf));
unzClose(uf);
RELEASEOBJECT(buf);
}
// Закрывает архив и очищает память
void CZipBuffer::close()
{
for (CFile& oFile : m_arrFiles)
RELEASEARRAYOBJECTS(oFile.m_pData);
m_arrFiles.clear();
for (CFile& oFile : m_arrFiles)
RELEASEARRAYOBJECTS(oFile.m_pData);
m_arrFiles.clear();
}
// Перемещает файл в архиве
void CZipBuffer::move(const std::string& sSrc, const std::string& sDst)
{
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sSrc] (const CFile& oFile) { return oFile.m_sPath == sSrc; });
if (it != m_arrFiles.end())
it->m_sPath = sDst;
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sSrc] (const CFile& oFile) { return oFile.m_sPath == sSrc; });
if (it != m_arrFiles.end())
it->m_sPath = sDst;
}
// Возвращает вектор путей в архиве
std::vector<std::string> CZipBuffer::getPaths()
{
std::vector<std::string> oRes;
for (CFile& oFile : m_arrFiles)
oRes.push_back(oFile.m_sPath);
return oRes;
std::vector<std::string> oRes;
for (CFile& oFile : m_arrFiles)
oRes.push_back(oFile.m_sPath);
return oRes;
}
// Сохраняет архив в переданную память, полученные данные необходимо освободить
void CZipBuffer::save(BYTE*& data, DWORD& length)
{
BUFFER_IO* buf = new BUFFER_IO;
buf->bGrow = 1;
buf->nCurrentPos = 0;
zipFile zip_file_handle = zipOpenHelp(buf);
BUFFER_IO* buf = new BUFFER_IO;
buf->bGrow = 1;
buf->nCurrentPos = 0;
zipFile zip_file_handle = zipOpenHelp(buf);
for (CFile& oFile : m_arrFiles)
if (!oFile.m_nLength)
getFile(oFile.m_sPath, oFile.m_pData, oFile.m_nLength);
for (CFile& oFile : m_arrFiles)
if (!oFile.m_nLength)
getFile(oFile.m_sPath, oFile.m_pData, oFile.m_nLength);
for (CFile& oFile : m_arrFiles)
{
if (ZIP_OK != zipOpenNewFileInZip( zip_file_handle, oFile.m_sPath.c_str(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, -1 ) ||
ZIP_OK != zipWriteInFileInZip(zip_file_handle, oFile.m_pData, oFile.m_nLength) ||
ZIP_OK != zipCloseFileInZip(zip_file_handle))
{
zipClose(zip_file_handle, NULL);
data = NULL;
length = 0;
return;
}
}
zipClose(zip_file_handle, NULL);
for (CFile& oFile : m_arrFiles)
{
if (ZIP_OK != zipOpenNewFileInZip( zip_file_handle, oFile.m_sPath.c_str(), NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, -1 ) ||
ZIP_OK != zipWriteInFileInZip(zip_file_handle, oFile.m_pData, oFile.m_nLength) ||
ZIP_OK != zipCloseFileInZip(zip_file_handle))
{
zipClose(zip_file_handle, NULL);
data = NULL;
length = 0;
return;
}
}
zipClose(zip_file_handle, NULL);
data = m_zipFile = buf->buffer;
length = m_sizeZip = buf->nCurrentPos;
RELEASEOBJECT(buf);
data = m_zipFile = buf->buffer;
length = m_sizeZip = buf->nCurrentPos;
RELEASEOBJECT(buf);
}
// По относительно пути в архиве возвращает файл, полученные данные будут освобождены после использования класса
void CZipBuffer::getFile(const std::string& sPath, BYTE*& data, DWORD& length)
{
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
if (it == m_arrFiles.end())
{
data = NULL;
length = 0;
return;
}
if (it->m_nLength)
{
data = it->m_pData;
length = it->m_nLength;
return;
}
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
if (it == m_arrFiles.end())
{
data = NULL;
length = 0;
return;
}
if (it->m_nLength)
{
data = it->m_pData;
length = it->m_nLength;
return;
}
BUFFER_IO* buf = new BUFFER_IO;
buf->buffer = m_zipFile;
buf->nSize = m_sizeZip;
unzFile uf = unzOpenHelp(buf);
BUFFER_IO* buf = new BUFFER_IO;
buf->buffer = m_zipFile;
buf->nSize = m_sizeZip;
unzFile uf = unzOpenHelp(buf);
data = NULL;
length = 0;
get_file_in_archive(uf, sPath.c_str(), &data, length);
unzClose(uf);
data = NULL;
length = 0;
get_file_in_archive(uf, sPath.c_str(), &data, length);
unzClose(uf);
it->m_nLength = length;
it->m_pData = data;
RELEASEOBJECT(buf);
it->m_nLength = length;
it->m_pData = data;
RELEASEOBJECT(buf);
}
// По относительно пути в архиве добавляет файл, переданные данные необходимо освободить
void CZipBuffer::addFile (const std::string& sPath, BYTE* data, DWORD length)
{
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
if (it == m_arrFiles.end())
{
BYTE* copyData = new BYTE[length];
memcpy(copyData, data, length);
m_arrFiles.push_back(CFile(sPath, copyData, length));
}
else
{
if (it->m_pData != data)
{
RELEASEARRAYOBJECTS(it->m_pData);
BYTE* copyData = new BYTE[length];
memcpy(copyData, data, length);
it->m_pData = copyData;
}
it->m_nLength = length;
}
std::vector<CFile>::iterator it =
std::find_if(m_arrFiles.begin(), m_arrFiles.end(), [&sPath] (const CFile& oFile) { return oFile.m_sPath == sPath; });
if (it == m_arrFiles.end())
{
BYTE* copyData = new BYTE[length];
memcpy(copyData, data, length);
m_arrFiles.push_back(CFile(sPath, copyData, length));
}
else
{
if (it->m_pData != data)
{
RELEASEARRAYOBJECTS(it->m_pData);
BYTE* copyData = new BYTE[length];
memcpy(copyData, data, length);
it->m_pData = copyData;
}
it->m_nLength = length;
}
}
// По относительно пути в архиве удаляет файл
bool CZipBuffer::removeFile(const std::string& sPath)
{
for (std::vector<CFile>::iterator i = m_arrFiles.begin(); i != m_arrFiles.end(); i++)
{
if (i->m_sPath == sPath)
{
RELEASEARRAYOBJECTS(i->m_pData);
m_arrFiles.erase(i);
return true;
}
}
return false;
for (std::vector<CFile>::iterator i = m_arrFiles.begin(); i != m_arrFiles.end(); i++)
{
if (i->m_sPath == sPath)
{
RELEASEARRAYOBJECTS(i->m_pData);
m_arrFiles.erase(i);
return true;
}
}
return false;
}