Skip to content

Fix wrong file names in zip files when they contain Chinese characters #6317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Files.Launcher/MessageHandlers/FileOperationsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,10 @@ public void TryCancel(string uid)

private void UpdateTaskbarProgress()
{
if (OwnerWindow == null || taskbar == null)
{
return;
}
if (operations.Any())
{
taskbar.SetProgressValue(OwnerWindow.Handle, (ulong)Progress, 100);
Expand Down
5 changes: 5 additions & 0 deletions Files/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,10 @@ public static class OptionalPackages
{
public const string ThemesOptionalPackagesName = "49306atecsolution.ThemesforFiles";
}

public static class Filesystem
{
public const int ExtendedAsciiCodePage = 437;
}
}
}
3 changes: 3 additions & 0 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,9 @@
<PackageReference Include="SQLitePCLRaw.bundle_green">
<Version>2.0.6</Version>
</PackageReference>
<PackageReference Include="Ude.NetStandard">
<Version>1.2.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<AppxManifest Include="..\Files.Package\Package.appxmanifest">
Expand Down
72 changes: 65 additions & 7 deletions Files/Filesystem/StorageItems/ZipStorageFolder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Files.Helpers;
using Files.Extensions;
using Files.Helpers;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Toolkit.Uwp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
Expand All @@ -16,6 +18,60 @@ namespace Files.Filesystem.StorageItems
{
public sealed class ZipStorageFolder : BaseStorageFolder
{
public Encoding ZipEncoding { get; set; } = null;

static ZipStorageFolder()
{
// Register all supported codepages (default is UTF-X only)
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Use extended ascii so you can convert the string back to bytes
ZipStrings.CodePage = Constants.Filesystem.ExtendedAsciiCodePage;
}

public static string DecodeEntryName(ZipEntry entry, Encoding zipEncoding)
{
if (zipEncoding == null || entry.IsUnicodeText)
{
return entry.Name;
}
var decoded = Common.Extensions.IgnoreExceptions(() =>
{
var rawBytes = Encoding.GetEncoding(Constants.Filesystem.ExtendedAsciiCodePage).GetBytes(entry.Name);
return zipEncoding.GetString(rawBytes);
});
return decoded ?? entry.Name;
}

public static Encoding DetectFileEncoding(ZipFile zipFile)
{
long readEntries = 0;
Ude.CharsetDetector cdet = new Ude.CharsetDetector();
foreach (var entry in zipFile.OfType<ZipEntry>())
{
readEntries++;
if (entry.IsUnicodeText)
{
return Encoding.UTF8;
}
var guessedEncoding = Common.Extensions.IgnoreExceptions(() =>
{
var rawBytes = Encoding.GetEncoding(Constants.Filesystem.ExtendedAsciiCodePage).GetBytes(entry.Name);
cdet.Feed(rawBytes, 0, rawBytes.Length);
cdet.DataEnd();
if (cdet.Charset != null && cdet.Confidence >= 0.9 && (readEntries >= Math.Min(zipFile.Count, 50)))
{
return Encoding.GetEncoding(cdet.Charset);
}
return null;
});
if (guessedEncoding != null)
{
return guessedEncoding;
}
}
return Encoding.UTF8;
}

public ZipStorageFolder(string path, string containerPath)
{
Name = System.IO.Path.GetFileName(path.TrimEnd('\\', '/'));
Expand Down Expand Up @@ -108,7 +164,7 @@ public override IAsyncOperation<BaseStorageFolder> CreateFolderAsync(string desi
zipFile.CommitUpdate();

var wnt = new WindowsNameTransform(ContainerPath);
return new ZipStorageFolder(wnt.TransformFile(zipDesiredName), ContainerPath);
return new ZipStorageFolder(wnt.TransformFile(zipDesiredName), ContainerPath) { ZipEncoding = ZipEncoding };
}
});
}
Expand Down Expand Up @@ -141,17 +197,18 @@ public override IAsyncOperation<IStorageItem> GetItemAsync(string name)
using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
{
zipFile.IsStreamOwner = true;
ZipEncoding ??= DetectFileEncoding(zipFile);
var entry = zipFile.GetEntry(name);
if (entry != null)
{
var wnt = new WindowsNameTransform(ContainerPath);
if (entry.IsDirectory)
{
return new ZipStorageFolder(wnt.TransformDirectory(entry.Name), ContainerPath, entry);
return new ZipStorageFolder(wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry) { ZipEncoding = ZipEncoding };
}
else
{
return new ZipStorageFile(wnt.TransformFile(entry.Name), ContainerPath, entry);
return new ZipStorageFile(wnt.TransformFile(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry);
}
}
return null;
Expand Down Expand Up @@ -187,12 +244,13 @@ public override IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync()
using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)))
{
zipFile.IsStreamOwner = true;
ZipEncoding ??= DetectFileEncoding(zipFile);
var wnt = new WindowsNameTransform(ContainerPath, true); // Check with Path.GetFullPath
var items = new List<IStorageItem>();
foreach (var entry in zipFile.OfType<ZipEntry>()) // Returns all items recursively
{
string winPath = System.IO.Path.GetFullPath(entry.IsDirectory ? wnt.TransformDirectory(entry.Name) : wnt.TransformFile(entry.Name));
if (winPath.StartsWith(Path)) // Child of self
string winPath = System.IO.Path.GetFullPath(entry.IsDirectory ? wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)) : wnt.TransformFile(DecodeEntryName(entry, ZipEncoding)));
if (winPath.StartsWith(Path.WithEnding("\\"))) // Child of self
{
var split = winPath.Substring(Path.Length).Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (split.Length > 0)
Expand All @@ -202,7 +260,7 @@ public override IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync()
var itemPath = System.IO.Path.Combine(Path, split[0]);
if (!items.Any(x => x.Path == itemPath))
{
items.Add(new ZipStorageFolder(itemPath, ContainerPath, entry));
items.Add(new ZipStorageFolder(itemPath, ContainerPath, entry) { ZipEncoding = ZipEncoding });
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion Files/Helpers/PathNormalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static string GetPathRoot(string path)
{
var pathAsUri = new Uri(path.Replace("\\", "/"));
rootPath = pathAsUri.GetLeftPart(UriPartial.Authority);
if (pathAsUri.IsFile)
if (pathAsUri.IsFile && !string.IsNullOrEmpty(rootPath))
{
rootPath = new Uri(rootPath).LocalPath;
}
Expand Down
7 changes: 4 additions & 3 deletions Files/Helpers/ZipHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public static async Task ExtractArchive(BaseStorageFile archive, BaseStorageFold
}

var wnt = new WindowsNameTransform(destinationFolder.Path);
var zipEncoding = ZipStorageFolder.DetectFileEncoding(zipFile);

var directories = new List<string>();
try
{
directories.AddRange(directoryEntries.Select((item) => wnt.TransformDirectory(item.Name)));
directories.AddRange(fileEntries.Select((item) => Path.GetDirectoryName(wnt.TransformFile(item.Name))));
directories.AddRange(directoryEntries.Select((entry) => wnt.TransformDirectory(ZipStorageFolder.DecodeEntryName(entry, zipEncoding))));
directories.AddRange(fileEntries.Select((entry) => Path.GetDirectoryName(wnt.TransformFile(ZipStorageFolder.DecodeEntryName(entry, zipEncoding)))));
}
catch (InvalidNameException ex)
{
Expand Down Expand Up @@ -98,7 +99,7 @@ public static async Task ExtractArchive(BaseStorageFile archive, BaseStorageFold
continue; // TODO: support password protected archives
}

string filePath = wnt.TransformFile(entry.Name);
string filePath = wnt.TransformFile(ZipStorageFolder.DecodeEntryName(entry, zipEncoding));

var hFile = NativeFileOperationsHelper.CreateFileForWrite(filePath);
if (hFile.IsInvalid)
Expand Down