Clean up after failed extraction.

This commit is contained in:
Paul Chote
2010-11-25 16:03:05 +13:00
parent d6e831eb07
commit e62149398b
2 changed files with 17 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using OpenRA.FileFormats;
@@ -67,7 +68,17 @@ namespace OpenRA.Utility
if (!File.Exists(zipFile)) { Console.WriteLine("Error: Could not find {0}", zipFile); return; }
string dest = "mods{0}{1}{0}{2}".F(Path.DirectorySeparatorChar,mod,path);
new ZipInputStream(File.OpenRead(zipFile)).ExtractZip(dest);
List<string> extracted = new List<string>();
try
{
new ZipInputStream(File.OpenRead(zipFile)).ExtractZip(dest, extracted);
}
catch (SharpZipBaseException e)
{
foreach(var f in extracted)
File.Delete(f);
Console.WriteLine("Error: Corrupted archive");
}
}
public static void DownloadUrl(string argValue)

View File

@@ -12,6 +12,7 @@ using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using OpenRA.FileFormats;
using System.Collections.Generic;
namespace OpenRA.Utility
{
@@ -38,21 +39,7 @@ namespace OpenRA.Utility
}
}
public static void ExtractPackagesFromZip(string mod, string dest)
{
string filepath = string.Format("{0}{1}{2}-packages.zip", dest, Path.DirectorySeparatorChar, mod);
string modPackageDir = string.Format("mods{0}{1}{0}packages{0}", Path.DirectorySeparatorChar, mod);
if (!Directory.Exists(modPackageDir))
Directory.CreateDirectory(modPackageDir);
new ZipInputStream(File.OpenRead(filepath)).ExtractZip(modPackageDir);
Console.WriteLine("Done");
}
public static void ExtractZip(this ZipInputStream z, string destPath)
public static void ExtractZip(this ZipInputStream z, string destPath, List<string> extracted)
{
ZipEntry entry;
while ((entry = z.GetNextEntry()) != null)
@@ -62,7 +49,9 @@ namespace OpenRA.Utility
Console.WriteLine("Extracting {0}", entry.Name);
if (!Directory.Exists(Path.Combine(destPath, Path.GetDirectoryName(entry.Name))))
Directory.CreateDirectory(Path.Combine(destPath, Path.GetDirectoryName(entry.Name)));
using (var f = File.Create(destPath + Path.DirectorySeparatorChar + entry.Name))
var path = destPath + Path.DirectorySeparatorChar + entry.Name;
extracted.Add(path);
using (var f = File.Create(path))
{
int bufSize = 2048;
byte[] buf = new byte[bufSize];