Archived Forum Post

Index of archived forum posts


ObjC: Howto add a file to a Zip using another filename?

Oct 08 '13 at 09:17


i have trouble finding out, how to add a file to a zip in ObectiveC / iOS and assigning it another name.

it seems the only way to add a single file is [zipObj AppendOneFileOrDir:sourceFileURL.path saveExtraPath: NO] -- but doesn't allow new names..

any help is very welcome.

Thx, NR.

Accepted Answer

After a successful call to AppendOneFileOrDir, the zip object contains one additional entry, and that entry will be the very last one. The index of the last entry is zip.NumEntries - 1 (because the 1st entry is at index 0).

After calling AppendOneFileOrDir, you may get the CkoZipEntry object by calling zip.GetEntryByIndex(zip.NumEntries-1). Then update the zipEntry.Filename property to the other name.

Remember, the zip.Append methods don't actually write to the zip archive. The zip archive is only written when the WriteZip / WriteZipAndClose methods are called. The Append methods only add entries to the zip object. When files are appended, each entry contains a reference to the full path of the file (or directory) on disk, as well as the path to be used for the entry within the zip when written. Updating the zipEntry.Filename updates the path to be stored within the zip when writing, but does not change the path of the file on disk that is referenced.


for others using this:

if ([zip AppendOneFileOrDir: ... ]) 
  CkoZipEntry *entry = [zip GetEntryByIndex: [NSNumber numberWithInt: [[zip NumEntries] integerValue] -1 ]];
  [entry setFileName: @"new_name.ext"];

  [zip WriteZipAndClose];