示例#1
0
        /// <summary>
        /// Adds a directory to the archive, compressing it in the process.
        /// </summary>
        /// <param name="dir">The directory to compress.</param>
        /// <param name="workingdir">The working directory to set.</param>
        /// <param name="level">The amount of compression to use.</param>

        public void CompressDirectory(string[] folders, string workingdir, CompressionLevel level = CompressionLevel.Normal)
        {
            ArgumentBuilder argumentBuilder = GetCompressionArguments(level);

            // Adding password switch only if password has been given in constructor
            if (!string.IsNullOrEmpty(password))
            {
                argumentBuilder.AddSwitch(new SwitchPassword(password));

                if (encryptHeaders)
                {
                    argumentBuilder.Add(new SwitchEncryptHeaders());
                }
            }

            argumentBuilder.AddTargets(folders);

            WProcess p = new WProcess(argumentBuilder);

            if (workingdir != null)
            {
                p.BaseProcess.StartInfo.WorkingDirectory = workingdir;
            }

            p.ProgressUpdated += ProgressUpdated;

            p.Execute();
        }
示例#2
0
        /// <summary>
        /// Returns a list of files inside the archive.
        /// </summary>
        /// <returns>The list of files inside the archive.</returns>
        protected ReadOnlyCollection <ArchiveFile> Index()
        {
            List <ArchiveFile> files = new List <ArchiveFile>();

            WProcess p = new WProcess(new ArgumentBuilder()
                                      .AddCommand(SevenZipCommands.List, archive)
                                      );

            string[] output = p.Execute();

            int dash = 0; //so it collects data only inbetween the lines made of dashes

            foreach (string line in output)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                else if (line.StartsWith("---"))
                {
                    dash++;
                }
                else if (indexReg.IsMatch(line) &&
                         dash == 1)
                {
                    ArchiveFile file = ArchiveFile.Parse(line);
                    files.Add(file);
                }
            }

            return(files.AsReadOnly());
        }
示例#3
0
        /// <summary>
        /// Extracts everything in the archive to the specified destination.
        /// </summary>
        /// <param name="destination">The directory to extract archive contents to.</param>
        /// <param name="overwrite">Whether or not to overwrite existing files. Default false.</param>
        /// <param name="keepStructure">Whether or not to retain folder structure in the archive. Default true.</param>
        public void ExtractAll(string destination, bool overwrite = false, bool keepStructure = true)
        {
            ArgumentBuilder argumentBuilder = GetExtractArguments(destination, overwrite, keepStructure);

            if (!string.IsNullOrEmpty(password))
            {
                argumentBuilder.AddSwitch(new SwitchPassword(password));
            }

            WProcess p = new WProcess(argumentBuilder);

            p.ProgressUpdated += ProgressUpdated;

            p.Execute();
        }
示例#4
0
        /// <summary>
        /// Tests the archive to see if it is valid.
        /// </summary>
        /// <returns>If the archive is valid.</returns>
        public bool TestArchive()
        {
            ArgumentBuilder argumentBuilder = new ArgumentBuilder()
                                              .AddCommand(SevenZipCommands.Test, archive);

            if (!string.IsNullOrEmpty(password))
            {
                argumentBuilder.AddSwitch(new SwitchPassword(password));
            }

            WProcess p = new WProcess(argumentBuilder);

            p.ProgressUpdated += ProgressUpdated;

            string aggregate = string.Join(" ", p.Execute());

            return(aggregate.Contains("Everything is Ok")); //a bit hacky but it's fine
        }