Stream to compress and write back data
Inheritance: Stream
Exemplo n.º 1
0
 private static void Compress()
 {
     using (Lz4CompressionStream stream = new Lz4CompressionStream(Console.OpenStandardOutput(), 0x100000))
     {
         Console.OpenStandardInput().CopyTo(stream, 0x1000);
     }
 }
Exemplo n.º 2
0
        public void StreamCompression_FileStream_1Mb ()
        {   
            var filename = System.IO.Path.GetRandomFileName ();
            int currentSz = 0;
            int sz = 0;
            int maxSz = 1024 * 1024 * 10;
            List<uint> hashes = new List<uint> (1024);
            byte[] buffer;

            try
            {
                // create file
                using (var stream = new Lz4CompressionStream (new System.IO.FileStream (filename, System.IO.FileMode.Create), 1 << 18, Lz4Mode.HighCompression, true))
                {
                    while (currentSz < maxSz)
                    {
                        buffer = GetStringsAsByte (1024);
                        stream.Write (buffer, 0, buffer.Length);
                        currentSz += buffer.Length;
                        // save hash
                        hashes.Add (SimpleHash (buffer));
                    }
                }

                // read
                buffer = new byte[1024];
                int ix = 0;
                using (var stream = new Lz4DecompressionStream (new System.IO.FileStream (filename, System.IO.FileMode.Open), true))
                { 
                    while ((sz = stream.Read (buffer, 0, buffer.Length)) > 0)
                    {
                        // check hash
                        Assert.IsTrue (SimpleHash (buffer) == hashes[ix++], "Hash mismatch");
                    }
                }
            }
            finally
            {
                System.IO.File.Delete (filename);
            }
        }
Exemplo n.º 3
0
        public void StreamCompression_SimpleTest ()
        {
            var txt = sampleTxt + sampleTxt + sampleTxt + sampleTxt + sampleTxt + sampleTxt;
            
            var mem = new System.IO.MemoryStream ();
            using (var stream = new Lz4CompressionStream (mem, 1 << 18, Lz4Mode.HighCompression))
            {
                var b = System.Text.Encoding.UTF8.GetBytes (txt);
                stream.Write (b, 0, b.Length);
            }
            // reset stream position
            mem.Position = 0;

            using (var stream = new Lz4DecompressionStream (mem))
            {
                var b = new byte[1024];
                int sz = stream.Read (b, 0, b.Length);

                // validate
                Assert.IsTrue (txt == System.Text.Encoding.UTF8.GetString (b, 0, sz));
            }            
        }
Exemplo n.º 4
0
 public void TestPerfermence(string path)
 {
     int num2;
     FileStream stream;
     int num3;
     Lz4CompressionStream stream5;
     Lz4DecompressionStream stream6;
     long length = new FileInfo(path).Length;
     byte[] buffer = new byte[0x100000];
     Stopwatch stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 0x100000))
         {
             using (FileStream stream2 = new FileStream(path + num2 + ".tmp", FileMode.Create, FileAccess.Write, FileShare.None, 0x100000))
             {
                 while ((num3 = stream.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     stream2.Write(buffer, 0, num3);
                 }
             }
         }
     }
     long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     Console.WriteLine("binary write: {0}MB, eslaped {1}ms, avg {2:N1}MB/s",
         (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f);
     for (num2 = 0; num2 < 10; num2++)
     {
         File.Delete(path + num2 + ".tmp");
     }
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 0x100000))
         {
             using (GZipStream stream3
                 = new GZipStream(new FileStream(path + num2 + ".tmp", FileMode.Create, FileAccess.Write, FileShare.None, 0x100000),
                 CompressionMode.Compress))
             {
                 while ((num3 = stream.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     stream3.Write(buffer, 0, num3);
                 }
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     float num5 = new FileInfo(path).Length / ((float)length);
     Console.WriteLine("gzip write: {0}MB, eslaped {1}ms, avg {2:N1}MB/s, compress rate {3:P}",
         new object[] { (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f, num5 });
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (GZipStream stream4 = new GZipStream(
             new FileStream(path + num2 + ".tmp",
                 FileMode.Open, FileAccess.Read, FileShare.None, 0x100000), CompressionMode.Decompress))
         {
             while (stream4.Read(buffer, 0, buffer.Length) != 0)
             {
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     Console.WriteLine("gzip read: {0}MB, eslaped {1}ms, avg {2:N1}MB/s",
         (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f);
     for (num2 = 0; num2 < 10; num2++)
     {
         File.Delete(path + num2 + ".tmp");
     }
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 0x100000))
         {
             using (stream5
                 = new Lz4CompressionStream(new FileStream(path + num2 + ".tmp", FileMode.Create, FileAccess.Write, FileShare.None, 0x100000),
                     0x100000, Lz4Mode.Fast, true))
             {
                 while ((num3 = stream.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     stream5.Write(buffer, 0, num3);
                 }
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     num5 = new FileInfo(path).Length / ((float)length);
     Console.WriteLine("lz4 write: {0}MB, eslaped {1}ms, avg {2:N1}MB/s, compress rate {3:P}",
         new object[] { (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f, num5 });
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream6
             = new Lz4DecompressionStream(new FileStream(path + num2 + ".tmp", FileMode.Open, FileAccess.Read, FileShare.None, 0x100000), true))
         {
             while (stream6.Read(buffer, 0, buffer.Length) != 0)
             {
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     Console.WriteLine("lz4 read: {0}MB, eslaped {1}ms, avg {2:N1}MB/s",
         (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f);
     for (num2 = 0; num2 < 10; num2++)
     {
         File.Delete(path + num2 + ".tmp");
     }
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None, 0x100000))
         {
             using (stream5 = new Lz4CompressionStream(new FileStream(path + num2 + ".tmp",
                 FileMode.Create, FileAccess.Write, FileShare.None, 0x100000), 0x100000, Lz4Mode.HighCompression, true))
             {
                 while ((num3 = stream.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     stream5.Write(buffer, 0, num3);
                 }
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     num5 = new FileInfo(path).Length / ((float)length);
     Console.WriteLine("lz4hc write: {0}MB, eslaped {1}ms, avg {2:N1}MB/s, compress rate {3:P}",
         new object[] { (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f, num5 });
     stopwatch = Stopwatch.StartNew();
     for (num2 = 0; num2 < 10; num2++)
     {
         using (stream6 = new Lz4DecompressionStream(new FileStream(path + num2 + ".tmp", FileMode.Open, FileAccess.Read, FileShare.None, 0x100000), true))
         {
             while (stream6.Read(buffer, 0, buffer.Length) != 0)
             {
             }
         }
     }
     elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
     Console.WriteLine("lz4hc read: {0}MB, eslaped {1}ms, avg {2:N1}MB/s",
         (length / 0x400L) / 0x400L, elapsedMilliseconds, ((length * 10f) / elapsedMilliseconds) / 1024f);
     for (num2 = 0; num2 < 10; num2++)
     {
         File.Delete(path + num2 + ".tmp");
     }
 }