Skip to content

Instantly share code, notes, and snippets.

@SuperJMN
Last active February 13, 2019 20:04
Show Gist options
  • Save SuperJMN/ce6da379323447b6e75ede284ac0f870 to your computer and use it in GitHub Desktop.
Save SuperJMN/ce6da379323447b6e75ede284ac0f870 to your computer and use it in GitHub Desktop.
UefiDownload.cs
public class UefiDownload : IDeploymentTask
{
private readonly IGitHubDownloader downloader;
private readonly IFileSystemOperations operations;
public UefiDownload(IGitHubDownloader downloader, IFileSystemOperations operations)
{
this.downloader = downloader;
this.operations = operations;
}
public async Task Execute()
{
using (var stream = await downloader.OpenZipStream("https://github.com/andreiw/RaspberryPiPkg"))
{
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
var mostRecentFolderEntry = GetMostRecentDirEntry(zipArchive);
var contents = zipArchive.Entries.Where(x => x.FullName.StartsWith(mostRecentFolderEntry.FullName) && !x.FullName.EndsWith("/"));
await ExtractContents(@"Downloaded\UEFI", mostRecentFolderEntry, contents);
}
}
private async Task ExtractContents(string destination, ZipArchiveEntry baseEntry,
IEnumerable<ZipArchiveEntry> entries)
{
foreach (var entry in entries)
{
var filePath = entry.FullName.Substring(baseEntry.FullName.Length);
var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
var dir = Path.GetDirectoryName(destFile);
if (!operations.DirectoryExists(dir))
{
operations.CreateDirectory(dir);
}
using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
using (var stream = entry.Open())
{
await stream.CopyToAsync(destStream);
}
}
}
private ZipArchiveEntry GetMostRecentDirEntry(ZipArchive p)
{
var dirs = from e in p.Entries
where e.FullName.EndsWith("/")
select e;
var splitted = from e in dirs
select new
{
e,
Parts = e.FullName.Split('/'),
};
var parsed = from r in splitted
select new
{
r.e,
Date = FirstParseableOrNull(r.Parts),
};
return parsed.OrderByDescending(x => x.Date).First().e;
}
private DateTime? FirstParseableOrNull(string[] parts)
{
foreach (var part in parts)
{
var candidate = part.Split('-');
if (candidate.Length > 1)
{
var datePart = candidate[0];
if (DateTime.TryParseExact(datePart, "yyyyMMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
return date;
}
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment