Example #1
0
        public Boolean compress(String fileToRead)
        {
            BitReader bitReader = new BitReader(fileToRead);

            FileInfo f   = new FileInfo(fileToRead);
            int      nbr = 8 * (int)f.Length;

            fillDictionaryFixedPart(symbolDictionary);

            String        symbol = "";
            List <String> list;

            int currentPosition = symbolDictionary.Count();

            while (nbr > 0)
            {
                int character = bitReader.readNBits(8);

                var c          = (char)character;
                var charString = c.ToString();

                int charIndex = getIndexFromDictionary(symbolDictionary, symbol + charString);
                if ((charIndex != -1) && (charIndex <= indexMaxSize))
                {
                    symbol = symbol + charString;
                }
                else
                {
                    if (symbolDictionary.Count > indexMaxSize)
                    {
                        if (freeze == 0)
                        {
                            symbolDictionary.Clear();
                            fillDictionaryFixedPart(symbolDictionary);
                        }
                    }
                    list = new List <string>();
                    list.Add(symbol + charString);
                    list.Add(symbol);
                    symbolDictionary.Add(currentPosition, list);
                    currentPosition++;

                    symbol = charString;
                }

                nbr -= 8;

                if (nbr <= 0)
                {
                    list = new List <string>();
                    list.Add(symbol + charString);
                    list.Add(symbol);
                    symbolDictionary.Add(currentPosition, list);
                }
            }

            bitReader.cleanUp();

            String    compressedFile = "File.ext." + ((freeze == 1) ? "f" : "e") + "l" + index + ".lzw";
            BitWriter bitWriter      = new BitWriter(compressedFile);

            writeCompressedFile(symbolDictionary, bitWriter);

            bitWriter.cleanUp();

            return(true);
        }
Example #2
0
        private void writeCompressedFile(Dictionary <int, List <String> > symbolDictionary, BitWriter bitWriter)
        {
            writeheader(bitWriter);

            Dictionary <int, List <String> > dictionaryFromInput = getDictionaryElements();

            int valueIndex = 0;

            foreach (KeyValuePair <int, List <String> > symbol in dictionaryFromInput)
            {
                var element = (symbol.Key != noOfSymbols) ? symbol.Value[1] : symbol.Value[0][0].ToString();
                valueIndex = getIndexFromDictionary(symbolDictionary, element);
                bitWriter.writeNBits(valueIndex, index);
            }

            bitWriter.writeNBits(0, 7);
        }
Example #3
0
 private void writeheader(BitWriter bitWriter)
 {
     bitWriter.writeBit(freeze);
     bitWriter.writeNBits(index, 4);
 }
Example #4
0
        public Boolean decompress(String compressedFileToRead)
        {
            BitReader bitReader = new BitReader(compressedFileToRead);

            FileInfo f   = new FileInfo(compressedFileToRead);
            int      nbr = 8 * (int)f.Length;

            symbolDecompressDictionary = new Dictionary <int, List <string> >();

            fillDictionaryFixedPart(symbolDecompressDictionary);

            String        symbol = "";
            List <String> list;

            int currentPosition = noOfSymbols;

            int[] headerData = getDataFromHeader(bitReader, nbr);
            nbr -= 5;

            this.freeze = headerData[0];
            this.index  = headerData[1];

            int characterIndex = bitReader.readNBits(index);
            var charString     = symbolDecompressDictionary[characterIndex][0];

            decompressedResult.Add(charString);
            symbol = charString;
            nbr   -= index;

            while (nbr >= index + 8)
            {
                characterIndex = bitReader.readNBits(index);

                if (characterIndex >= symbolDecompressDictionary.Count)
                {
                    charString = symbol + symbol[0];
                }
                else
                {
                    charString = symbolDecompressDictionary[characterIndex][0];
                }

                decompressedResult.Add(charString);

                if (symbolDictionary.Count > indexMaxSize)
                {
                    if (freeze == 0)
                    {
                        symbolDecompressDictionary.Clear();
                        fillDictionaryFixedPart(symbolDecompressDictionary);
                    }
                }
                list = new List <string>();
                list.Add(symbol + charString[0]);
                symbolDecompressDictionary.Add(currentPosition, list);
                currentPosition++;

                symbol = charString;
                nbr   -= index;
            }

            //generate the decompressed file
            String    decompressedFile = "File.ext." + ((freeze == 1) ? "f" : "e") + "l" + index + ".lzw.ext";
            BitWriter bitWriter        = new BitWriter(decompressedFile);

            writeDecompressedDataToFile(decompressedResult, bitWriter);
            bitWriter.cleanUp();

            return(true);
        }