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.)