Пример #1
0
        static unsafe void Test2()
        {
            var cctx = ExternMethods.ZSTD_createCCtx();
            var dctx = ExternMethods.ZSTD_createDCtx();

            var src          = File.ReadAllBytes("dickens");
            var dest         = new byte[ExternMethods.ZSTD_compressBound((nuint)src.Length)];
            var uncompressed = new byte[src.Length];

            fixed(byte *dstPtr = dest)
            fixed(byte *srcPtr          = src)
            fixed(byte *uncompressedPtr = uncompressed)
            {
                var compressedLength = ExternMethods.ZSTD_compressCCtx(cctx, (IntPtr)dstPtr, (nuint)dest.Length, (IntPtr)srcPtr, (nuint)src.Length,
                                                                       level);

                var decompressedLength = ExternMethods.ZSTD_decompressDCtx(dctx, (IntPtr)uncompressedPtr, (nuint)uncompressed.Length, (IntPtr)dstPtr, compressedLength);

                Console.WriteLine($"{compressedLength} {decompressedLength} {src.Length}");
            }
            ExternMethods.ZSTD_freeCCtx(cctx);
            ExternMethods.ZSTD_freeDCtx(dctx);
        }
Пример #2
0
        public void CompressAndDecompressWithNative(int level)
        {
            var srcBuffer = File.ReadAllBytes("dickens");

            var compressBound = Compressor.GetCompressBound(srcBuffer.Length);

            Assert.Equal((int)ExternMethods.ZSTD_compressBound((nuint)srcBuffer.Length), compressBound);

            var compressor       = new Compressor(level);
            var compressedSharp  = compressor.Wrap(srcBuffer);
            var compressedNative = CompressNative(srcBuffer, compressBound, level);

            Assert.True(compressedNative.SequenceEqual(compressedSharp));

            var decompressBound = (int)Decompressor.GetDecompressedSize(compressedSharp);

            Assert.Equal(decompressBound, srcBuffer.Length);

            var decompressor       = new Decompressor();
            var decompressedSharp  = decompressor.Unwrap(compressedSharp);
            var decompressedNative = DecompressNative(compressedNative, decompressBound);

            Assert.True(decompressedSharp.SequenceEqual(decompressedNative));
        }