SetDictionary() public method

Set the dictionary to be used for either Inflation or Deflation.
public SetDictionary ( byte dictionary ) : int
dictionary byte The dictionary bytes to use.
return int
Exemplo n.º 1
0
        private int LoadSharedDictionary()
        {
            if (sharedDictionary == null)
            {
                throw new ZlibException("Shared dictionary is required to decompress this stream");
            }
            if (_z.Adler32 != sharedDictionaryAdler32)
            {
                throw new ZlibException("Shared dictionary checksum verification failed. Please make sure you use same dictionary for compression and decompression.");
            }
            int rc = _z.SetDictionary(sharedDictionary);

            if (rc != ZlibConstants.Z_OK)
            {
                throw new ZlibException("Error setting shared dictionary: " + _z.Message);
            }

            return(rc);
        }
Exemplo n.º 2
0
        public void Zlib_BasicDictionaryDeflateInflate()
        {
            int rc;
            int comprLen = 40000;
            int uncomprLen = comprLen;
            byte[] uncompr = new byte[uncomprLen];
            byte[] compr = new byte[comprLen];
            //long dictId;

            ZlibCodec compressor = new ZlibCodec();
            rc = compressor.InitializeDeflate(CompressionLevel.BestCompression);
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at InitializeDeflate() [{0}]", compressor.Message));

            string dictionaryWord = "hello ";
            byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord);
            string TextToCompress = "hello, hello!  How are you, Joe? I said hello. ";
            byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

            rc = compressor.SetDictionary(dictionary);
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at SetDeflateDictionary() [{0}]", compressor.Message));

            int dictId = compressor.Adler32;

            compressor.OutputBuffer = compr;
            compressor.NextOut = 0;
            compressor.AvailableBytesOut = comprLen;

            compressor.InputBuffer = BytesToCompress;
            compressor.NextIn = 0;
            compressor.AvailableBytesIn = BytesToCompress.Length;

            rc = compressor.Deflate(FlushType.Finish);
            Assert.AreEqual<int>(ZlibConstants.Z_STREAM_END, rc, String.Format("at Deflate() [{0}]", compressor.Message));

            rc = compressor.EndDeflate();
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at EndDeflate() [{0}]", compressor.Message));


            ZlibCodec decompressor = new ZlibCodec();

            decompressor.InputBuffer = compr;
            decompressor.NextIn = 0;
            decompressor.AvailableBytesIn = comprLen;

            rc = decompressor.InitializeInflate();
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at InitializeInflate() [{0}]", decompressor.Message));

            decompressor.OutputBuffer = uncompr;
            decompressor.NextOut = 0;
            decompressor.AvailableBytesOut = uncomprLen;

            while (true)
            {
                rc = decompressor.Inflate(FlushType.None);
                if (rc == ZlibConstants.Z_STREAM_END)
                {
                    break;
                }
                if (rc == ZlibConstants.Z_NEED_DICT)
                {
                    Assert.AreEqual<long>(dictId, decompressor.Adler32, "Unexpected Dictionary");
                    rc = decompressor.SetDictionary(dictionary);
                }
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Inflate/SetInflateDictionary() [{0}]", decompressor.Message));
            }

            rc = decompressor.EndInflate();
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at EndInflate() [{0}]", decompressor.Message));

            int j = 0;
            for (; j < uncompr.Length; j++)
                if (uncompr[j] == 0)
                    break;

            Assert.AreEqual<int>(TextToCompress.Length, j, String.Format("Unequal lengths"));

            int i = 0;
            for (i = 0; i < j; i++)
                if (TextToCompress[i] != uncompr[i])
                    break;

            Assert.AreEqual<int>(j, i, String.Format("Non-identical content"));

            var result = System.Text.ASCIIEncoding.ASCII.GetString(uncompr, 0, j);

            TestContext.WriteLine("orig length: {0}", TextToCompress.Length);
            TestContext.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
            TestContext.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
            TestContext.WriteLine("result length: {0}", result.Length);
            TestContext.WriteLine("result of inflate:\n{0}", result);
        }
    private void Run()
    {
        int rc;
        int bufferSize = 40000;
        byte[] compressedBytes = new byte[bufferSize];
        byte[] decompressedBytes = new byte[bufferSize];
        
        ZlibCodec compressingStream = new ZlibCodec();
        rc = compressingStream.InitializeDeflate(CompressionLevel.LEVEL9_BEST_COMPRESSION);
        CheckForError(compressingStream, rc, "InitializeDeflate");

        string dictionaryWord = "hello ";
        byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord);
        string TextToCompress = "hello, hello!  How are you, Joe? ";
        byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

        rc = compressingStream.SetDictionary(dictionary);
        CheckForError(compressingStream, rc, "SetDeflateDictionary");

        long dictId = compressingStream.Adler32;

        compressingStream.OutputBuffer = compressedBytes;
        compressingStream.NextOut = 0;
        compressingStream.AvailableBytesOut = bufferSize;

        compressingStream.InputBuffer = BytesToCompress;
        compressingStream.NextIn = 0;
        compressingStream.AvailableBytesIn = BytesToCompress.Length;

        rc = compressingStream.Deflate(ZlibConstants.Z_FINISH);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            System.Console.Out.WriteLine("deflate should report Z_STREAM_END");
            System.Environment.Exit(1);
        }
        rc = compressingStream.EndDeflate();
        CheckForError(compressingStream, rc, "deflateEnd");

        ZlibCodec decompressingStream = new ZlibCodec();

        decompressingStream.InputBuffer = compressedBytes;
        decompressingStream.NextIn = 0;
        decompressingStream.AvailableBytesIn = bufferSize;

        rc = decompressingStream.InitializeInflate();
        CheckForError(decompressingStream, rc, "inflateInit");
        decompressingStream.OutputBuffer = decompressedBytes;
        decompressingStream.NextOut = 0;
        decompressingStream.AvailableBytesOut = decompressedBytes.Length;

        while (true)
        {
            rc = decompressingStream.Inflate(ZlibConstants.Z_NO_FLUSH);
            if (rc == ZlibConstants.Z_STREAM_END)
            {
                break;
            }
            if (rc == ZlibConstants.Z_NEED_DICT)
            {
                if ((int)decompressingStream.Adler32 != (int)dictId)
                {
                    System.Console.Out.WriteLine("unexpected dictionary");
                    System.Environment.Exit(1);
                }
                rc = decompressingStream.SetDictionary(dictionary);
            }
            CheckForError(decompressingStream, rc, "inflate with dict");
        }

        rc = decompressingStream.EndInflate();
        CheckForError(decompressingStream, rc, "EndInflate");

        int j = 0;
        for (; j < decompressedBytes.Length; j++)
            if (decompressedBytes[j] == 0)
                break;

        var result = System.Text.ASCIIEncoding.ASCII.GetString(decompressedBytes, 0, j);

        Console.WriteLine("orig length: {0}", TextToCompress.Length);
        Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
        Console.WriteLine("decompressed length: {0}", decompressingStream.TotalBytesOut);
        Console.WriteLine("result length: {0}", result.Length);
        Console.WriteLine("result of inflate:\n{0}", result);
    }