private void Write(string inputFile, string outputFile) { using (BitWriter bitWriter = new BitWriter(outputFile)) { //header WriteAlocationMap(bitWriter); WriteCharFrequencyOn4Bytes(bitWriter); FileInfo file = new FileInfo(inputFile); int numberOfBits = 8 * (int)file.Length; using (BitReader bitReader = new BitReader(inputFile)) { while (numberOfBits > 0) { int character = bitReader.readNBits(8); bitWriter.WriteNBits(_charCode[character], _codeLength[character]); numberOfBits -= 8; } bitWriter.WriteNBits(0, 7); } } }
private void Encode(string s, BitWriter bitWriter) { bitWriter.WriteNBits(_symbolList.IndexOf(s.ToString()), _index); }
public void Compress(string inputFile, string outputFile) { FileInfo file = new FileInfo(inputFile); buffer = new byte[file.Length]; int index = 0; using (BitReader bitReader = new BitReader(inputFile)) using (BitWriter bitWriter = new BitWriter(outputFile)) { FillBuffer(bitReader); int character = buffer[index]; int position = 0; int length = 0; //first token WriteToken(bitWriter, position, length, character); _slidingWindow.Add(character); index++; character = buffer[index]; length = 0; while (index < buffer.Length) { int i = 0; string prevChar = null; while (i < _searchBufferMaxLength) { if (i < _slidingWindow.Count) { break; } int lookAheadIndex = index - i - 1; if (buffer[lookAheadIndex] == character) { //vezi cat de lunga ii secventa de potrivire prevChar = character.ToString(); int j = lookAheadIndex; while (j < _lookAheadBufferMaxLength + i) { if (j > buffer.Length) { break; } character = buffer[lookAheadIndex]; } } else { i++; } } } /*******************************************************************************/ //first token WriteToken(bitWriter, position, length, character); _slidingWindow.Add(character); character = bitReader.readNBits(8); position = 0; if (_slidingWindow[0] == character) { length = 1; _slidingWindow.Add(character); character = bitReader.readNBits(8); } else { length = 0; } //second token WriteToken(bitWriter, position, length, character); _slidingWindow.Add(character); character = bitReader.readNBits(8); if (_slidingWindow[1] == character) { position = 0; _slidingWindow.Add(character); character = bitReader.readNBits(8); if (_slidingWindow[2] == character) { } else { } } else if (_slidingWindow[0] == character) { position = 1; _slidingWindow.Add(character); character = bitReader.readNBits(8); if (_slidingWindow[1] == character) { } else { length = 1; } } //third token WriteToken(bitWriter, position, length, character); } }
private void DecodeAndWrite(BitReader bitReader, int fileLength, TreeNode <KeyValuePair <int, int> > huffmanTree, BitWriter bitWriter) { TreeNode <KeyValuePair <int, int> > currentNode; for (int i = 0; i < fileLength; i++) { currentNode = huffmanTree; while (!currentNode.IsLeaf()) { int bit = bitReader.readBit(); currentNode = bit == 0 ? currentNode.leftChild : currentNode.rightChild; } //scrie caracterul int decodedChar = currentNode.value.Key; bitWriter.WriteNBits(decodedChar, 8); } }
private void WriteToken(BitWriter bitWriter, int position, int length, int character) { bitWriter.WriteNBits(position, _p); bitWriter.WriteNBits(length, _l); bitWriter.WriteNBits(character, 8); }