Search code examples
c#zipsharpziplib

Why is my SharpZipLib ZipEntry.Comment not getting set (or read)?


I've been working on a tool that uses SharpZipLib to add files to a zipfile, with a comment against the ZipEntry to store a piece of metadata I need. (I know there are other ways I could handle these metadata, but I'd like to avoid rearchitecting my solution if I can avoid it.)

The [slightly simplified] code used to write the file and metadata to the zipfile reads:

public static void AddFileToZip(string path, Guid metadata)
{
    using (ZipFile zipFile = new ZipFile(__zipName))
    {
        zipFile.BeginUpdate();
        zipFile.Add(path);
        zipFile.CommitUpdate();
        zipFile.Close();
    }
    // Close and reopen the ZipFile so it can find the ZipEntry:
    using (ZipFile zipFile = new ZipFile(__zipName))
    {
        string cleanPath = ZipEntry.CleanName(path);
        zipFile.BeginUpdate();
        zipFile.GetEntry(cleanPath).Comment = metadata.ToString("N");
        zipFile.CommitUpdate();
        zipFile.Close();
    }
}

The test harness for this, then reads:

[Test]
public void ArchiveCreationTests()
{
    // Hard-code some variables
    string testFile = @"C:\Users\owen.blacker\Pictures\Ddraig arian.png";
    Guid guid = Guid.NewGuid();

    MyClassName.AddFileToZip(testFile, guid);
    Assert.IsTrue(File.Exists(__zipName), "File does not exist: " + __zipName);

    string cleanName = ZipEntry.CleanName(testFile);
    ZipFile zipfile = new ZipFile(__zipName);
    Assert.GreaterOrEqual(
        zipfile.FindEntry(cleanName, true),
        0,
        "Cannot file ZipEntry " + cleanName);

    ZipEntry zipEntry = zipfile.GetEntry(cleanName);
    StringAssert.AreEqualIgnoringCase(
        guid.ToString("N"),
        zipEntry.Comment,
        "Cannot validate GUID comment.");
}

Now my zipfile is being created — and it does indeed contain my test image Ddraig arian.png —, the ZipEntry is successfully being found, but the StringAssert call is always failing. I'm not entirely sure whether it's failing because it's not being written or if it's failing because it's not being read.

Now I know that you have to use ZipFile/ZipEntry to access the ZipEntry.Comment, as ZipInputStream doesn't let you get to the Comment, but I am using ZipFile and ZipEntry, so I can't see why it wouldn't work.

Does anyone have any ideas?

(The slightly odd close-and-reopen in AddFileToZip is because the ZipFile.GetEntry call was always failing, presumably because the ZipEntry hadn't yet been written to the file index. And yes, my test file is indeed a silver dragon.)


Solution

  • Would you be able to use DotNetZip? I find it easier to use much of the time, I was able to get ZipEntry comments working, see below.

    Using DotNetZip:

    using (ZipFile zip = new ZipFile(__zipName))
    {
        string testFile = @"...";
        ZipEntry newEntry = zip.AddFile(testFile);
        newEntry.Comment = "test";
        zip.Save();
    }
    using (ZipFile zip = new ZipFile(__zipName))
    {
        Console.WriteLine(zip[0].Comment);
    }
    

    It appears that SharpZipLib doesn't fully support ZipEntry.Comment, see @DRMacIver answer for good research there, I've also tried multiple ways and cannot figure it out (I am able to set the comment and save, but when I read it again it is null).

    I don't know why it doesn't work, but I would guess that it might be because standard zifiles does not support comments for Files, only one comment for the whole zip file. So I'm thinking they might be extending zip to support that, maybe they never finished it or never tested it.

    Unrelated but I'll mention that I've done tests previously and SharpZipLib was able to achieve slightly better compression, but the ease of use of DotNetZip still made it a better solution for me.

    I have not tried using SharpZipLib to read a zip file created with DotNetZip with working comments to see if it might be a problem with reading it or writing it (I am curious about this)