Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileHelper
- {
- private static HttpClient _httpClient = new HttpClient()
- {
- Timeout = TimeSpan.FromSeconds(5)
- };
- public FileHelper() { }
- public async Task<bool> Download(string url, string path)
- {
- var policyResult = await Policy
- .Handle<TaskCanceledException>()
- .WaitAndRetryAsync(retryCount: 3, sleepDurationProvider: i => TimeSpan.FromMilliseconds(300))
- .ExecuteAndCaptureAsync(async () =>
- {
- using (var httpResponse = await _httpClient.GetAsync(url).ConfigureAwait(false))
- {
- httpResponse.EnsureSuccessStatusCode();
- return await httpResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
- }
- }).ConfigureAwait(false);
- if (policyResult.Outcome == OutcomeType.Failure || policyResult.Result == null)
- return false;
- try
- {
- File.WriteAllBytes(path, policyResult.Result);
- return true;
- }
- catch (Exception ex)
- {
- // log error
- return false;
- }
- }
- public bool ExtractTarGz(string fileName, string directory)
- {
- try
- {
- using (var inStream = File.OpenRead(fileName))
- using (var gzipStream = new GZipInputStream(inStream))
- using (var tarArchive = TarArchive.CreateInputTarArchive(gzipStream))
- {
- tarArchive.ExtractContents(directory);
- }
- }
- catch (Exception ex)
- {
- // log error
- return false;
- }
- return true;
- }
- public IList<string> GetFiles(string path)
- {
- var allFiles = new List<string>();
- try
- {
- var localJsonFiles = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
- var localTextFiles = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
- allFiles.AddRange(localJsonFiles);
- allFiles.AddRange(localTextFiles);
- }
- catch (UnauthorizedAccessException ex)
- {
- // log error
- }
- return allFiles;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement