Search code examples
c#unicodeencodingunzip

German letters and encoding in C#


I have an unzipping function, and I am using System.Text.Encoding to make sure that the files that are being extracted keep the same names after extraction because usually the files that I am unzipping contains German letters.
I tried different things like Encoding.Default or Encoding.UTF8 but nothing works äÄéöÖüß.txt gets converted to „Ž‚”™á.txt or in case of default it is black boxes :/

any suggestions?

using (ZipArchive archive = System.IO.Compression.ZipFile.Open(ZipFile, ZipArchiveMode.Read, System.Text.Encoding.Default))
{

    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        string fullPath = Path.Combine(appPath, entry.FullName);
        if (String.IsNullOrEmpty(entry.Name))
        {
            Directory.CreateDirectory(fullPath);
        }
        else
        {
            if (!entry.Name.Equals("Updater.exe"))
            {
                entry.ExtractToFile(fullPath,true);

            }
        }
    }
}

Solution

  • Try CodePage 850 (has worked for me):

    using (ZipArchive archive = System.IO.Compression.ZipFile.Open(ZipFile, ZipArchiveMode.Read,  System.Text.Encoding.GetEncoding(850)))
    {
          // ....
    

    The next comment is from (an ancient version) of Sharpziplib that put me in the right direction:

        /* Using the codepage 1252 doesn't solve the 8bit ASCII problem :/
           any help would be appreciated.
    
          // get encoding for latin characters (like ö, ü, ß or ô)
          static Encoding ecp1252 = Encoding.GetEncoding(1252);
        */
    
        // private static Encoding _encoding = System.Text.ASCIIEncoding;
        private static Encoding _encoding = System.Text.Encoding.GetEncoding(850);
    

    The last line is my change, to made it correctly read zip-files with special characters.