Manipulating entry order in zip file

Jul 2, 2010 at 5:35 AM

I'm writing an ePub creation tool, and using DotNetZip to make the zip container.

The special file mimetype must be first in the container, according to Epub guide (http://www.hxa.name/articles/content/epub-guide_hxa7241_2007.html#containerstructure).

ZipFile.Save() method places entries in the container sorted or unsorted (according SortEntriesBeforeSaving property).

How I can place the mimetype file first?

Jul 2, 2010 at 6:52 AM

I found some workaround

zipFile.AddDirectory(directory);

const string fakeName = "         !";
const string mimetypeName = "mimetype";

var mimetype = zipFile.Entries.FirstOrDefault(e => e.FileName == mimetypeName);
if(mimetype != null)
{
    zipFile.RemoveEntry(mimetype);

    byte[] source = File.ReadAllBytes(Path.Combine(directory, mimetypeName));
    mimetype = zipFile.AddEntry(fakeName, source);
}

zipFile.SaveProgress +=
        (sender, args) =>
        {
            if (args.CurrentEntry == mimetype && args.CurrentEntry.FileName != mimetypeName)
                args.CurrentEntry.FileName = mimetypeName;
        };