Skip to content

Commit 7a341e1

Browse files
authored
Merge pull request #1028 from twpol/feature/use-native-compression
Replace all compression code with System.IO.Compression
2 parents 73afcba + c5f291d commit 7a341e1

File tree

7 files changed

+37
-53
lines changed

7 files changed

+37
-53
lines changed

Source/Launcher/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ static void CheckOR(List<string> missingFiles, string path)
116116
// Required libraries:
117117
"GNU.Gettext.dll",
118118
"GNU.Gettext.WinForms.dll",
119-
"ICSharpCode.SharpZipLib.dll",
120-
"DotNetZip.dll",
121119
@"Native/X86/OpenAL32.dll",
122120
@"Native/X64/OpenAL32.dll",
123121
// Programs:
@@ -142,4 +140,4 @@ static int SafeReadKey(RegistryKey key, string name, int defaultValue)
142140
}
143141
}
144142
}
145-
}
143+
}

Source/Menu/ImportExportSaveForm.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.Diagnostics;
2020
using System.Drawing;
2121
using System.IO;
22-
using System.IO.Packaging; // Needs Project > Add reference > .NET > Component name = WindowsBase
22+
using System.IO.Compression;
2323
using System.Windows.Forms;
2424
using GNU.Gettext;
2525
using GNU.Gettext.WinForms;
@@ -69,8 +69,6 @@ private void bExport_Click(object sender, EventArgs e)
6969
// Create a Zip-compatible file/compressed folder containing:
7070
// all files with the same stem (i.e. *.save, *.png, *.replay, *.txt)
7171

72-
// For Zip, see http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx
73-
7472
// Copy files to new package in folder save_packs
7573
var fullFilePath = Path.Combine(UserSettings.UserDataFolder, Save.File);
7674
var toFile = Path.GetFileNameWithoutExtension(Save.File) + "." + SavePackFileExtension;
@@ -117,56 +115,35 @@ void UpdateFileList(string message)
117115

118116
static void AddFileToZip(string zipFilename, string fileToAdd)
119117
{
120-
using (var zip = Package.Open(zipFilename, FileMode.OpenOrCreate))
118+
using (var zip = ZipFile.Open(zipFilename, ZipArchiveMode.Update))
121119
{
122-
var destFilename = @".\" + Path.GetFileName(fileToAdd);
123-
var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
124-
if (zip.PartExists(uri))
125-
{
126-
zip.DeletePart(uri);
127-
}
128-
var part = zip.CreatePart(uri, "", CompressionOption.Normal);
129-
using (var source = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
130-
{
131-
using (var destination = part.GetStream())
132-
{
133-
CopyStream(source, destination);
134-
}
135-
}
120+
zip.CreateEntryFromFile(fileToAdd, Path.GetFileName(fileToAdd), CompressionLevel.Optimal);
136121
}
137122
}
138123

139-
140124
static void ExtractFilesFromZip(string zipFilename, string path)
141125
{
142-
using (var zip = Package.Open(zipFilename, FileMode.Open, FileAccess.Read))
126+
using (var zip = ZipFile.OpenRead(zipFilename))
143127
{
144-
foreach (var part in zip.GetParts())
128+
foreach (var part in zip.Entries)
145129
{
146-
var fileName = Path.Combine(path, part.Uri.ToString().TrimStart('/').Replace("%20", " "));
147-
try
148-
{
149-
using (var destination = new FileStream(fileName, FileMode.Create))
130+
// Older save packs have an extra root file we don't need
131+
if (part.FullName == "[Content_Types].xml") continue;
132+
133+
// Older save packs have percent-encoded paths
134+
var fileName = Path.GetFullPath(Path.Combine(path, part.FullName.Replace("%20", " ")));
135+
136+
try {
137+
if (Path.GetFileName(fileName).Length > 0)
150138
{
151-
using (var source = part.GetStream())
152-
{
153-
CopyStream(source, destination);
154-
}
139+
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
140+
part.ExtractToFile(fileName, true);
155141
}
156142
}
157143
catch { } // Ignore attempts to copy to a destination file locked by another process as
158144
// ResumeForm locks PNG of selected save.
159145
}
160146
}
161147
}
162-
163-
static void CopyStream(Stream source, Stream target)
164-
{
165-
const int bufferSize = 0x1000;
166-
var buffer = new byte[bufferSize];
167-
var bytesRead = 0;
168-
while ((bytesRead = source.Read(buffer, 0, bufferSize)) > 0)
169-
target.Write(buffer, 0, bytesRead);
170-
}
171148
}
172149
}

