Introduction
- You can store custom data along with compressed files. The custom data is saved
in extra fields. Extra fields can be present in two locations: in the local file
header and in the central directory. You can store your custom data in one or both
of the locations.
- You need to choose a unique identifier for your extra data that should not collide
with existing mappings. You can find more information about the format of extra
field and currently known mappings in the Appnote.txt
file from PKWARE. This file is also distributed with the ZipArchive Library. The
ZipArchive Library uses the following identifiers:
- Extra fields collections are accessible through:
- The CZipExtraData class represents the custom data.
- You can create and add a CZipExtraData object to the
local or central field using the CZipExtraField::CreateNew(WORD)
method or its overload.
- You can read and write custom data using the CZipExtraData::m_data
member.
Writing Local Extra Field
You can specify local extra fields only when creating a new file in an archive,
because the local header is written at that time. You need to:
Sample Code
const WORD id = 0xABCD;
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"), CZipArchive::zipCreate);
CZipFileHeader templ;
templ.SetFileName(_T("file.dat"));
CZipExtraData* extra = templ.m_aLocalExtraData.CreateNew(id);
const char* data = "data to save";
size_t dataSize = strlen(data);
extra->m_data.Allocate((DWORD)dataSize);
memcpy(extra->m_data, data, dataSize);
zip.OpenNewFile(templ);
zip.CloseNewFile();
zip.Close();
Writing Central Extra Field
- You can modify central extra fields at any time, but you need to remember that they
are saved when the central directory is saved to an archive.
- The central directory is removed from an archive with any other modification and
saved back when you call CZipArchive::Close() or CZipArchive::Finalize(). To read more about saving the
central directory, see Compressing Data.
- If you are not sure if the central directory is saved, request the information with
the CZipArchive::GetCentralDirInfo() method.
- If the central directory is saved and you don't want to do any other modifications,
you can remove the central directory with the CZipArchive::RemoveCentralDirectoryFromArchive()
method. This won't work for existing segmented archives, because the ZipArchive
Library does not support their modification.
Sample Code
const WORD id = 0xABCD;
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
CZipExtraData* extra = zip[0]->m_aCentralExtraData.CreateNew(id);
const char* data = "data to save";
size_t dataSize = strlen(data);
extra->m_data.Allocate((DWORD)dataSize);
memcpy(extra->m_data, data, dataSize);
zip.RemoveCentralDirectoryFromArchive();
zip.Close();
Reading Extra Fields
Sample Code
const WORD id = 0xABCD;
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
const int index = 0;
zip.OpenFile(index);
zip.CloseFile();
const CZipFileHeader* info = zip[index];
CZipExtraData* extra = info->m_aLocalExtraData.Lookup(id);
if (extra != NULL)
{
}
extra = info->m_aCentralExtraData.Lookup(id);
if (extra != NULL)
{
}
zip.Close();