|
19 | 19 | using System.Diagnostics;
|
20 | 20 | using System.Drawing;
|
21 | 21 | using System.IO;
|
22 |
| -using System.IO.Packaging; // Needs Project > Add reference > .NET > Component name = WindowsBase |
| 22 | +using System.IO.Compression; |
23 | 23 | using System.Windows.Forms;
|
24 | 24 | using GNU.Gettext;
|
25 | 25 | using GNU.Gettext.WinForms;
|
@@ -69,8 +69,6 @@ private void bExport_Click(object sender, EventArgs e)
|
69 | 69 | // Create a Zip-compatible file/compressed folder containing:
|
70 | 70 | // all files with the same stem (i.e. *.save, *.png, *.replay, *.txt)
|
71 | 71 |
|
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 |
| - |
74 | 72 | // Copy files to new package in folder save_packs
|
75 | 73 | var fullFilePath = Path.Combine(UserSettings.UserDataFolder, Save.File);
|
76 | 74 | var toFile = Path.GetFileNameWithoutExtension(Save.File) + "." + SavePackFileExtension;
|
@@ -117,56 +115,35 @@ void UpdateFileList(string message)
|
117 | 115 |
|
118 | 116 | static void AddFileToZip(string zipFilename, string fileToAdd)
|
119 | 117 | {
|
120 |
| - using (var zip = Package.Open(zipFilename, FileMode.OpenOrCreate)) |
| 118 | + using (var zip = ZipFile.Open(zipFilename, ZipArchiveMode.Update)) |
121 | 119 | {
|
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); |
136 | 121 | }
|
137 | 122 | }
|
138 | 123 |
|
139 |
| - |
140 | 124 | static void ExtractFilesFromZip(string zipFilename, string path)
|
141 | 125 | {
|
142 |
| - using (var zip = Package.Open(zipFilename, FileMode.Open, FileAccess.Read)) |
| 126 | + using (var zip = ZipFile.OpenRead(zipFilename)) |
143 | 127 | {
|
144 |
| - foreach (var part in zip.GetParts()) |
| 128 | + foreach (var part in zip.Entries) |
145 | 129 | {
|
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) |
150 | 138 | {
|
151 |
| - using (var source = part.GetStream()) |
152 |
| - { |
153 |
| - CopyStream(source, destination); |
154 |
| - } |
| 139 | + Directory.CreateDirectory(Path.GetDirectoryName(fileName)); |
| 140 | + part.ExtractToFile(fileName, true); |
155 | 141 | }
|
156 | 142 | }
|
157 | 143 | catch { } // Ignore attempts to copy to a destination file locked by another process as
|
158 | 144 | // ResumeForm locks PNG of selected save.
|
159 | 145 | }
|
160 | 146 | }
|
161 | 147 | }
|
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 |
| - } |
171 | 148 | }
|
172 | 149 | }
|
0 commit comments