Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing string in utf8 format to zip file ( Ionic.Zip )

Tags:

c#

asp.net

I have a problem that output string must be utf8-formatted, I am writing currently ansi string to zip file without problems like this:

StreamReader tr = new StreamReader( "myutf8-file.xml");
String myfilecontent = tr.ReadToEnd();
ZipFile zip = new ZipFile());
zip.AddFileFromString("my.xml", "", myfilecontent  );

How to force string (my.xml file content) to be UTF8.

like image 982
Tom Avatar asked Dec 30 '22 15:12

Tom


1 Answers

Don't use the deprecated AddFileFromString method. Use AddEntry(string, string, string, Encoding) instead:

zip.AddEntry("my.xml", "", myfilecontent, Encoding.UTF8);

If you're actually reading a UTF-8 text file to start with though, why not just open the stream and pass that into AddEntry? There's no need to decode from UTF-8 and then re-encode...

like image 108
Jon Skeet Avatar answered Jan 07 '23 16:01

Jon Skeet