/// <summary> /// Creates a zip archive containing all files from provided paths. /// </summary> /// <param name="arguments">Program arguments.</param> /// <param name="archiveStream">The Stream the archive will be written to.</param> /// <param name="paths">Map of driveLetter->path for all files to collect.</param> private static void CreateArchive(Arguments arguments, Stream archiveStream, List <string> paths) { #if DOT_NET_4_0 using (var archive = new SharpZipArchive(archiveStream)) #else using (var archive = new NativeArchive(archiveStream)) #endif { var system = arguments.ForceNative ? (IFileSystem) new NativeFileSystem() : new RawFileSystem(); var filePaths = paths.SelectMany(path => system.GetFilesFromPath(path)).ToList(); foreach (var filePath in filePaths.Where(path => !system.FileExists(path))) { Console.WriteLine($"Warning: file or folder '{filePath}' does not exist."); } var fileHandles = OpenFiles(system, filePaths); archive.CollectFilesToArchive(fileHandles); } }
private static void Main(string[] args) { Arguments arguments; try { arguments = new Arguments(args); } catch (ArgumentException e) { Console.WriteLine(e.Message); return; } catch (Exception e) { Console.WriteLine($"Unknown error while parsing arguments: {e.Message}"); return; } if (arguments.HelpRequested) { Console.WriteLine(arguments.GetHelp(arguments.HelpTopic)); return; } Dictionary <char, List <string> > paths; try { paths = CollectionPaths.GetPaths(arguments); } catch (Exception e) { Console.WriteLine($"Error occured while collecting files:\n{e}"); return; } var stopwatch = new Stopwatch(); stopwatch.Start(); try { using (var archiveStream = arguments.SFTPInMemory ? new MemoryStream() : OpenFileStream($@"{arguments.OutputPath}\{Environment.MachineName}.zip")) { #if DOT_NET_4_0 using (var archive = new SharpZipArchive(archiveStream)) #else using (var archive = new NativeArchive(archiveStream)) #endif { foreach (var drive in paths) { var driveName = drive.Key; var system = FileSystem.GetFileSystem(drive.Key, FileAccess.Read); var files = drive.Value .SelectMany(path => system.GetFilesFromPath(path)) .Select(file => new Tuple <string, DiscFileInfo>($"{driveName}\\{file.FullName}", file)); archive.CollectFilesToArchive(files); } } if (arguments.SFTPCheck) { int port; var server = arguments.SFTPServer.Split(':'); try { port = int.Parse(server[1]); } catch (Exception) { port = 22; } Sftp.Sftp.SendUsingSftp(archiveStream, server[0], port, arguments.UserName, arguments.UserPassword, $@"{arguments.OutputPath}/{Environment.MachineName}.zip"); } } if (arguments.SFTPCheck) { if (File.Exists($@"{arguments.OutputPath}\{Environment.MachineName}.zip")) { File.Delete($@"{arguments.OutputPath}\{Environment.MachineName}.zip"); } } } catch (Exception e) { Console.WriteLine($"Error occured while collecting files:\n{e}"); } stopwatch.Stop(); Console.WriteLine("Extraction complete. {0} elapsed", new TimeSpan(stopwatch.ElapsedTicks).ToString("g")); }