Ejemplo n.º 1
0
        public void Zlib_ParallelDeflateStream()
        {
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            TestContext.WriteLine("{0}: Zlib_ParallelDeflateStream Start", sw.Elapsed);

            int sz = 256*1024 + this.rnd.Next(120000);
            string FileToCompress = System.IO.Path.Combine(TopLevelDir, String.Format("Zlib_ParallelDeflateStream.{0}.txt", sz));

            CreateAndFillFileText( FileToCompress, sz);

            TestContext.WriteLine("{0}: Created file: {1}", sw.Elapsed, FileToCompress );

            byte[] original = File.ReadAllBytes(FileToCompress);

            int crc1 = DoCrc(FileToCompress);

            TestContext.WriteLine("{0}: Original CRC: {1:X8}", sw.Elapsed, crc1 );

            byte[] working = new byte[WORKING_BUFFER_SIZE];
            int n = -1;
            long originalLength;
            MemoryStream ms1 = new MemoryStream();
            {
                using (FileStream fs1 = File.OpenRead(FileToCompress))
                {
                    originalLength = fs1.Length;
                    using (var compressor = new Alienlab.Zlib.ParallelDeflateOutputStream(ms1, true))
                    {
                        while ((n = fs1.Read(working, 0, working.Length)) != 0)
                        {
                            compressor.Write(working, 0, n);
                        }
                    }
                }
                ms1.Seek(0, SeekOrigin.Begin);
            }

            TestContext.WriteLine("{0}: Compressed {1} bytes into {2} bytes", sw.Elapsed,
                                  originalLength, ms1.Length);

            var crc = new Alienlab.Crc.CRC32();
            int crc2= 0;
            byte[] decompressedBytes= null;
            using (MemoryStream ms2 = new MemoryStream())
            {
                using (var decompressor = new DeflateStream(ms1, CompressionMode.Decompress, false))
                {
                    while ((n = decompressor.Read(working, 0, working.Length)) != 0)
                    {
                        ms2.Write(working, 0, n);
                    }
                }
                TestContext.WriteLine("{0}: Decompressed", sw.Elapsed);
                TestContext.WriteLine("{0}: Decompressed length: {1}", sw.Elapsed, ms2.Length);
                ms2.Seek(0, SeekOrigin.Begin);
                crc2 = crc.GetCrc32(ms2);
                decompressedBytes = ms2.ToArray();
                TestContext.WriteLine("{0}: Decompressed CRC: {1:X8}", sw.Elapsed, crc2 );
            }


            TestContext.WriteLine("{0}: Checking...", sw.Elapsed );

            bool check = true;
            if (originalLength != decompressedBytes.Length)
            {
                TestContext.WriteLine("Different lengths.");
                check = false;
            }
            else
            {
                for (int i = 0; i < decompressedBytes.Length; i++)
                {
                    if (original[i] != decompressedBytes[i])
                    {
                        TestContext.WriteLine("byte {0} differs", i);
                        check = false;
                        break;
                    }
                }
            }

            Assert.IsTrue(check,"Data check failed");
            TestContext.WriteLine("{0}: Done...", sw.Elapsed );
        }
