コード例 #1
0
ファイル: GPakReader.cs プロジェクト: YasirNacak/GPAK
        public GPakReader(string filename)
        {
            PackagePath = GPakUtil.GetPackageFileName(filename);
            PackageName = Path.GetFileName(PackagePath);

            if (File.Exists(PackagePath))
            {
                PackageFileSize    = (int)new FileInfo(PackagePath).Length;
                _packageFileReader = new BinaryReader(File.Open(PackagePath, System.IO.FileMode.Open));

                if (IsHeaderValid())
                {
                    _entryTable = new Dictionary <string, GPakEntry>();
                    ReadEntries();
                }
                else
                {
                    throw new Exception("Invalid Header");
                }
            }
            else
            {
                throw new FileNotFoundException(PackagePath + " does not found.");
            }
        }
コード例 #2
0
ファイル: GPakWriter.cs プロジェクト: YasirNacak/GPAK
        public GPakWriter(string filename)
        {
            _packageFilename = GPakUtil.GetPackageFileName(filename);

            bool doesFileExist = File.Exists(_packageFilename);

            StartWriting();

            if (!doesFileExist)
            {
                WriteHeader();
            }

            EndWriting();
        }
コード例 #3
0
ファイル: GPakWriter.cs プロジェクト: YasirNacak/GPAK
        public void AddEntry(string filename, bool shouldCompress)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException(filename + " could not be opened!");
            }

            if (filename.Length > byte.MaxValue)
            {
                throw new ConstraintException("file name length can't be greater than " + byte.MaxValue);
            }

            StartWriting();

            var fi = new FileInfo(filename);

            // Write file name
            var fnLength  = Encoding.ASCII.GetByteCount(fi.Name);
            var fnContent = Encoding.ASCII.GetBytes(fi.Name);

            _fileWriter.Write(fnLength);
            _fileWriter.Write(fnContent);

            // Write file content
            int fSize;
            var fContent = File.ReadAllBytes(filename);

            if (shouldCompress)
            {
                var compressedFContent = GPakUtil.Compress(fContent);
                fSize    = compressedFContent.Length;
                fContent = compressedFContent;
                _fileWriter.Write('C');
            }
            else
            {
                fSize    = Convert.ToInt32(fi.Length);
                fContent = File.ReadAllBytes(filename);
                _fileWriter.Write('N');
            }

            _fileWriter.Write(fSize);
            _fileWriter.Write(fContent);

            EndWriting();
        }
コード例 #4
0
ファイル: GPakReader.cs プロジェクト: YasirNacak/GPAK
        public byte[] GetEntryContent(string entryName)
        {
            var entry = _entryTable[entryName];

            if (entry == null)
            {
                return(null);
            }

            var extractReader = new BinaryReader(File.Open(PackagePath, System.IO.FileMode.Open));

            var result = entry.IsCompressed
                ? GPakUtil.Decompress(GPakUtil.GetBytesFromFile(extractReader, entry.ContentOffset, entry.SizeInBytes))
                : GPakUtil.GetBytesFromFile(extractReader, entry.ContentOffset, entry.SizeInBytes);

            extractReader.Close();

            return(result);
        }
コード例 #5
0
ファイル: GPakReader.cs プロジェクト: YasirNacak/GPAK
        public void ExtractAll(string toDirectory = "")
        {
            if (toDirectory != "" && !Directory.Exists(toDirectory))
            {
                Directory.CreateDirectory(toDirectory);
            }

            var extractReader = new BinaryReader(File.Open(PackagePath, System.IO.FileMode.Open));

            foreach (var(name, entry) in _entryTable)
            {
                var writer = new BinaryWriter(File.Open(Path.Combine(toDirectory, name), System.IO.FileMode.Create));

                writer.Write(entry.IsCompressed
                    ? GPakUtil.Decompress(GPakUtil.GetBytesFromFile(extractReader, entry.ContentOffset, entry.SizeInBytes))
                    : GPakUtil.GetBytesFromFile(extractReader, entry.ContentOffset, entry.SizeInBytes));

                writer.Close();
            }

            extractReader.Close();
        }
コード例 #6
0
ファイル: GPakReader.cs プロジェクト: YasirNacak/GPAK
        private void ReadEntries()
        {
            int i = GPakUtil.FirstEntryOffset;

            while (i < PackageFileSize)
            {
                // Read 4 bytes to find the length of the entry's name
                var entryNameLength =
                    GPakUtil.GetByteRangeAsInteger(GPakUtil.GetBytesFromFile(_packageFileReader, i, 4));
                i += 4;

                // Read ENTRY_NAME bytes to obtain it's name
                var entryName =
                    GPakUtil.GetByteRangeAsString(GPakUtil.GetBytesFromFile(_packageFileReader, i, entryNameLength));
                i += entryName.Length;

                // Read 1 byte to check if the entry is compressed or not ("C" for compressed, "N" for not compressed)
                var compressedFlag =
                    GPakUtil.GetByteRangeAsString(GPakUtil.GetBytesFromFile(_packageFileReader, i, 1));
                i += 1;
                var isCompressed = compressedFlag == "C";

                // Read 4 bytes to get the size of the entry in the package
                var entryContentSize =
                    GPakUtil.GetByteRangeAsInteger(GPakUtil.GetBytesFromFile(_packageFileReader, i, 4));
                i += 4;

                // Create a new entry based on the previously gathered info
                _entryTable.Add(entryName, new GPakEntry(entryContentSize, isCompressed, i));

                i += entryContentSize;

                EntryCount++;
            }

            _packageFileReader.Close();
        }
コード例 #7
0
ファイル: GPakReader.cs プロジェクト: YasirNacak/GPAK
        private bool IsHeaderValid()
        {
            var header = GPakUtil.GetBytesFromFile(_packageFileReader, 0, GPakUtil.FirstEntryOffset);

            return(GPakUtil.GetByteRangeAsString(header).Equals(GPakUtil.Header));
        }