Ejemplo n.º 1
0
        public static CompressResult Decompress([NotNull] Stream compressedFile, [NotNull] Stream outputFile, int bufferSize = 0x100000, Action <CompressResult> blockCallback = null)
        {
            long position;

            if (compressedFile == null)
            {
                throw new ArgumentNullException(nameof(compressedFile));
            }
            if (outputFile == null)
            {
                throw new ArgumentNullException(nameof(outputFile));
            }
            byte[]         buffer = new byte[bufferSize];
            CompressResult result = new CompressResult();

            try
            {
                position = compressedFile.Position;
            }
            catch (Exception)
            {
                position     = -1L;
                result.Bytes = -1L;
            }
            using (Lz4DecompressionStream stream = new Lz4DecompressionStream(compressedFile))
            {
                int num2;
                while ((num2 = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputFile.Write(buffer, 0, num2);
                    result.CompressedBytes += num2;
                    if (position != -1L)
                    {
                        long num3 = compressedFile.Position;
                        result.Bytes += num3 - position;
                        position      = num3;
                    }
                    if (blockCallback != null)
                    {
                        blockCallback(result);
                    }
                }
            }
            return(result);
        }
Ejemplo 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);
            }
        }
Ejemplo 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));
            }
        }
Ejemplo n.º 4
0
 public static CompressResult Decompress([NotNull] Stream compressedFile, [NotNull] Stream outputFile, int bufferSize = 0x100000, Action<CompressResult> blockCallback = null)
 {
     long position;
     if (compressedFile == null)
     {
         throw new ArgumentNullException("compressedFile");
     }
     if (outputFile == null)
     {
         throw new ArgumentNullException("outputFile");
     }
     byte[] buffer = new byte[bufferSize];
     CompressResult result = new CompressResult();
     try
     {
         position = compressedFile.Position;
     }
     catch (Exception)
     {
         position = -1L;
         result.Bytes = -1L;
     }
     using (Lz4DecompressionStream stream = new Lz4DecompressionStream(compressedFile))
     {
         int num2;
         while ((num2 = stream.Read(buffer, 0, buffer.Length)) > 0)
         {
             outputFile.Write(buffer, 0, num2);
             result.CompressedBytes += num2;
             if (position != -1L)
             {
                 long num3 = compressedFile.Position;
                 result.Bytes += num3 - position;
                 position = num3;
             }
             if (blockCallback != null)
             {
                 blockCallback(result);
             }
         }
     }
     return result;
 }