Search code examples
c#csvdotnetzip

How to give an Order to the files included in zip file created with dotnet Zip


I would like to be able to include the file with a given order while creating a zip while using DotNet Zip.

Files don't appear in the sequence they were added to the zip.For now the order appears to be random.

I would like to have the files xyz-Header,xyz-Summary included first and then the rest of the files.

xyz-Header.csv

xyz-Summary.csv

xyz-Male.csv

xyz-Female.csv

xyz and names for other files are programmatically determined but Header and Summary files are always included.

Code Snippet

    private MemoryStream GetZip()
    {
        ZipFile zip = new ZipFile();
        List<string, string> files = getFiles();
        zip.AddEntry("xyz-Header.csv", getHeader(files ));
        zip.AddEntry("xyz-Summary", getSummary(files));

        foreach (var x in files)
        {
            zip.AddEntry("xyz-" + x.Item1 + ".csv", x.Item2);
        }

        MemoryStream memoryStream = new MemoryStream();
        zip.Save(memoryStream);
        memoryStream.position = 0;
        return memoryStream;
    }

I would appreciate any help on this.


Solution

  • There is not reason the files have to be ordered in the zip, you can get the file list and the give it any order you like:

      using (ZipFile zip = ZipFile.Read(NameOfExistingZipFile))
      {
        foreach (ZipEntry e in zip.Order(x => x.FileName))
        {
          // do your styff e.Extract();
        }
      }