private static void SetEncryption(ArchOptions archOptions, string value) { var parts = value.Split(':'); if (2 != parts.Length) { throw new ArgumentException(value); } string method = parts[0]; OptionValidators.ValidateContains("method", method, Enum.GetNames(typeof(ZipEncryptionMethod))); string password = parts[1]; if (string.IsNullOrEmpty(password)) { throw new ArgumentException("Invalid password."); } archOptions.EncryptionMethod = (ZipEncryptionMethod)Enum.Parse(typeof(ZipEncryptionMethod), parts[0]); archOptions.Password = password; }
private static void Archive(ArchOptions archOptions) { if (false == File.Exists(archOptions.SourceFilename)) { throw new FileNotFoundException("Archive source was not found", archOptions.SourceFilename); } var localArch = new LocalArch(); var stopwatch = new Stopwatch(); if (archOptions.MeasureTime) { stopwatch.Start(); } ArchiveFile(localArch, archOptions); if (archOptions.MeasureTime) { stopwatch.Stop(); Console.WriteLine(string.Format("Archive took {0} minutes ({1} seconds)", stopwatch.Elapsed.TotalMinutes, stopwatch.Elapsed.TotalSeconds)); } }
private static void SetCustomParameter(ArchOptions archOptions, string value) { var parts = value.Split('='); if (2 != parts.Length) { throw new ArgumentException(value); } archOptions.CustomParameters[parts[0]] = parts[1]; }
private static void SetDecompress(ArchOptions archOptions, string password) { archOptions.Decompress = true; archOptions.Password = password; }
private static ArchOptions ParseArgs(string[] args) { var archOptions = new ArchOptions(); archOptions.SourceFilename = args[0]; OptionValidators.ValidatePath("source filename", archOptions.SourceFilename); Program.ArchOptionDescriptorSet.Apply(archOptions, args.Skip(1)); return archOptions; }
private static void DecompressFile(LocalArch localArch, ArchOptions archOptions, FileMode fileMode) { if ((false == string.IsNullOrEmpty(archOptions.OutputFilename)) && (false == Directory.Exists(archOptions.OutputFilename))) { throw new DirectoryNotFoundException(archOptions.OutputFilename); } localArch.Decompress(archOptions.SourceFilename, archOptions.OutputFilename, fileMode, archOptions.Password, archOptions.BufferSize, archOptions.PreallocationPercent); }
private static void CompressFile(LocalArch localArch, ArchOptions archOptions, FileMode fileMode) { string targetFilename = archOptions.OutputFilename; string extension = archOptions.ArchiveFormat.GetExtension(); if (string.IsNullOrEmpty(targetFilename)) { targetFilename = Path.ChangeExtension(Path.GetFileName(archOptions.SourceFilename), extension); } else if (Directory.Exists(targetFilename)) { targetFilename = Path.Combine(targetFilename, Path.ChangeExtension( Path.GetFileName(archOptions.SourceFilename), extension)); } localArch.Compress(archOptions.SourceFilename, targetFilename, fileMode, archOptions.ArchiveFormat, archOptions.CompressionMethod, archOptions.CompressionLevel, archOptions.EncryptionMethod, archOptions.Password, archOptions.BufferSize, archOptions.PreallocationPercent, archOptions.Check, archOptions.CustomParameters); }
private static void CheckFile(LocalArch localArch, ArchOptions archOptions) { localArch.Check(archOptions.SourceFilename, archOptions.Password, archOptions.BufferSize); }
private static void ArchiveFile(LocalArch localArch, ArchOptions archOptions) { FileMode fileMode = FileMode.Create; if (false == archOptions.Overwrite) { fileMode = FileMode.CreateNew; } if (archOptions.Decompress) { if (archOptions.Check) { CheckFile(localArch, archOptions); } else { DecompressFile(localArch, archOptions, fileMode); } } else { CompressFile(localArch, archOptions, fileMode); } }