public void Compress() { Dictionary<byte, string> codes = new Dictionary<byte, string>(); foreach (var s in Stats.Bytes) codes.Add(s.Byte, s.Code); List<byte> Compressed = new List<byte>(); using (MemoryStream mStream = new MemoryStream()) { StringBuilder buffer = new StringBuilder(); foreach (var b in _rawData) { var code = codes[b]; buffer.Append(code); } string sBuffer = buffer.ToString(); int bytesLength = (sBuffer.Length % 8 == 0) ? sBuffer.Length / 8 : sBuffer.Length / 8 + 1; for (int i = 0; i < bytesLength * 8; i += 8) { string cache = (sBuffer.Length >= i + 8) ? sBuffer.Substring(i, 8) : sBuffer.Substring(i); if (cache.Length < 8) cache = cache.PadRight(8, '0'); mStream.WriteByte(Convert.ToByte(cache, 2)); } _archive = new Archive(codes, (uint)_rawData.Count(), mStream.ToArray()); } }
public void ReadFromFile(string fileName) { byte[] compressed = File.ReadAllBytes(fileName); using (MemoryStream stream = new MemoryStream(compressed)) { using (BinaryReader reader = new BinaryReader(stream)) { _archive = new Archive(reader); } } }