Introduction
- The ZipArchive Library allows the following operations in memory:
- opening archives for creation and modification,
- compressing memory files,
- extracting files from archive to memory files.
This allows the whole archive processing to be performed entirely in memory.
- The
CZipMemFile object is not destroyed throughout the whole processing.
- Note that it is not an error to set the file pointer in the
CZipMemFile object to a position beyond the end
of the file.
- To avoid frequent memory re-allocations, depending on the situation, consider taking one of the following actions:
- initiate
CZipMemFile with a large enough grow value in the nGrowBy parameter in the constructor
- attach a buffer with the
CZipMemFile::Attach() method.
- set the length of the memory file with the
CZipMemFile::SetLength() method (e.g. during extraction to memory,
you can set it to the
CZipFileHeader::m_uUncomprSize value).
In-Memory Archive Creating and Opening
To create or open an archive in memory, use the
CZipArchive::Open(CZipAbstractFile&) method
and pass
CZipMemFile object as an argument.
Creating
If you are creating a new archive, the memory file will hold the resulting archive.
- To clear the contents of the memory file before creating a new archive, open the archive using the CZipArchive::zipCreate mode.
- To keep the contents of the memory file and append the archive at the end of it, open the archive using the CZipArchive::zipCreateAppend mode.
Sample Code
CZipMemFile mf;
CZipArchive zip;
zip.Open(mf, CZipArchive::zipCreate);
zip.AddNewFile(_T("C:\\Temp\\file.dat"));
zip.Close();
CZipFile f;
if (f.Open(_T("C:\\Temp\\test.zip"),
CZipFile::modeWrite | CZipFile::modeCreate, false))
{
int iLen = (int)mf.GetLength();
BYTE* b = mf.Detach();
f.Write(b, iLen);
f.Close();
free(b);
}
Opening
If you are opening an existing archive, the memory file should already hold an archive.
Sample Code
CZipFile f;
if (f.Open(_T("C:\\Temp\\test.zip"), CZipFile::modeRead, false))
{
int iLen = (int)f.GetLength();
BYTE* b = (BYTE*)malloc((UINT)iLen);
f.Read(b, iLen);
f.Close();
CZipMemFile mf;
mf.Attach(b, iLen);
CZipArchive zip;
zip.Open(mf);
zip.ExtractFile(0, _T("C:\\Temp"));
zip.Close();
}
In-Memory Data Compressing and Extracting
- To compress a memory file, use one of the following methods:
- To extract a file in an archive to a memory file, use the
CZipArchive::ExtractFile(ZIP_INDEX_TYPE, CZipAbstractFile&) method. When extracting to
a memory file, it is good to initialize the memory file size to the size of the extracted data to avoid frequent memory
re-allocations.
Sample Code
LPCTSTR zipFileName = _T("C:\\Temp\\test.zip");
CZipArchive zip;
zip.Open(zipFileName, CZipArchive::zipCreate);
CZipMemFile mf;
LPCTSTR data1 = _T("Test data");
mf.Write(data1, (DWORD)(_tcslen(data1) * sizeof(TCHAR)));
zip.AddNewFile(mf, _T("file1.txt"));
zip.Close();
zip.Open(zipFileName);
CZipMemFile mfOut;
mfOut.SetLength(zip[0]->m_uUncomprSize);
((CZipAbstractFile*)&mfOut)->SeekToBegin();
zip.ExtractFile(0, mfOut);
zip.Close();
BYTE* b = mfOut.Detach();
_tprintf((LPCTSTR)b);
free(b);
See Also API Links