Beispiel #1
0
 /// <summary>
 /// Returns a string representation of a given cabinet file.
 /// </summary>
 /// <param name="cabPath"></param>
 /// <returns></returns>
 public string ToString(string cabPath)
 {
     if (!File.Exists(cabPath))
     {
         return(string.Empty);
     }
     using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) {
         return(cfCabinet.GetStringFullRepresentation());
     }
 }
Beispiel #2
0
 /// <inheritdoc cref="ICabManager.ListFiles"/>
 public IEnumerable <IFileInCab> ListFiles(string cabPath)
 {
     if (!File.Exists(cabPath))
     {
         return(Enumerable.Empty <IFileInCab>());
     }
     using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) {
         return(cfCabinet.GetFiles()
                .Select(file => new FileInCab {
             CabPath = cabPath,
             RelativePathInCab = file.RelativePathInCab,
             LastWriteTime = file.FileDateTime,
             SizeInBytes = file.UncompressedFileSize,
             FileAttributes = GetFileAttributes(file.FileAttributes)
         } as IFileInCab));
     }
 }
Beispiel #3
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);
        }