/// <inheritdoc />
        public int CheckFileSet(IEnumerable <IFileInArchiveToCheck> filesToCheck)
        {
            int total = 0;

            foreach (var groupedFiles in filesToCheck.ToNonNullEnumerable().GroupBy(f => f.ArchivePath))
            {
                try {
                    _cancelToken?.ThrowIfCancellationRequested();
                    HashSet <string> list = null;
                    if (File.Exists(groupedFiles.Key))
                    {
                        using (var proLibrary = new ProLibrary(groupedFiles.Key, _cancelToken)) {
                            list = proLibrary.Files.Select(f => f.RelativePath).ToHashSet(new HashSet <string>(StringComparer.OrdinalIgnoreCase));
                        }
                    }
                    foreach (var file in groupedFiles)
                    {
                        if (list != null && list.Contains(file.PathInArchive.ToCleanRelativePathUnix()))
                        {
                            file.Processed = true;
                            total++;
                        }
                        else
                        {
                            file.Processed = false;
                        }
                    }
                } catch (OperationCanceledException) {
                    throw;
                } catch (Exception e) {
                    throw new ArchiverException($"Failed to check files from {groupedFiles.Key}.", e);
                }
            }
            return(total);
        }
示例#2
0
        public void ProlibraryTestEdgeCases()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath) || UoeUtilities.GetProVersionFromDlc(dlcPath).Major != 11)
            {
                return;
            }

            // test the edge cases were the file entries fill 511/512/513 of the file entries block size
            var filePath = Path.Combine(TestFolder, "test_edge_cases");

            File.WriteAllText($"{filePath}1", "");
            File.WriteAllText($"{filePath}2", "");
            File.WriteAllText($"{filePath}3", "");
            File.WriteAllText($"{filePath}4", "");
            File.WriteAllText($"{filePath}5", "");

            for (int i = 65; i >= 63; i--)
            {
                // progress prolib
                OeProlibArchiver oeArchiver;
                try {
                    oeArchiver = new OeProlibArchiver(dlcPath, Encoding.Default);
                } catch (ArchiverException e) {
                    Console.WriteLine($"Cancelling test, prolib not found! : {e.Message}");
                    return;
                }
                oeArchiver.ArchiveFileSet(new List <IFileToArchive> {
                    new FileInArchive {
                        SourcePath = $"{filePath}1", PathInArchive = new string('a', 83), ArchivePath = Path.Combine(TestFolder, "test_edge_cases_official.pl")
                    },
                    new FileInArchive {
                        SourcePath = $"{filePath}2", PathInArchive = new string('b', 84), ArchivePath = Path.Combine(TestFolder, "test_edge_cases_official.pl")
                    },
                    new FileInArchive {
                        SourcePath = $"{filePath}3", PathInArchive = new string('f', 84), ArchivePath = Path.Combine(TestFolder, "test_edge_cases_official.pl")
                    },
                    new FileInArchive {
                        SourcePath = $"{filePath}4", PathInArchive = new string('c', i), ArchivePath = Path.Combine(TestFolder, "test_edge_cases_official.pl")
                    },
                    new FileInArchive {
                        SourcePath = $"{filePath}5", PathInArchive = new string('d', 1), ArchivePath = Path.Combine(TestFolder, "test_edge_cases_official.pl")
                    }
                });

                // our prolib
                File.Copy(Path.Combine(TestFolder, "test_edge_cases_official.pl"), Path.Combine(TestFolder, "test_edge_cases.pl"));
                using (var prolib = new ProLibrary(Path.Combine(TestFolder, "test_edge_cases.pl"), null)) {
                    prolib.Save();
                }

                Assert.IsTrue(File.ReadAllBytes(Path.Combine(TestFolder, "test_edge_cases_official.pl")).SequenceEqual(File.ReadAllBytes(Path.Combine(TestFolder, "test_edge_cases.pl"))), "file not recreated the same way : test_edge_cases.");

                File.Delete(Path.Combine(TestFolder, "test_edge_cases.pl"));
                File.Delete(Path.Combine(TestFolder, "test_edge_cases_official.pl"));
            }
        }
 /// <inheritdoc />
 public IEnumerable <IFileInArchive> ListFiles(string archivePath)
 {
     if (!File.Exists(archivePath))
     {
         return(Enumerable.Empty <IFileInArchive>());
     }
     using (var proLibrary = new ProLibrary(archivePath, _cancelToken)) {
         return(proLibrary.Files
                .Select(file => new FileInProlib {
             ArchivePath = archivePath,
             PathInArchive = file.RelativePath,
             LastWriteTime = file.LastWriteTime ?? DateTime.Now,
             SizeInBytes = file.Size,
             IsRcode = file.Type == ProLibraryFileType.Rcode,
             DateAdded = file.PackTime ?? DateTime.Now
         } as IFileInArchive));
     }
 }
        private int DoAction(IEnumerable <IFileArchivedBase> filesIn, Action action)
        {
            if (filesIn == null)
            {
                return(0);
            }

            var files = filesIn.ToList();

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

            int nbFilesProcessed = 0;

            foreach (var plGroupedFiles in files.GroupBy(f => f.ArchivePath))
            {
                if (action != Action.Archive && !File.Exists(plGroupedFiles.Key))
                {
                    continue;
                }
                try {
                    if (action == Action.Extract)
                    {
                        // create all necessary extraction folders
                        foreach (var extractDirGroupedFiles in plGroupedFiles.GroupBy(f => Path.GetDirectoryName(((IFileInArchiveToExtract)f).ExtractionPath)))
                        {
                            if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key))
                            {
                                Directory.CreateDirectory(extractDirGroupedFiles.Key);
                            }
                        }
                    }
                    using (var proLibrary = new ProLibrary(plGroupedFiles.Key, _cancelToken)) {
                        if (_prolibVersion != ProlibVersion.Default)
                        {
                            proLibrary.Version = _version;
                        }
                        proLibrary.CodePageName = _codePage;
                        proLibrary.OnProgress  += OnProgressionEvent;
                        try {
                            foreach (var file in plGroupedFiles)
                            {
                                var fileRelativePath = file.PathInArchive;
                                switch (action)
                                {
                                case Action.Archive:
                                    var fileToArchive = (IFileToArchive)file;
                                    if (File.Exists(fileToArchive.SourcePath))
                                    {
                                        proLibrary.AddExternalFile(fileToArchive.SourcePath, fileRelativePath);
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                    }
                                    break;

                                case Action.Extract:
                                    if (proLibrary.ExtractToFile(fileRelativePath, ((IFileInArchiveToExtract)file).ExtractionPath))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                    }
                                    break;

                                case Action.Delete:
                                    if (proLibrary.DeleteFile(fileRelativePath))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                    }
                                    break;

                                case Action.Move:
                                    if (proLibrary.MoveFile(fileRelativePath, ((IFileInArchiveToMove)file).NewRelativePathInArchive))
                                    {
                                        nbFilesProcessed++;
                                        file.Processed = true;
                                    }
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException(nameof(action), action, null);
                                }
                            }
                            if (action != Action.Extract)
                            {
                                proLibrary.Save();
                            }
                        } finally {
                            proLibrary.OnProgress -= OnProgressionEvent;
                        }
                    }
                } catch (OperationCanceledException) {
                    throw;
                } catch (Exception e) {
                    throw new ArchiverException($"Failed to move files from {plGroupedFiles.Key}.", e);
                }
            }
            return(nbFilesProcessed);
        }
