Beispiel #1
0
 private void OnProgressionEvent(object sender, CfSaveEventArgs e)
 {
     if (sender is CfCabinet cabinet)
     {
         OnProgress?.Invoke(this, CabProgressionEventArgs.NewProgress(cabinet.CabPath, e.RelativePathInCab, Math.Round(e.TotalBytesDone / (double)e.TotalBytesToProcess * 100, 2)));
     }
 }
Beispiel #2
0
        private int DoAction(IEnumerable <IFileCabBase> filesIn, Action action)
        {
            var files = filesIn.ToList();

            files.ForEach(f => f.Processed = false);

            int nbFilesProcessed = 0;

            foreach (var groupedFiles in files.GroupBy(f => f.CabPath))
            {
                if (action != Action.Archive && !File.Exists(groupedFiles.Key))
                {
                    continue;
                }
                try {
                    if (action == Action.Extract)
                    {
                        // create all necessary extraction folders
                        foreach (var extractDirGroupedFiles in groupedFiles.GroupBy(f => Path.GetDirectoryName(((IFileInCabToExtract)f).ExtractionPath)))
                        {
                            if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key))
                            {
                                Directory.CreateDirectory(extractDirGroupedFiles.Key);
                            }
                        }
                    }
                    using (var cfCabinet = new CfCabinet(groupedFiles.Key, _cancelToken)) {
                        cfCabinet.OnProgress += OnProgressionEvent;
                        try {
                            foreach (var file in groupedFiles)
                            {
                                var fileRelativePath = file.RelativePathInCab.NormalizeRelativePath();
                                switch (action)
                                {
                                case Action.Archive:
                                    var fileToArchive = (IFileToAddInCab)file;
                                    if (File.Exists(fileToArchive.SourcePath))
                                    {
                                        cfCabinet.AddExternalFile(fileToArchive.SourcePath, fileRelativePath);
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                        OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
                                    }
                                    break;

                                case Action.Extract:
                                    if (cfCabinet.ExtractToFile(fileRelativePath, ((IFileInCabToExtract)file).ExtractionPath))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                        OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
                                    }
                                    break;

                                case Action.Delete:
                                    if (cfCabinet.DeleteFile(fileRelativePath))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                        OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
                                    }
                                    break;

                                case Action.Move:
                                    if (cfCabinet.MoveFile(fileRelativePath, ((IFileInCabToMove)file).NewRelativePathInCab.NormalizeRelativePath()))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                        OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
                                    }
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException(nameof(action), action, null);
                                }
                            }
                            if (action != Action.Extract)
                            {
                                cfCabinet.Save(_compressionType);
                            }
                        } finally {
                            cfCabinet.OnProgress -= OnProgressionEvent;
                        }
                    }
                } catch (OperationCanceledException) {
                    throw;
                } catch (Exception e) {
                    throw new CabException($"Failed to {action} files in {groupedFiles.Key}.", e);
                }
                OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(groupedFiles.Key));
            }
            return(nbFilesProcessed);
        }