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);
}
}
}
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With