Ejemplo n.º 2
0
        public void Zlib_Streams_VariousSizes()
        {
            byte[] working = new byte[WORKING_BUFFER_SIZE];
            int n = -1;
            Int32[] Sizes = { 8000, 88000, 188000, 388000, 580000, 1580000 };

            for (int p = 0; p < Sizes.Length; p++)
            {
                // both binary and text files
                for (int m = 0; m < 2; m++)
                {
                    int sz = this.rnd.Next(Sizes[p]) + Sizes[p];
                    string FileToCompress = System.IO.Path.Combine(TopLevelDir, String.Format("Zlib_Streams.{0}.{1}", sz, (m == 0) ? "txt" : "bin"));
                    Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary file '{0}' already exists.", FileToCompress);
                    TestContext.WriteLine("Creating file {0}   {1} bytes", FileToCompress, sz);
                    if (m == 0)
                        CreateAndFillFileText(FileToCompress, sz);
                    else
                        _CreateAndFillBinary(FileToCompress, sz, false);

                    int crc1 = DoCrc(FileToCompress);
                    TestContext.WriteLine("Initial CRC: 0x{0:X8}", crc1);

                    // try both GZipStream and DeflateStream
                    for (int k = 0; k < 2; k++)
                    {
                        // compress with Alienlab.and System.IO.Compression
                        for (int i = 0; i < 2; i++)
                        {
                            string CompressedFileRoot = String.Format("{0}.{1}.{2}.compressed", FileToCompress,
                                              (k == 0) ? "GZIP" : "DEFLATE",
                                              (i == 0) ? "Ionic" : "BCL");

                            int x = k + i * 2;
                            int z = (x == 0) ? 4 : 1;
                            // why 4 trials??   (only for GZIP and Alienlab.
                            for (int h = 0; h < z; h++)
                            {
                                string CompressedFile = (x == 0)
                                    ? CompressedFileRoot + ".trial" + h
                                    : CompressedFileRoot;

                                using (var input = System.IO.File.OpenRead(FileToCompress))
                                {
                                    using (var raw = System.IO.File.Create(CompressedFile))
                                    {
                                        Stream compressor = null;
                                        try
                                        {
                                            switch (x)
                                            {
                                                case 0: // k == 0, i == 0
                                                    compressor = new Alienlab.Zlib.GZipStream(raw, CompressionMode.Compress, true);
                                                    break;
                                                case 1: // k == 1, i == 0
                                                    compressor = new Alienlab.Zlib.DeflateStream(raw, CompressionMode.Compress, true);
                                                    break;
                                                case 2: // k == 0, i == 1
                                                    compressor = new System.IO.Compression.GZipStream(raw, System.IO.Compression.CompressionMode.Compress, true);
                                                    break;
                                                case 3: // k == 1, i == 1
                                                    compressor = new System.IO.Compression.DeflateStream(raw, System.IO.Compression.CompressionMode.Compress, true);
                                                    break;
                                            }
                                            //TestContext.WriteLine("Compress with: {0} ..", compressor.GetType().FullName);

                                            TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(CompressedFile));

                                            if (x == 0)
                                            {
                                                if (h != 0)
                                                {
                                                    Alienlab.Zlib.GZipStream gzip = compressor as Alienlab.Zlib.GZipStream;

                                                    if (h % 2 == 1)
                                                        gzip.FileName = FileToCompress;

                                                    if (h > 2)
                                                        gzip.Comment = "Compressing: " + FileToCompress;

                                                }
                                            }

                                            n = -1;
                                            while ((n = input.Read(working, 0, working.Length)) != 0)
                                            {
                                                compressor.Write(working, 0, n);
                                            }

                                        }
                                        finally
                                        {
                                            if (compressor != null)
                                                compressor.Dispose();
                                        }
                                    }
                                }

                                // now, decompress with Alienlab.and System.IO.Compression
                                // for (int j = 0; j < 2; j++)
                                for (int j = 1; j >= 0; j--)
                                {
                                    using (var input = System.IO.File.OpenRead(CompressedFile))
                                    {
                                        Stream decompressor = null;
                                        try
                                        {
                                            int w = k + j * 2;
                                            switch (w)
                                            {
                                                case 0: // k == 0, j == 0
                                                    decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true);
                                                    break;
                                                case 1: // k == 1, j == 0
                                                    decompressor = new Alienlab.Zlib.DeflateStream(input, CompressionMode.Decompress, true);
                                                    break;
                                                case 2: // k == 0, j == 1
                                                    decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
                                                    break;
                                                case 3: // k == 1, j == 1
                                                    decompressor = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress, true);
                                                    break;
                                            }

                                            //TestContext.WriteLine("Decompress: {0} ...", decompressor.GetType().FullName);
                                            string DecompressedFile =
                                                String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL");

                                            TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile));

                                            using (var s2 = System.IO.File.Create(DecompressedFile))
                                            {
                                                n = -1;
                                                while (n != 0)
                                                {
                                                    n = decompressor.Read(working, 0, working.Length);
                                                    if (n > 0)
                                                        s2.Write(working, 0, n);
                                                }
                                            }

                                            int crc2 = DoCrc(DecompressedFile);
                                            Assert.AreEqual<UInt32>((UInt32)crc1, (UInt32)crc2);

                                        }
                                        finally
                                        {
                                            if (decompressor != null)
                                                decompressor.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            TestContext.WriteLine("Done.");
        }
Ejemplo n.º 3
0
        private void PerformTrialWi8870(byte[] buffer)
        {
            TestContext.WriteLine("Original");

            byte[] compressedBytes = null;
            using (MemoryStream ms1 = new MemoryStream())
            {
                using (DeflateStream compressor = new DeflateStream(ms1, CompressionMode.Compress, false))
                {
                    compressor.Write(buffer, 0, buffer.Length);
                }
                compressedBytes = ms1.ToArray();
            }

            TestContext.WriteLine("Compressed {0} bytes into {1} bytes",
                                  buffer.Length, compressedBytes.Length);

            byte[] decompressed= null;
            using (MemoryStream ms2 = new MemoryStream())
            {
                using (var deflateStream = new DeflateStream(ms2, CompressionMode.Decompress, false))
                {
                    deflateStream.Write(compressedBytes, 0, compressedBytes.Length);
                }
                decompressed = ms2.ToArray();
            }

            TestContext.WriteLine("Decompressed");


            bool check = true;
            if (buffer.Length != decompressed.Length)
            {
                TestContext.WriteLine("Different lengths.");
                check = false;
            }
            else
            {
                for (int i=0; i < buffer.Length; i++)
                {
                    if (buffer[i] != decompressed[i])
                    {
                        TestContext.WriteLine("byte {0} differs", i);
                        check = false;
                        break;
                    }
                }
            }

            Assert.IsTrue(check,"Data check failed.");
        }
Ejemplo n.º 4
0
        public void Zlib_DisposedException_DeflateStream()
        {
            string TextToCompress = LetMeDoItNow;

                MemoryStream ms1= new MemoryStream();

                Stream compressor= new DeflateStream(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 DeflateStream(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);

        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        public void Zlib_DeflateStream_InMemory()
        {
            String TextToCompress = UntilHeExtends;

            CompressionLevel[] levels = {CompressionLevel.Level0,
                                         CompressionLevel.Level1,
                                         CompressionLevel.Default,
                                         CompressionLevel.Level7,
                                         CompressionLevel.BestCompression};

            // compress with various Alienlab.levels, and System.IO.Compression (default level)
            for (int k= 0; k < levels.Length + 1; k++)
            {
                MemoryStream ms= new MemoryStream();

                Stream compressor = null;
                if (k == levels.Length)

                    compressor = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress, false);
                else
                {
                    compressor = new Alienlab.Zlib.DeflateStream(ms, CompressionMode.Compress, levels[k], false);
                    TestContext.WriteLine("using level: {0}", levels[k].ToString());
                }

                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();

                var a = ms.ToArray();
                TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

                // de-compress with both Alienlab.and System.IO.Compression
                for (int j = 0; j < 2; j++)
                {
                    var slow = new MySlowMemoryStream(a); // want to force EOF
                    Stream decompressor = null;

                    switch (j)
                    {
                    case 0:
                        decompressor = new Alienlab.Zlib.DeflateStream(slow, CompressionMode.Decompress, false);
                        break;
                    case 1:
                        decompressor = new System.IO.Compression.DeflateStream(slow, System.IO.Compression.CompressionMode.Decompress, false);
                        break;
                    }

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

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

                    TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
                    TestContext.WriteLine("\n");
                    Assert.AreEqual<String>(TextToCompress, DecompressedText);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///   Uncompress a DEFLATE'd byte array into a byte array.
        /// </summary>
        ///
        /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
        /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
        /// <seealso cref="GZipStream.UncompressBuffer(byte[])">GZipStream.UncompressBuffer(byte[])</seealso>
        /// <seealso cref="ZlibStream.UncompressBuffer(byte[])">ZlibStream.UncompressBuffer(byte[])</seealso>
        ///
        /// <param name="compressed">
        ///   A buffer containing data that has been compressed with DEFLATE.
        /// </param>
        ///
        /// <returns>The data in uncompressed form</returns>
        public static byte[] UncompressBuffer(byte[] compressed)
        {
            using (var input = new System.IO.MemoryStream(compressed))
            {
                System.IO.Stream decompressor =
                    new DeflateStream( input, CompressionMode.Decompress );

                return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///   Compress a byte array into a new byte array using DEFLATE.
        /// </summary>
        ///
        /// <remarks>
        ///   Uncompress it with <see cref="DeflateStream.UncompressBuffer(byte[])"/>.
        /// </remarks>
        ///
        /// <seealso cref="DeflateStream.CompressString(string)">DeflateStream.CompressString(string)</seealso>
        /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
        /// <seealso cref="GZipStream.CompressBuffer(byte[])">GZipStream.CompressBuffer(byte[])</seealso>
        /// <seealso cref="ZlibStream.CompressBuffer(byte[])">ZlibStream.CompressBuffer(byte[])</seealso>
        ///
        /// <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 System.IO.MemoryStream())
            {
                System.IO.Stream compressor =
                    new DeflateStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression );

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