/// <summary> /// Parses the encodings from the raw encoding string into a Hash Table/Dict /// </summary> public void BuildEncodingDictionary() { string rawEncTable = GetRawEncodingTable(); string pattern = @"(\d{1,3})([_])([0-1]*)([_]{2})?"; Match match = Regex.Match(rawEncTable, pattern); while (match.Success) { byte ch = (byte)Convert.ToInt32(match.Groups[1].Value); EncodingDict.Add(match.Groups[3].Value, ch); match = match.NextMatch(); } }
public void DecompressFile() { StringBuilder writePath = new StringBuilder(); writePath.Append(Directory.GetCurrentDirectory()); writePath.Append(@"\" + FileWrite + ".txt"); StringBuilder readPath = new StringBuilder(); readPath.Append(Directory.GetCurrentDirectory()); readPath.Append(@"\" + FileRead); FileInfo file = new FileInfo(readPath.ToString()); Console.WriteLine("File is decompressing"); using (StreamWriter writer = new StreamWriter(writePath.ToString())) { using (BinaryReader reader = new BinaryReader(File.Open(readPath.ToString(), FileMode.Open))) { reader.BaseStream.Seek(ByteOffset, SeekOrigin.Begin); bool found = false; int exponent = 0; StringBuilder encoding = new StringBuilder(); byte rByte = reader.ReadByte(); while (reader.BaseStream.Position != (reader.BaseStream.Length + ByteOffset)) { while (!found) { //byte bitLoc = (byte)Math.Pow(2, exponent); if (IsBitOn(rByte, exponent)) { encoding.Append("1"); } else { encoding.Append("0"); } byte val; if (EncodingDict.TryGetValue(encoding.ToString(), out val)) { writer.Write((char)val); encoding.Clear(); exponent++; found = true; } else { exponent++; } if (exponent > 7) { if (reader.BaseStream.Position < reader.BaseStream.Length) { rByte = reader.ReadByte(); exponent = 0; } else { Console.WriteLine("File is done Compressing"); Console.ReadKey(); Environment.Exit(0); } } } found = false; } } } }