Skip to content

Commit ed17c28

Browse files
committed
fix: Use only standard built-in compression classes
1 parent 42e128a commit ed17c28

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

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">

0 commit comments

Comments
 (0)