Source/Menu/Menu.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
</Reference>
3333
<Reference Include="PresentationFramework" />
3434
<Reference Include="System.DirectoryServices" />
35+
<Reference Include="System.IO.Compression" />
3536
<Reference Include="System.Net.Http" />
36-
<Reference Include="WindowsBase" />
3737
</ItemGroup>
3838
<ItemGroup>
3939
<Compile Update="KeyInputControl.cs">

Source/Orts.Parsers.Msts/Orts.Parsers.Msts.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
<PrivateAssets>all</PrivateAssets>
2020
</PackageReference>
2121
<PackageReference Include="MonoGame.Framework.WindowsDX" Version="3.8.0.1641" />
22-
<PackageReference Include="SharpZipLib" Version="1.4.1" />
2322
</ItemGroup>
2423
</Project>

Source/Orts.Parsers.Msts/SBR.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
1918
using Microsoft.Xna.Framework;
2019
using System;
2120
using System.Collections.Generic;
2221
using System.Diagnostics;
2322
using System.IO;
23+
using System.IO.Compression;
2424
using System.Text;
2525

2626
namespace Orts.Parsers.Msts
@@ -60,7 +60,9 @@ public static SBR Open(string filename)
6060
// SIMISA@@ means uncompressed
6161
if (headerString.StartsWith("SIMISA@F"))
6262
{
63-
fb = new InflaterInputStream(fb);
63+
// Skip over the 2 byte zlib header and onto the DEFLATE stream itself
64+
fb.Read(buffer, 16, 2);
65+
fb = new DeflateStream(fb, CompressionMode.Decompress);
6466
}
6567
else if (headerString.StartsWith("\r\nSIMISA"))
6668
{

Source/Orts.Updater/Orts.Updater.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
<DocumentationFile>..\..\Program\Orts.Updater.xml</DocumentationFile>
1717
</PropertyGroup>
1818
<ItemGroup>
19+
<Reference Include="System.IO.Compression" />
1920
<Reference Include="System.Security" />
2021
</ItemGroup>
2122
<ItemGroup>
2223
<ProjectReference Include="..\Orts.Common\Orts.Common.csproj" />
2324
<ProjectReference Include="..\Orts.Settings\Orts.Settings.csproj" />
2425
</ItemGroup>
2526
<ItemGroup>
26-
<PackageReference Include="DotNetZip" Version="1.16.0" />
2727
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.355802">
2828
<PrivateAssets>all</PrivateAssets>
2929
</PackageReference>

Source/Orts.Updater/UpdateManager.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
using Ionic.Zip;
1918
using Newtonsoft.Json;
2019
using ORTS.Common;
2120
using ORTS.Settings;
@@ -24,6 +23,7 @@
2423
using System.ComponentModel;
2524
using System.Diagnostics;
2625
using System.IO;
26+
using System.IO.Compression;
2727
using System.Linq;
2828
using System.Net;
2929
using System.Runtime.InteropServices;
@@ -413,14 +413,22 @@ void DownloadUpdate(int progressMin, int progressLength)
413413

414414
void ExtractUpdate(int progressMin, int progressLength)
415415
{
416-
using (var zip = ZipFile.Read(FileUpdateStage))
416+
using (var zip = ZipFile.OpenRead(FileUpdateStage))
417417
{
418-
zip.ExtractProgress += (object sender, ExtractProgressEventArgs e) =>
418+
for (var index = 0; index < zip.Entries.Count; index++)
419419
{
420-
if (e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
421-
TriggerApplyProgressChanged(progressMin + progressLength * e.EntriesExtracted / e.EntriesTotal);
422-
};
423-
zip.ExtractAll(PathUpdateStage, ExtractExistingFileAction.OverwriteSilently);
420+
TriggerApplyProgressChanged(progressMin + progressLength * index / zip.Entries.Count);
421+
422+
var absolutePath = Path.GetFullPath(Path.Combine(PathUpdateStage, zip.Entries[index].FullName));
423+
if (!absolutePath.StartsWith(PathUpdateStage))
424+
throw new IOException("Invalid update: attempting to extract outside staging area");
425+
426+
if (Path.GetFileName(absolutePath).Length > 0)
427+
{
428+
Directory.CreateDirectory(Path.GetDirectoryName(absolutePath));
429+
zip.Entries[index].ExtractToFile(absolutePath, true);
430+
}
431+
}
424432
}
425433

426434
File.Delete(FileUpdateStage);

0 commit comments

Comments
 (0)