1

I'm using Ionic.zip to zip a bunch of files. This is my code to add those file into the zip.

ZipFile zip = new ZipFile();
foreach (string filepath in listoffile) //5 files
{                             
     zip.AddFile(filepath, "");
}
zip.Save(mypath + "attachment.zip");

When I check the number of file in the zip it show 5, but when I open the zip file it only have 4 files inside and missing the file that contain chinese character in the filename. I try multiple time and different file, the missing of file only happen when the file contain chinese character in it. Is there anyway that I can do to solve this problem?

Xion
  • 452
  • 2
  • 6
  • 25
  • 1
    "it only have 4 files inside and missing the file that contain chinese character in the filename" - do you have an example of a filename that isn't working? I've tested locally with "a.txt", "b.txt" ... "e.txt" and it worked just fine - so an example that *doesn't* work would be great. – Marc Gravell Jun 23 '20 at 13:22
  • @MarcGravell for example this kind of filename 2020板二-2.png – Xion Jun 23 '20 at 13:26
  • yes, I can repro that filename not working with ionic-zip, thanks – Marc Gravell Jun 23 '20 at 13:43
  • Not all ZIP files are the same. There is different version of the ZIP Specification and not all tools meet all the requirements. There are optional requirements. Most times I've seen issues when new files are added to an existing zip, files are deleted, or files are replaced. – jdweng Jun 23 '20 at 13:50

1 Answers1

2

This looks to be a bug or limitation in , which may be best addressed by logging an issue with wherever you got it from; however, a workaround might be: don't use that - the following works fine with the inbuilt .NET types (on some frameworks you might need to add a package reference to System.IO.Compression). It is a bit more manual, but: it works.

    using (var output = File.Create("attachment.zip"))
    using (var zip = new ZipArchive(output, ZipArchiveMode.Create))
    {
        foreach (var file in listoffile)
        {
            var entry = zip.CreateEntry(file);
            using (var source = File.OpenRead(file))
            using (var destination = entry.Open())
            {
                source.CopyTo(destination);
            }
        }
    }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900