Search code examples
c#dotnetzip

How to read a single file inside a folder inside a zip file with DotNetZip?


After some research, I found DotNetZip very easy to use when it comes to reading files in zip files. Sadly, I did run into a minor issue.

My zip file is set up like this:

  • Top layer: zip file itself.
  • Second layer: folder with the same name as the zip file.
  • Third layer: data (including the file needed).

Using the following code, I keep stumbling upon it not finding an entry

using (ZipFile zip = ZipFile.Read(modPath))
{
     string[] temp1 = modPath.Split('\\');
     string mod = temp1[temp1.Length - 1];

     mod = mod.Remove(mod.Length - 6);

     string modinfo = @mod + "/info.json";

     ZipEntry e = zip[modinfo]; // No entry found here
}

Am I overlooking something here?

Edit: Added the loop to get the name

Inside a zip


Solution

  • You can iterate through the contents of the zip file using foreach and then find your file.

                using (ZipFile zip = ZipFile.Read(modPath))
                {
                    ZipEntry e;
                    foreach (ZipEntry k in zip)
                    {
                        if (k.FileName.Contains("info.json"))
                        {
                            e = k;
                            break;
                        }
                    }
                }