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.
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
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);
mf.SetLength(0);
zip.ExtractFile(0, mf);
zip.Close();
BYTE* b = mf.Detach();
_tprintf((LPCTSTR)b);
free(b);
See Also API Links