Domanda

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

È stato utile?

Soluzione 2

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)

Altri suggerimenti

Yeah, this isn't going to work as is as far as I can tell. The comment isn't being written and won't be given the current iteration of the library.

I've not used this library at all, but doing some inspection of the source code:

If we look here in the source code we can see it only runs updates if contentsEdited. Hmm. I wonder what sets that? Seems a likely culprit.

Searching around we find the following suspicious comment attached to some commented out code

/* Modify not yet ready for public consumption.
   Direct modification of an entry should not overwrite original data before its read.

And generally searching around in the source code we find a lot of half finished debris around modification of entries.

So basically I think what you're hitting is a half finished library that doesn't do what you want. Sorry. :-)

Some additional code archaeology reveals no method to me for writing the comment when you first add the file, alas. It might be possible to hack something together by fishing out the ZipEntry object somehow before you commit the update, but how is eluding me.

The code is public, only need changes this... in File ZipFile.cs line (for me 1810)

This sample is for IStaticDataSource ... for other method change other methods :)

/// <summary>
/// Add a file entry with data.
/// </summary>
/// <param name="dataSource">The source of the data for this entry.</param>
/// <param name="entryName">The name to give to the entry.</param>
/// <param name="compressionMethod">The compression method to use.</param>
/// <param name="useUnicodeText">Ensure Unicode used for name/comments for this entry.</param>
/// <param name="comment">Comentario</param>
public void Add(IStaticDataSource dataSource, string entryName,
    CompressionMethod compressionMethod, bool useUnicodeText, string comment)
{
    if (dataSource == null)
    {
        throw new ArgumentNullException("dataSource");
    }

    if (entryName == null)
    {
        throw new ArgumentNullException("entryName");
    }

    CheckUpdating();

    ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
    entry.IsUnicodeText = useUnicodeText;
    entry.CompressionMethod = compressionMethod;
    entry.Comment = comment;

    AddUpdate(new ZipUpdate(dataSource, entry));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top