A nasty encoding issue

Coding problem is annoying. Yesterday met a, spent a few hours to solve.

DotNetZip use this library automatically packaged folder, the original code is:

                    var dir = new DirectoryInfo(@"c:\foo");
                    using (ZipFile zip = new ZipFile(Encoding.UTF8))
                    {
                        var entry = zip.AddDirectory(dir.FullName, dir.Name);
                        zip.Save(Path.Combine(path, dir.Name + ".zip"));
                    }

Looks normal, it seems to work, successfully packaged, opened with 7-zip, the inside of the Chinese folder is also displayed properly.

However, if you are using 7-zip hand-packaged zip, double click in Explorer, you can see inside the folder. And this with code generation zip, if packed into a Chinese folder, double-click it blank. It seems not a problem, but since I packed all pdg file (a file format e-books, do not understand a search glance), originally zip suffix directly into uvz, you can use a software called the UnicornViewer reading, and this code generation with zip, later renamed but with UnicornViewer open, suggesting open failure.

English folders can be, not Chinese folder, it seems likely to be "coding problems."

View the zip attribute with the following code:

                the using (ZIP = ZipFile.Read the ZipFile ( "foo.zip"))) 
                { 
                    the foreach (entry in the ZipEntry ZIP) 
                    { 
                        IF (entry.IsDirectory) 
                        {// add breakpoint here, to view the properties of entry 
                            BREAK; 
                        } 
                    } 
                }

It was found packed with 7-zip file, entry of AlternateEncoding displayed as IBM437, FileName property is garbled, such as "the world's first six-time winner Zhao Guorong real album," appears as "╩└╜τ╡┌╥╗╬ ╗┴ ∙ ╣┌═⌡╒╘╣ · ╚┘╩╡╒╜╫¿╝¡ ".

Check a lot of information, debugging for a long time, finally I understand, it was a IBM437 to decode the result string gb2312 / gbk encoding. Understand this point, modify the code:

                    var dir = new DirectoryInfo(@"c:\foo");
                    byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(dir.Name.ToCharArray());
                    string newName = Encoding.GetEncoding("IBM437").GetString(bytes);
                    using (ZipFile zip = new ZipFile(Encoding.UTF8))
                    {
                        zip.AlternateEncoding = Encoding.GetEncoding("IBM437");
                        var entry = zip.AddDirectory(dir.FullName, newName);
                        zip.Save(Path.Combine(path, dir.Name + ".zip"));
                    }

Test.

Coding problems previously encountered, mostly garbled characters turn into the normal display, this time deliberately have to generate a "garbled" in order to work properly, huh, huh. Who cares, anyway, it just works.

Guess you like

Origin www.cnblogs.com/badnumber/p/12128273.html