Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

            }
        }
    }
}
like image 575
eMizo Avatar asked Nov 15 '13 09:11

eMizo


People also ask

What encoding do you use for German characters?

As for what encoding to use, Germans often use ISO/IEC 8859-15, but UTF-8 is increasingly becoming the norm, and can handle any kind of non-ASCII characters at the same time.

How do I get German letters on my keyboard?

In the Control Panel box double-click on the keyboard symbol. At the top of the open "Keyboard Properties" panel, click on the "Language" tab. Click the "Add Language" button and scroll to the German variation you want to use: German (Austrian), German (Swiss), German (Standard), etc.

What is Unicode and ASCII encoding?

Unicode is the universal character encoding used to process, store, and exchange text data in any language, while ASCII is used to represent text such as symbols, letters, numerals, and so on.


2 Answers

I used the following libraries:

using System.IO;
using System.Text;

with Encoding.Latin1 in the following method:

File.ReadAllLinesAsync(filePath, Encoding.Latin1, cancellationToken);

which worked in my case.

like image 188
Alreadytakenindeed Avatar answered Sep 25 '22 01:09

Alreadytakenindeed


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.

like image 28
GvS Avatar answered Sep 25 '22 01:09

GvS