示例#5
0
        public void ReadAllKindsOfLib()
        {
            CreateProlibResources();
            var list = GetProlibResources();

            foreach (var prolibResource in list)
            {
                using (var prolib = new ProLibrary(Path.Combine(TestFolder, prolibResource.FileName), null)) {
                    Assert.AreEqual(prolibResource.ContainedFiles, string.Join(",", prolib.Files.Select(f => f.RelativePath)), $"Wrong file listing for {prolibResource.FileName}.");
                    foreach (var libraryFileEntry in prolib.Files)
                    {
                        if (!File.Exists(Path.Combine(TestFolder, libraryFileEntry.RelativePath)))
                        {
                            prolib.ExtractToFile(libraryFileEntry.RelativePath, Path.Combine(TestFolder, libraryFileEntry.RelativePath));
                        }
                    }
                }
            }

            Assert.IsTrue(File.ReadAllBytes(Path.Combine(TestFolder, "file")).SequenceEqual(new byte[] { 0xAA }), "file incorrect.");
            Assert.IsTrue(File.ReadAllBytes(Path.Combine(TestFolder, "file2")).SequenceEqual(new byte[] { 0xAA, 0xAA }), "file2 incorrect.");
            Assert.IsTrue(File.ReadAllBytes(Path.Combine(TestFolder, "file.r")).SequenceEqual(GetBytesFromResource("file.r")), "file.r incorrect.");

            foreach (var prolibResource in list.Where(f => f.IsCompressed))
            {
                File.Copy(Path.Combine(TestFolder, prolibResource.FileName), Path.Combine(TestFolder, $"{prolibResource.FileName}_copy.pl"));
                using (var prolib = new ProLibrary(Path.Combine(TestFolder, $"{prolibResource.FileName}_copy.pl"), null)) {
                    prolib.Save();
                    var data = new byte[new FileInfo(Path.Combine(TestFolder, prolibResource.FileName)).Length];
                    File.ReadAllBytes(Path.Combine(TestFolder, $"{prolibResource.FileName}_copy.pl")).CopyTo(data, 0);

                    Assert.IsTrue(data.SequenceEqual(File.ReadAllBytes(Path.Combine(TestFolder, prolibResource.FileName))), $"file not recreated the same way : {prolibResource.FileName}.");
                }
            }

            if (!TestHelper.GetDlcPath(out string dlcPath) || UoeUtilities.GetProVersionFromDlc(dlcPath).Major != 11)
            {
                return;
            }
            OeProlibArchiver oeArchiver;

            try {
                oeArchiver = new OeProlibArchiver(dlcPath, Encoding.Default);
            } catch (ArchiverException e) {
                Console.WriteLine($"Cancelling test, prolib not found! : {e.Message}");
                return;
            }

            File.WriteAllBytes(Path.Combine(TestFolder, "file3"), new byte[] { 0xAA, 0xAA, 0xAA });
            File.WriteAllBytes(Path.Combine(TestFolder, "file4"), new byte[] { 0xAA, 0xAA, 0xAA, 0xAA });

            foreach (var prolibResource in list.Where(f => f.IsCompressed && f.Version == ProLibraryVersion.V11Standard))
            {
                var path = Path.Combine(TestFolder, $"{prolibResource.FileName}_copy.pl");
                Assert.AreEqual(string.Join(",", prolibResource.ContainedFiles.Split(',').OrderBy(s => s)), string.Join(",", oeArchiver.ListFiles(path).Select(f => f.PathInArchive).OrderBy(s => s)), $"Wrong file listing 2 for {prolibResource.FileName}.");
                oeArchiver.ArchiveFileSet(new List <IFileToArchive> {
                    new FileInArchive {
                        SourcePath = Path.Combine(TestFolder, "file3"), PathInArchive = "sub/file3", ArchivePath = path
                    }
                });
                oeArchiver.ArchiveFileSet(new List <IFileToArchive> {
                    new FileInArchive {
                        SourcePath = Path.Combine(TestFolder, "file4"), PathInArchive = "sub/file4", ArchivePath = path
                    }
                });
                var fileCount = (string.IsNullOrEmpty(prolibResource.ContainedFiles) ? 0 : prolibResource.ContainedFiles.Split(',').Length) + 2;

                using (var prolib = new ProLibrary(path, null)) {
                    Assert.AreEqual(fileCount, prolib.Files.Count, $"Bad count for {prolibResource.FileName}.");
                    prolib.Save();
                }
                Assert.AreEqual(fileCount, oeArchiver.ListFiles(path).Count());
            }
        }