This static class is used to call the functions in lzfx.dll, which is coded in C and compiled for native Win32. LZFX is a fast-executing compression and decompression algorithm for byte arrays, although it does not achieve great compression ratios. The lzfx DLL is based on BSD-licensed source code from http://lzfx.googlecode.com.
Exemplo n.º 1
0
        /// <summary>
        /// This method returns an uncompressed version of the given compressed byte array
        /// </summary>
        /// <param name="inputBlob">the byte array to be decompressed</param>
        /// <param name="decompressedLength">the known length of the decompressed byte array</param>
        /// <param name="compressionCode">a generation number that indicates what compression technique to use</param>
        /// <returns>the decompressed byte array</returns>
        public static unsafe Byte[] DecompressBlob(Byte[] inputBlob, int decompressedLength,
                                                   int compressionCode)
        {
            if (compressionCode == 1)
            {
                // the byte array of the output blob
                Byte[] decompressedBlob = new Byte[decompressedLength];
                // Decompress using LZFX algorithm.
                // This method will throw an exception if the decompressed data does not
                // exactly fit into the allocated array size.
                LZFX.Decompress(inputBlob, decompressedBlob);

                return(decompressedBlob);
            }
            if (compressionCode == 2)
            {
                // the byte array of the output blob
                Byte[] decompressedBlob = new Byte[decompressedLength];
                // Decompress using LZFX algorithm.
                // This method will throw an exception if the decompressed data does not
                // exactly fit into the allocated array size.
                LZ4.Decompress(inputBlob, decompressedBlob);

                return(decompressedBlob);
            }
            else
            {   // return without doing any compression
                return(inputBlob);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This returns a compressed version of the given byte array
        /// </summary>
        /// <param name="uncompressedBlob">the uncompressed byte array</param>
        /// <param name="compressionCode">a generation number that indicates what compression technique to use</param>
        /// <returns>the compressed byte array</returns>
        public static unsafe Byte[] CompressBlob(Byte[] uncompressedBlob, int compressionCode)
        {
            if (compressionCode == 1)
            {
                Byte[] compressedBlob;
                // the byte-array length of the input blob
                int inputLength = uncompressedBlob.Length;
                // The byte array that will be created by the compression.
                // Note that some incompressible BLOBs will actually be made larger by LZFX
                // compression.  We have observed about 1% increase over the original BLOB,
                // so the factor of 1.05 is expected to be safe.  We add 16 since the factor
                // of 1.05 is insufficient when the BLOB is very small.
                compressedBlob = new Byte[(int)(inputLength * 1.05) + 16];

                // Compress using LZFX algorithm.
                // This method resizes the compressed byte array for us.
                LZFX.Compress(uncompressedBlob, ref compressedBlob);
                return(compressedBlob);
            }
            if (compressionCode == 2)
            {
                Byte[] compressedBlob;
                // the byte-array length of the input blob
                int inputLength = uncompressedBlob.Length;
                // The byte array that will be created by the compression.
                // Note that some incompressible BLOBs might actually be made larger by LZ4
                // compression.  The GetMaxSize method calls a function built into LZ4 which
                // returns the maximum possible size of the output array. LZ4 documentation suggests
                // that speed is optimized by allocating this max size.
                compressedBlob = new Byte[(int)LZ4.GetMaxSize(inputLength)];

                // Compress using LZ4 algorithm.
                // This method resizes the compressed byte array for us.
                LZ4.Compress(uncompressedBlob, ref compressedBlob, 4);
                return(compressedBlob);
            }
            else
            {   // return without doing any compression
                return(uncompressedBlob);
            }
        }