Ensure we set ZipConstants.DefaultCodePage by using a helper method.

We were currently dealing with this terrible global variable in FileSystem/ZipFile.cs previously, but other parts of the code such as DownloadPackageLogic were creating these files too, and may not have executed the static ctors that fixed the encoding yet.
This commit is contained in:
RoosterDragon
2017-06-30 19:13:07 +01:00
committed by Paul Chote
parent 1f436ad0cb
commit 297f4ad9ed
4 changed files with 53 additions and 18 deletions

View File

@@ -13,9 +13,8 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile;
using OpenRA.Primitives;
namespace OpenRA.FileSystem
{
@@ -26,12 +25,7 @@ namespace OpenRA.FileSystem
class ReadOnlyZipFile : IReadOnlyPackage
{
public string Name { get; protected set; }
protected SZipFile pkg;
static ReadOnlyZipFile()
{
ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
}
protected ZipFile pkg;
// Dummy constructor for use with ReadWriteZipFile
protected ReadOnlyZipFile() { }
@@ -39,7 +33,7 @@ namespace OpenRA.FileSystem
public ReadOnlyZipFile(Stream s, string filename)
{
Name = filename;
pkg = new SZipFile(s);
pkg = ZipFileHelper.Create(s);
}
public Stream GetStream(string filename)
@@ -117,7 +111,7 @@ namespace OpenRA.FileSystem
}
pkgStream.Position = 0;
pkg = new SZipFile(pkgStream);
pkg = ZipFileHelper.Create(pkgStream);
Name = filename;
}
@@ -152,11 +146,6 @@ namespace OpenRA.FileSystem
public ReadOnlyZipFile Parent { get; private set; }
readonly string path;
static ZipFolder()
{
ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
}
public ZipFolder(ReadOnlyZipFile parent, string path)
{
if (path.EndsWith("/", StringComparison.Ordinal))

View File

@@ -164,6 +164,7 @@
<Compile Include="Primitives\SegmentStream.cs" />
<Compile Include="Primitives\SpatiallyPartitioned.cs" />
<Compile Include="Primitives\ConcurrentCache.cs" />
<Compile Include="Primitives\ZipFileHelper.cs" />
<Compile Include="SelectableExts.cs" />
<Compile Include="Selection.cs" />
<Compile Include="Server\Connection.cs" />

View File

@@ -0,0 +1,46 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace OpenRA.Primitives
{
public static class ZipFileHelper
{
/// <summary>
/// Creates a <see cref="ZipFile"/> with UTF8 encoding. Avoid using <see cref="ZipFile(Stream)"/> as you
/// cannot be sure of the encoding that will be used.
/// </summary>
public static ZipFile Create(Stream stream)
{
// SharpZipLib uses this global as the encoding to use for all ZipFiles.
// The initial value is the system code page, which causes several problems.
// 1) On some systems, the code page for a certain encoding might not even be installed.
// 2) The code page is different on every system, resulting in unpredictability.
// 3) The code page might not work for decoding some archives.
// We set the default to UTF8 instead which fixes all these problems.
ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
return new ZipFile(stream);
}
/// <summary>
/// Creates a <see cref="ZipFile"/> with UTF8 encoding. Avoid using <see cref="ZipFile(FileStream)"/> as you
/// cannot be sure of the encoding that will be used.
/// </summary>
public static ZipFile Create(FileStream stream)
{
ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
return new ZipFile(stream);
}
}
}