Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit fe52374

Browse files
Using a single ZipHelper instance instead of delegate
1 parent c2274a5 commit fe52374

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@ class GitInstaller : IGitInstaller
1818
private readonly CancellationToken cancellationToken;
1919
private readonly IEnvironment environment;
2020
private readonly ILogging logger;
21-
22-
private delegate void ExtractZipFile(string archive, string outFolder, CancellationToken cancellationToken,
23-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null);
24-
private ExtractZipFile extractCallback;
21+
private readonly IZipHelper zipHelper;
2522

2623
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken)
27-
: this(environment, null, cancellationToken)
24+
: this(environment, ZipHelper.Instance, cancellationToken)
2825
{
2926
}
3027

31-
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken)
28+
public GitInstaller(IEnvironment environment, IZipHelper zipHelper, CancellationToken cancellationToken)
3229
{
3330
Guard.ArgumentNotNull(environment, nameof(environment));
3431

3532
logger = Logging.GetLogger(GetType());
3633
this.cancellationToken = cancellationToken;
3734

3835
this.environment = environment;
39-
this.extractCallback = sharpZipLibHelper != null
40-
? (ExtractZipFile)sharpZipLibHelper.Extract
41-
: ZipHelper.ExtractZipFile;
42-
36+
this.zipHelper = zipHelper;
4337

4438
GitInstallationPath = environment.GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData)
4539
.ToNPath().Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion);
@@ -184,7 +178,7 @@ public Task<bool> SetupGitIfNeeded(NPath tempPath, IProgress<float> zipFileProgr
184178
{
185179
logger.Trace("Extracting \"{0}\" to \"{1}\"", archiveFilePath, unzipPath);
186180

187-
extractCallback(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
181+
zipHelper.Extract(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
188182
estimatedDurationProgress);
189183
}
190184
catch (Exception ex)
@@ -249,7 +243,7 @@ public Task<bool> SetupGitLfsIfNeeded(NPath tempPath, IProgress<float> zipFilePr
249243
{
250244
logger.Trace("Extracting \"{0}\" to \"{1}\"", archiveFilePath, unzipPath);
251245

252-
extractCallback(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
246+
zipHelper.Extract(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
253247
estimatedDurationProgress);
254248
}
255249
catch (Exception ex)

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ class UnzipTask: TaskBase
88
{
99
private readonly string archiveFilePath;
1010
private readonly string extractedPath;
11+
private readonly IZipHelper zipHelper;
1112
private readonly IProgress<float> zipFileProgress;
1213
private readonly IProgress<long> estimatedDurationProgress;
1314

14-
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
15+
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath,
16+
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
17+
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, zipFileProgress, estimatedDurationProgress)
18+
{
19+
20+
}
21+
22+
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath, IZipHelper zipHelper, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
1523
: base(token)
1624
{
1725
this.archiveFilePath = archiveFilePath;
1826
this.extractedPath = extractedPath;
27+
this.zipHelper = zipHelper;
1928
this.zipFileProgress = zipFileProgress;
2029
this.estimatedDurationProgress = estimatedDurationProgress;
2130
}
@@ -32,7 +41,7 @@ private void UnzipArchive()
3241
Logger.Trace("Zip File: {0}", archiveFilePath);
3342
Logger.Trace("Target Path: {0}", extractedPath);
3443

35-
ZipHelper.ExtractZipFile(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
44+
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
3645

3746
Logger.Trace("Completed");
3847
}

src/GitHub.Api/Installer/ZipHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ namespace GitHub.Unity
88
{
99
class ZipHelper : IZipHelper
1010
{
11+
private static IZipHelper instance;
12+
13+
public static IZipHelper Instance
14+
{
15+
get
16+
{
17+
if (instance == null)
18+
{
19+
instance = new ZipHelper();
20+
}
21+
22+
return instance;
23+
}
24+
}
25+
1126
public static bool Copy(Stream source, Stream destination, int chunkSize, long totalSize,
1227
Func<long, long, bool> progress, int progressUpdateRate)
1328
{

0 commit comments

Comments
 (0)