Ejemplo n.º 1
0
        public void Load(string filePath)
        {
            using (FileStream input = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(input))
                {
                    header.Load(reader);
                    if (!header.IsCompatibleVersion())
                    {
                        throw new Exception("Incompatible archive version");
                    }

                    var blockCount = reader.ReadInt32();
                    for (var block = 0; block < blockCount; block++)
                    {
                        var text     = reader.ReadString();
                        var size     = reader.ReadInt32();
                        var data     = reader.ReadBytes(size);
                        var expected = reader.ReadUInt64();
                        var crc      = CRC64.Compute(data);
                        if (crc != expected)
                        {
                            throw new Exception($"Item checksum error: '{text}'");
                        }
                        items.Add(text, data);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 private void WriteArchive(BinaryWriter writer)
 {
     writer.Write(items.Count);
     foreach (var(key, data) in items)
     {
         writer.Write(key);
         writer.Write(data.Length);
         writer.Write(data);
         var crc = CRC64.Compute(data);
         writer.Write(crc);
     }
 }