/// <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, IEnumerable <string> paths) { #if DOT_NET_4_0 using (var archive = new SharpZipArchive(archiveStream, arguments.ZipPassword)) #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); } }
/// <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, IEnumerable <string> paths) { try { using (var archive = new SharpZipArchive(archiveStream, arguments.ZipPassword)) { 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.Error.WriteLine($"Warning: file or folder '{filePath}' does not exist."); } var fileHandles = OpenFiles(system, filePaths); archive.CollectFilesToArchive(fileHandles); } } catch (DiskReadException e) { Console.Error.WriteLine($"Failed to read files, this is usually due to lacking admin privilages.\nError:\n{e}"); } }
/// <summary>Caches current log messages within the Zip archive</summary> /// <param name="logger">The logging object</param> /// <param name="archive">The destination archive object</param> /// <param name="system">The system object used to prep the log file for export</param> private static void ArchiveCurrentLog(Logger logger, SharpZipArchive archive, IFileSystem system) { // Only run if logging is enabled if (logger.LoggingOptions["output_buffer_enabled"] == "true") { string logfileTempPath = String.Format("CyLR_Collection_Log_{0}.log", DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss")); // Cache log messages to File object using (StreamWriter sw = File.CreateText(logfileTempPath)) { sw.Write(logger.logMessages); } // Add cached file to archive List <string> paths = new List <string> { logfileTempPath }; var fileHandles = OpenFiles(system, paths, logger); archive.CollectFilesToArchive(fileHandles); // Removed cached log file File.Delete(logfileTempPath); } }
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")); }