Modification of Archives: Replacing, Renaming, Deleting and Changing Data
Introduction
Replacing
Sample Code
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
CZipAddNewFileInfo info(_T("C:\\Temp\\file.dat"), _T("replacement.dat"));
info.m_uReplaceIndex = 0;
zip.AddNewFile(info);
zip.Close();
Callbacks Called
Renaming
Callback Called
If the sizes of the new and the old filenames differ, then the space inside the
archive needs to be adjusted and this operation notifies about the progress using
the
CZipActionCallback::cbRename callback.
Deleting
You can delete files in an archive by specifying one of:
If you plan to delete more files at once, use the methods that take arrays as arguments
- they are optimized for multiple file deletion.
Sample Code
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
zip.RemoveFile(0);
CZipIndexesArray indexes;
indexes.Add(0);
indexes.Add(1);
zip.RemoveFiles(indexes);
CZipStringArray names;
names.Add(_T("Temp\\file1.dat"));
names.Add(_T("file4.dat"));
zip.RemoveFiles(names);
zip.Close();
Callbacks Called
Changing the Modification Time of Files Inside an Archive
The modification time of the file is written in both local and central headers.
To modify it, you need to update the time stamp in these both locations to keep
the archive consistent. Follow the steps below to do it:
Sample Code
CZipArchive zip;
zip.Open(_T("C:\\Temp\\test.zip"));
int iIndexOfFile = 1;
zip.ReadLocalHeader(iIndexOfFile);
CZipFileHeader* pHeader = zip.GetFileInfo(iIndexOfFile);
pHeader->SetTime(time(0));
zip.OverwriteLocalHeader(iIndexOfFile);
zip.RemoveCentralDirectoryFromArchive();
zip.Close();
The ZipArchive Library provides methods to write and read global comments.