コード例 #1
0
        public Boolean compress(String fileToRead)
        {
            initializeBuffer(fileToRead);

            Token token;
            int   slidingWindowOffset = 0;
            int   currentPosition     = 0;

            char tokenCharacter;
            int  tokenOffset = 0;
            int  tokenLength = 0;

            int patternLength = 0;
            int i             = 0;
            int off           = 0;

            var pos = 0;

            while ((currentPosition < entryBuffer.Count()) && (currentPosition < maxNoBitsForLength + entryBuffer.Count()))
            {
                tokenCharacter = (char)entryBuffer[currentPosition];

                if (currentPosition != 0)
                {
                    tokenLength = 0;
                    tokenOffset = 0;

                    for (off = 0; off < slidingWindowOffset; off++)
                    {
                        i             = 0;
                        patternLength = 0;

                        pos = currentPosition - off + i - 1;
                        while ((pos < currentPosition) && (entryBuffer[pos] == entryBuffer[currentPosition + i]) && ((currentPosition + i) < maxNoBitsForLength))
                        {
                            i++;
                            patternLength++;
                            pos = currentPosition - off + i - 1;
                        }

                        if (i > 0)
                        {
                            if (patternLength > tokenLength)
                            {
                                tokenLength    = patternLength;
                                tokenOffset    = off;
                                tokenCharacter = (char)entryBuffer[currentPosition + i];
                            }
                        }
                    }
                    currentPosition += tokenLength + 1;
                }
                else
                {
                    currentPosition++;
                }

                if ((currentPosition > entryBuffer.Count) && (tokenLength > 0))
                {
                    token = new Token(tokenOffset, tokenLength - 1, (char)entryBuffer[currentPosition - 2]);
                    tokens.Add(token);
                }
                else
                {
                    // var te = tokenLength;
                    token = new Token(tokenOffset, tokenLength, tokenCharacter);
                    tokens.Add(token);
                }

                slidingWindowOffset = ((currentPosition - maxNoBitsForOffset) > 0) ? (currentPosition - maxNoBitsForOffset) : (currentPosition);
            }

            //generate the compressed file
            String compressedFile = "File.ext.o" + noBitsForOffset + "l" + noBitsForLength + ".lz77";

            BitWriter bitWriter = new BitWriter(compressedFile);

            writeTokensToFile(tokens, bitWriter);

            return(true);
        }
コード例 #2
0
 private void writeHeader(BitWriter bitWriter)
 {
     bitWriter.writeNBits(noBitsForOffset, 5);
     bitWriter.writeNBits(noBitsForLength, 3);
 }