Exemplo n.º 1
0
        /// <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
            {
                string ZipLevel = "3";
                if (!String.IsNullOrEmpty(arguments.ZipLevel))
                {
                    ZipLevel = arguments.ZipLevel;
                }
                using (var archive = new SharpZipArchive(archiveStream, arguments.ZipPassword, ZipLevel))
                {
                    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}");
            }
        }
Exemplo n.º 2
0
        /// <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, Logger logger)
        {
            try
            {
                string ZipLevel = "3";
                if (!String.IsNullOrEmpty(arguments.ZipLevel))
                {
                    ZipLevel = arguments.ZipLevel;
                    logger.debug(String.Format("Set zip compression level to {0}", ZipLevel));
                }
                using (var archive = new SharpZipArchive(archiveStream, arguments.ZipPassword, ZipLevel))
                {
                    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)))
                    {
                        logger.warn($"Warning: file or folder '{filePath}' does not exist.");
                    }
                    var fileHandles = OpenFiles(system, filePaths, logger);

                    archive.CollectFilesToArchive(fileHandles);

                    // Save the active log message to the archive file prior to completion
                    // though after the collection of targeted files finishes.
                    ArchiveCurrentLog(logger, archive, system);
                }
            }
            catch (DiskReadException e)
            {
                logger.error($"Failed to read files, this is usually due to lacking admin privileges.\nError:\n{e}");
            }
        }
Exemplo n.º 3
0
        private static MemoryStream CreateZipArchive(IEnumerable <CyLR.archive.File> testData, string password)
        {
            var zipFile = new MemoryStream();
            var archive = new SharpZipArchive(zipFile, password);

            using (archive)
            {
                archive.CollectFilesToArchive(testData);
            }
            return(zipFile);
        }
Exemplo n.º 4
0
Arquivo: Program.cs Projeto: n5de/CyLR
        /// <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)
        {
            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.WriteLine($"Warning: file or folder '{filePath}' does not exist.");
                }
                var fileHandles = OpenFiles(system, filePaths);

                archive.CollectFilesToArchive(fileHandles);
            }
        }
Exemplo n.º 5
0
        /// <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);
            }
        }