Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:29:15+00:00 2026-06-15T19:29:15+00:00

I’ve been working on a tool that uses SharpZipLib to add files to a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:29:16+00:00Added an answer on June 15, 2026 at 7:29 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.