Missing file when zip

651 Views Asked by At

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?

1

There are 1 best solutions below

0
Marc Gravell On

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);
            }
        }
    }