private void downloadFromGdrive(ImageDownloadProgress task, GdriveWebClient webClient) { string fileName = task.Dir.Subdirectory + ".7z"; string archiveFileName = task.TargetDirectory.AddPath(fileName); lock (_syncOutput) Console.WriteLine($"Downloading {task.Dir.Subdirectory} from {task.GdriveUrl}"); if (File.Exists(archiveFileName)) { try { File.Delete(archiveFileName); } catch (Exception ex) { Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); return; } } try { webClient.DownloadFromGdrive(task.GdriveUrl, task.TargetDirectory); } catch (Exception ex) { Console.WriteLine($"Error downloading {archiveFileName} from {task.GdriveUrl}: {ex.Message}"); return; } if (!File.Exists(archiveFileName)) { Console.WriteLine($"Failed to download {archiveFileName} from {task.GdriveUrl}"); return; } var sevenZip = new SevenZip(silent: true); sevenZip.Extract(archiveFileName, task.TargetDirectory, Enumerable.Empty <string>()); Interlocked.Add(ref _countInDownloadedDirs, task.FilesOnline.Count - task.FilesDownloaded.Count); ProgressChanged?.Invoke(); try { File.Delete(archiveFileName); } catch (Exception ex) { Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); } }
public static bool DownloadAndExtract(this GdriveWebClient webClient, string gdriveUrl, string targetDirectory, string fileName) { if (!Str.Equals(".7z", Path.GetExtension(fileName))) { throw new ArgumentException(); } string archiveFileName = targetDirectory.AddPath(fileName); if (File.Exists(archiveFileName)) { try { File.Delete(archiveFileName); } catch (Exception ex) { Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); return(false); } } try { webClient.DownloadFromGdrive(gdriveUrl, targetDirectory); } catch (Exception ex) { Console.WriteLine($"Error downloading {archiveFileName} from {gdriveUrl}: {ex.Message}"); return(false); } if (!File.Exists(archiveFileName)) { Console.WriteLine($"Failed to download {archiveFileName} from {gdriveUrl}"); return(false); } var sevenZip = new SevenZip(silent: true); sevenZip.Extract(archiveFileName, targetDirectory, Enumerable.Empty <string>()); try { File.Delete(archiveFileName); } catch (Exception ex) { Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); } return(true); }
public async Task <bool> DownloadAndExtract(string remotePath, FsPath targetDirectory, FsPath fileName, CancellationToken token) { if (!Str.Equals(".7z", fileName.Extension())) { throw new ArgumentException(); } FsPath archiveFileName = targetDirectory.Join(fileName); if (archiveFileName.IsFile()) { try { archiveFileName.DeleteFile(); } catch (Exception ex) { lock (_syncOutput) Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); return(false); } } bool downloaded = await TryDownloadFile(remotePath, archiveFileName, token); if (!downloaded) { return(false); } if (!archiveFileName.IsFile()) { lock (_syncOutput) Console.WriteLine($"Failed to download {archiveFileName} from {remotePath}"); return(false); } var sevenZip = new SevenZip(silent: true); sevenZip.Extract(archiveFileName, targetDirectory, Enumerable.Empty <FsPath>()); try { archiveFileName.DeleteFile(); } catch (Exception ex) { lock (_syncOutput) Console.WriteLine($"Failed to remove {archiveFileName}: {ex.Message}"); } return(true); }