public void Zlib_ZlibStream_CompressWhileWriting()
        {
            System.IO.MemoryStream msSinkCompressed;
            System.IO.MemoryStream msSinkDecompressed;
            ZlibStream zOut;

            // first, compress:
            msSinkCompressed = new System.IO.MemoryStream();
            zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true);
            CopyStream(StringToMemoryStream(IhaveaDream), zOut);
            zOut.Close();

            // at this point, msSinkCompressed contains the compressed bytes

            // now, decompress:
            msSinkDecompressed = new System.IO.MemoryStream();
            zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress);
            msSinkCompressed.Position = 0;
            CopyStream(msSinkCompressed, zOut);

            string result = MemoryStreamToString(msSinkDecompressed);
            TestContext.WriteLine("decompressed: {0}", result);
            Assert.AreEqual<String>(IhaveaDream, result);
        }
        public void Zlib_ZlibStream_CompressWhileReading_wi8557()
        {
            // workitem 8557
            System.IO.MemoryStream msSinkCompressed;
            System.IO.MemoryStream msSinkDecompressed;

            // first, compress:
            msSinkCompressed = new System.IO.MemoryStream();
            ZlibStream zIn= new ZlibStream(StringToMemoryStream(WhatWouldThingsHaveBeenLike),
                                           CompressionMode.Compress,
                                           CompressionLevel.BestCompression,
                                           true);
            CopyStream(zIn, msSinkCompressed);

            // At this point, msSinkCompressed contains the compressed bytes.
            // Now, decompress:
            msSinkDecompressed = new System.IO.MemoryStream();
            ZlibStream zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress);
            msSinkCompressed.Position = 0;
            CopyStream(msSinkCompressed, zOut);

            string result = MemoryStreamToString(msSinkDecompressed);
            TestContext.WriteLine("decompressed: {0}", result);
            Assert.AreEqual<String>(WhatWouldThingsHaveBeenLike, result);
        }
        public void zlib_Compat_decompress_wi13446()
        {
            var zlibbedFile = GetContentFile("zlibbed.file");
            var streamCopy = new Action<Stream,Stream,int>( (source,dest,bufferSize) => {
                    var temp = new byte[bufferSize];
                    while (true)
                    {
                        var read = source.Read(temp, 0, temp.Length);
                        if (read <= 0) break;
                        dest.Write(temp, 0, read);
                    }
                });

            var unpack = new Action<int> ((bufferSize) => {
                    using (var output = new MemoryStream())
                    {
                        using (var input = File.OpenRead(zlibbedFile))
                        {
                            using (var zinput = new ZlibStream(input, CompressionMode.Decompress))
                            {
                                streamCopy(zinput, output, bufferSize);
                            }
                        }
                    }
                });

            unpack(1024);
            unpack(16384);
        }
        public void Zlib_DisposedException_ZlibStream()
        {
            string TextToCompress =  IhaveaDream;

            MemoryStream ms1= new MemoryStream();

            Stream compressor= new ZlibStream(ms1, CompressionMode.Compress, false);

            TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                  TextToCompress.Length, TextToCompress);
            TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

            StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
            sw.Write(TextToCompress);
            sw.Close(); // implicitly closes compressor
            sw.Close(); // implicitly closes compressor, again

            compressor.Close(); // explicitly closes compressor
            var a = ms1.ToArray();
            TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

            var ms2 = new MemoryStream(a);
            Stream decompressor  = new ZlibStream(ms2, CompressionMode.Decompress, false);

            TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

            var sr = new StreamReader(decompressor, Encoding.ASCII);
            string DecompressedText = sr.ReadToEnd();
            sr.Close();

            TestContext.WriteLine("decompressor.CanRead = {0}",decompressor.CanRead);

            TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
            TestContext.WriteLine("\n");
            Assert.AreEqual<String>(TextToCompress, DecompressedText);
        }
        public void Zlib_CloseTwice()
        {
            string TextToCompress = LetMeDoItNow;

            for (int i = 0; i < 3; i++)
            {
                MemoryStream ms1= new MemoryStream();

                Stream compressor = null;
                switch (i)
                {
                case 0:
                    compressor= new DeflateStream(ms1, CompressionMode.Compress, CompressionLevel.BestCompression, false);
                    break;
                case 1:
                    compressor = new GZipStream(ms1, CompressionMode.Compress, false);
                    break;
                case 2:
                    compressor = new ZlibStream(ms1, CompressionMode.Compress, false);
                    break;
                }

                TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                      TextToCompress.Length, TextToCompress);
                TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

                StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
                sw.Write(TextToCompress);
                sw.Close(); // implicitly closes compressor
                sw.Close();// implicitly closes compressor, again

                compressor.Close(); // explicitly closes compressor
                var a = ms1.ToArray();
                TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

                var ms2 = new MemoryStream(a);
                Stream decompressor = null;

                switch (i)
                {
                case 0:
                    decompressor = new DeflateStream(ms2, CompressionMode.Decompress, false);
                    break;
                case 1:
                    decompressor = new GZipStream(ms2, CompressionMode.Decompress, false);
                    break;
                case 2:
                    decompressor = new ZlibStream(ms2, CompressionMode.Decompress, false);
                    break;
                }

                TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

                var sr = new StreamReader(decompressor, Encoding.ASCII);
                string DecompressedText = sr.ReadToEnd();

                // verify that multiple calls to Close() do not throw
                sr.Close();
                sr.Close();
                decompressor.Close();

                TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
                TestContext.WriteLine("\n");
                Assert.AreEqual<String>(TextToCompress, DecompressedText);
            }
        }
예제 #6
0
        /// <summary>
        ///   Uncompress a ZLIB-compressed byte array into a byte array.
        /// </summary>
        ///
        /// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
        /// <seealso cref="ZlibStream.UncompressString(byte[])"/>
        ///
        /// <param name="compressed">
        ///   A buffer containing ZLIB-compressed data.
        /// </param>
        ///
        /// <returns>The data in uncompressed form</returns>
        public static byte[] UncompressBuffer(byte[] compressed)
        {
            using (var input = new MemoryStream(compressed))
            {
                Stream decompressor =
                    new ZlibStream( input, CompressionMode.Decompress );

                return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
            }
        }
예제 #7
0
        /// <summary>
        ///   Compress a byte array into a new byte array using ZLIB.
        /// </summary>
        ///
        /// <remarks>
        ///   Uncompress it with <see cref="ZlibStream.UncompressBuffer(byte[])"/>.
        /// </remarks>
        ///
        /// <seealso cref="ZlibStream.CompressString(string)"/>
        /// <seealso cref="ZlibStream.UncompressBuffer(byte[])"/>
        ///
        /// <param name="b">
        /// A buffer to compress.
        /// </param>
        ///
        /// <returns>The data in compressed form</returns>
        public static byte[] CompressBuffer(byte[] b)
        {
            using (var ms = new MemoryStream())
            {
                Stream compressor =
                    new ZlibStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression );

                ZlibBaseStream.CompressBuffer(b, compressor);
                return ms.ToArray();
            }
        }