Modification of Archives: Replacing, Renaming, Deleting and Changing Data
Applies To: All But Multi-Segment Archives

Introduction

Replacing

Sample Code
    CZipArchive zip;
    zip.Open(_T("C:\\Temp\\test.zip"));
    CZipAddNewFileInfo info(_T("C:\\Temp\\file.dat"), _T("replacement.dat"));
    // replace the first file in the archive
    info.m_uReplaceIndex = 0;
    zip.AddNewFile(info);
    zip.Close();

Callbacks Called

Deleting

You can delete files in an archive by specifying either 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.

If you don't want to physically remove the file from the archive, but only from the central directory (e.g. to save the processing time or for safer archive modifications), use the CZipArchive::RemoveFile method and set the bRemoveData parameter to false.

Sample Code
    CZipArchive zip;
    zip.Open(_T("C:\\Temp\\test.zip"));
    // delete the first file
    zip.RemoveFile(0);
    // delete two first files
    CZipIndexesArray indexes;
    indexes.Add(0);
    indexes.Add(1);
    zip.RemoveFiles(indexes);
    // delete files by their names
    CZipStringArray names;
    names.Add(_T("Temp\\file1.dat"));
    names.Add(_T("file4.dat"));
    zip.RemoveFiles(names);
    zip.Close();

Callbacks Called

Committing Modification Changes

Commit Mode

Callback Called

If data sizes of modified files differ after modifications, then the space inside the archive needs to be adjusted and the CZipArchive::CommitChanges() method notifies about the progress using the CZipActionCallback::cbModify callback.
Sample Code
    CZipArchive zip;
    zip.Open(_T("C:\\Temp\\test.zip"));
    zip.SetCommitMode(CZipArchive::cmManual);
    // rename the first file in the archive
    zip[0]->SetFileName(_T("renamed.dat"));    
    zip[2]->SetFileName(_T("renamed2.dat"));
    zip.CommitChanges();
    // perform possibly some other operations
    zip.Close();

Renaming

Sample Code
    CZipArchive zip;
    zip.Open(_T("C:\\Temp\\test.zip"));
    zip.SetCommitMode(CZipArchive::cmManual);
    // rename the first file in the archive
    zip[0]->SetFileName(_T("renamed.dat"));    
    zip[2]->SetFileName(_T("renamed2.dat"));
    // this will call CommitChanges
    zip.Close();

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;
    // read the local header information
    zip.ReadLocalHeader(iIndexOfFile);
    CZipFileHeader* pHeader = zip.GetFileInfo(iIndexOfFile);
    // set the time
    pHeader->SetTime(time(0));
    // write the local header information
    zip.OverwriteLocalHeader(iIndexOfFile);
    zip.RemoveCentralDirectoryFromArchive();
    // the central directory will be written back to the archive
    // during closing
    zip.Close();

Writing and Reading File and Archive Comments

The ZipArchive Library provides the following methods to write and read comments.

Controlling Windows System File Cache

When performing extensive file operations (especially on large files under a 64-bit system) you may notice that large amount of memory is being used. This is may be related to the growing size of Windows System File Cache. Resolving of the issue is beyond the scope of the ZipArchive Library, but here are possible solutions: Some more information can be also found for example in these articles:
Article ID: 0610231944
Copyright © 2000 - 2009 Artpol Software - Tadeusz Dracz