Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After zipping folder all zip entries change encoding

We use ASP.NET core 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 that contains russian symbols if we open this archive with windows explorer, we see the following:

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

If we use the following code:

ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName);

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

like image 652
sribin Avatar asked Oct 30 '22 08:10

sribin


1 Answers

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

using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true, Encoding.GetEncoding(866)))
{
    ...
}
like image 128
sribin Avatar answered Nov 09 '22 05:11

sribin