5

I am using DotNetZip. When i am archiving file which have english name all normally. but when i archiving file with russian names in result archive with bad names of file. Some peoplese said that string

ZipConstants.DefaultCodePage = 866;

But it not compile. I also use zip.UseUnicodeAsNecessary properties, and convert my file names to utf8 and utf7.

Xaver
  • 991
  • 2
  • 19
  • 37
  • sorry I'm a little confused between this two libraries: http://www.icsharpcode.net/OpenSource/SharpZipLib/ http://dotnetzip.codeplex.com/releases/view/27890 in first alll really works like i am want. but i very interested how do this on second. because second library have the good clases what can: add files, folders in archives. – Xaver Apr 02 '10 at 08:55
  • Don't convert file names to utf8 yourself, leave it up to the library to do it for you. – Hans Passant Apr 02 '10 at 12:46
  • The constant you referred to does not exist in DotNetZip. Also, there's no need to "convert" your filenames from utf-8 to utf-7, or anything else. DotNetZip will do the string encoding for you, in the code page you select. see http://cheeso.members.winisp.net/DotNetZipHelp/html/aecbb638-1ecf-807e-b933-5bb1a2a95e81.htm – Cheeso Apr 06 '10 at 21:06
  • Try using https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx in .net 4.5 – juFo Nov 15 '16 at 09:18

3 Answers3

21
zip.AlternateEncodingUsage = ZipOption.Always;
zip.AlternateEncoding = Encoding.UTF8;
prime23
  • 3,362
  • 2
  • 36
  • 52
  • Windows Explorer does not support UTF-8 encoded zip files. To read the zip you create, you will have to use a tool or library that supports unicode, such as WinRar, DotNetZip, etc. – Lin Song Yang May 19 '15 at 01:22
  • I have successfully opened UTF-8 zip file on my Windows 8.1 machine. – Funbit Jun 15 '15 at 05:02
9

To create a unicode zip file in DotNetZip:

using (var zip = new ZipFile())
{
   zip.UseUnicodeAsNecessary= true;
   zip.AddFile(filename, "directory\\in\\archive");
   zip.Save("archive.zip");
}

If you want a particular, specific code page, then you must use something else:

using (var zip = new ZipFile())
{
   zip.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding(866);
   zip.AddFile(filename, "directory\\in\\archive");
   zip.Save("archive.zip");
}

Check the documentation for those properties before using them!

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 5
    UseUnicodeAsNecessary is obsolete, and use below code is recommended: zip.AlternateEncodingUsage = ZipOption.Always; zip.AlternateEncoding = Encoding.UTF8; – David Oct 31 '12 at 03:50
2

try this

zip.AddEntry("yourfile.txt", "yourtext", Encoding.GetEncoding("utf-8"));

encoding type : encoding type info

Moory Pc
  • 860
  • 15
  • 16