示例#1
0
        internal static void PackMegFile(string fullyQualifiedMegFileSavePath, IEnumerable <string> absoluteFilePaths)
        {
            if (fullyQualifiedMegFileSavePath == null)
            {
                throw new ArgumentNullException(nameof(fullyQualifiedMegFileSavePath));
            }

            if (absoluteFilePaths == null)
            {
                throw new ArgumentNullException(nameof(absoluteFilePaths));
            }

            if (!PathUtility.IsValidDirectoryPath(fullyQualifiedMegFileSavePath))
            {
                throw new ArgumentException($"The provided path {fullyQualifiedMegFileSavePath} does not exist.");
            }

            IEnumerable <string> contentFiles = absoluteFilePaths.ToList();

            if (!contentFiles.Any())
            {
                throw new ArgumentException($"The meg file must contain at least one file.");
            }

            MegFileBinaryFileBuilder builder = new MegFileBinaryFileBuilder();
            MegFile megFile = builder.Build(new MegFileAttribute()
            {
                ContentFiles = contentFiles
            });

            using (BinaryWriter writer = new BinaryWriter(File.Open(fullyQualifiedMegFileSavePath, FileMode.Create)))
            {
                writer.Write(megFile.GetBytes());
            }

            foreach (string megFileFile in megFile.ContentTable
                     .MegFileContentTableRecords.SelectMany(contentTableMegFileContentTableRecord => megFile.Files.Where(megFileFile => megFile
                                                                                                                         .LookupFileNameByIndex(
                                                                                                                             Convert.ToInt32(contentTableMegFileContentTableRecord.FileNameTableRecordIndex))
                                                                                                                         .Equals(MegFileContentUtility.ExtractFileNameForMegFile(megFileFile),
                                                                                                                                 StringComparison.InvariantCultureIgnoreCase))))
            {
                using (FileStream readStream = File.OpenRead(megFileFile))
                {
                    using (FileStream writeStream = new FileStream(fullyQualifiedMegFileSavePath,
                                                                   FileMode.Append,
                                                                   FileAccess.Write, FileShare.None))
                    {
                        byte[] buffer = new byte[1024];
                        int    bytesRead;
                        while ((bytesRead = readStream.Read(buffer, 0, 1024)) > 0)
                        {
                            writeStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
        }
        public void MegBuilderBuildHeaderTest()
        {
            MegFileBinaryFileBuilder megFileBinaryFileBuilder = new MegFileBinaryFileBuilder();

            byte[]  b       = File.ReadAllBytes(TEST_DATA_PATH_IN);
            MegFile megFile = megFileBinaryFileBuilder.Build(b);

            Assert.IsNotNull(megFile);
        }
        public void MegBuilderBuildFromAttributeTest()
        {
            MegFileUtility.UnpackMegFile(TEST_DATA_PATH_IN, TEST_DATA_PATH_UNPACK_OUT);
            MegFileBinaryFileBuilder builder   = new MegFileBinaryFileBuilder();
            MegFileAttribute         attribute = new MegFileAttribute
            {
                FilePath     = TEST_DATA_PATH_PACK_OUT,
                FileName     = Path.GetFileName(TEST_DATA_PATH_PACK_OUT),
                ContentFiles = EXPECTED_FILES
            };
            MegFile megFile = builder.Build(attribute);

            Assert.IsNotNull(megFile);
        }
示例#4
0
        private static MegFile LoadMegFileFromDisk(string megFilePath)
        {
            uint headerSize = MegFileUtility.GetMegFileHeaderSize(megFilePath);

            byte[] megFileHeader = new byte[headerSize];
            using (BinaryReader reader = new BinaryReader(new FileStream(megFilePath, FileMode.Open)))
            {
                reader.Read(megFileHeader, 0, megFileHeader.Length);
            }

            MegFileBinaryFileBuilder builder = new MegFileBinaryFileBuilder();

            return(builder.Build(megFileHeader));
        }