protected override void ExecuteCore() { SevenZipCompressor compressor = null; try { var files = this.GetAllSources().ToList(); foreach (var file in files) { FileHelper.WaitForReady(FileHelper.GetDataFilePath(file)); } long nextLength = 0, nextFile = 0; FileLength = files.Sum(file => new FileInfo(FileHelper.GetFilePath(file)).Length); compressor = new SevenZipCompressor { CompressionLevel = (CompressionLevel)Enum.Parse(typeof(CompressionLevel), TaskXml.GetAttributeValue("compressionLevel"), true) }; switch (Path.GetExtension(RelativePath).ToLowerInvariant()) { case ".7z": compressor.ArchiveFormat = OutArchiveFormat.SevenZip; break; case ".zip": compressor.ArchiveFormat = OutArchiveFormat.Zip; break; case ".tar": compressor.ArchiveFormat = OutArchiveFormat.Tar; break; } var filesStart = Path.GetFullPath(FileHelper.GetFilePath(string.Empty)).Length + 1; compressor.FileCompressionStarted += (sender, e) => { ProcessedSourceCount += nextFile; ProcessedFileLength += nextLength; nextFile = 1; nextLength = new FileInfo(e.FileName).Length; CurrentSource = e.FileName.Substring(filesStart); Save(); }; compressor.CompressFiles(FileHelper.GetFilePath(RelativePath), Path.GetFullPath(FileHelper.GetFilePath(BaseFolder)).Length + 1, files.Select(file => Path.GetFullPath(FileHelper.GetFilePath(file))).ToArray()); ProcessedSourceCount += nextFile; ProcessedFileLength += nextLength; Finish(); } catch (SevenZipException) { if (compressor == null) { throw; } throw new AggregateException(compressor.Exceptions); } }
public CompressTask(string archiveFilePath, IEnumerable <string> files, string baseFolder = null, string compressionLevel = null) : base(files, archiveFilePath, TaskType.CompressTask) { TaskXml.SetAttributeValue("compressionLevel", compressionLevel ?? "Ultra"); BaseFolder = baseFolder ?? string.Empty; }