After writing to the zip folder, all zip entries change the encoding

We use the ASP.NET kernel and have this code:

public static void Compress(string sourceDirectoryName, string destinationArchiveFileName) { var directoryName = Path.GetFileName(sourceDirectoryName); var remotePath = sourceDirectoryName.Split(new[] { directoryName }, StringSplitOptions.RemoveEmptyEntries).First(); using (var zipStream = new MemoryStream()) { using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true, Encoding.UTF8)) { zip.CreateEntry(string.Concat(directoryName, directorySlash)); foreach (var path in Directory.EnumerateFileSystemEntries(sourceDirectoryName, "*", SearchOption.AllDirectories)) { if (!Directory.Exists(path)) zip.CreateEntryFromFile(path, path.RemoveSubString(remotePath).ReplacePathSeparatorOnSlash()); else zip.CreateEntry(string.Concat(path.RemoveSubString(remotePath).ReplacePathSeparatorOnSlash(), directorySlash)); } } using (var outputZip = new FileStream(destinationArchiveFileName, FileMode.Create)) { zipStream.Seek(0, SeekOrigin.Begin); zipStream.CopyTo(outputZip); } } } 

After zipping sourceDirectoryName containing Russian characters, if we open this archive using Windows Explorer, we will see the following:

enter image description here and where the name marked in green is correct and the name marked in red has changed its encoding of the name.

If we use the following code:

 ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName); 

We have the same problem. How to solve this problem?

+5
source share
1 answer

I have found the answer. I have to use 866 encoding:

 using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true, Encoding.GetEncoding(866))) { ... } 
+2
source

Source: https://habr.com/ru/post/1244283/


All Articles