Example #1
0
        /// <summary>
        /// 字节数组压缩
        /// 返回:已压缩的字节数组
        /// </summary>
        /// <param name="data">待压缩的字节数组</param>
        /// <returns></returns>
        public static byte[] CompressBytes(byte[] data)
        {
            var f = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(ICSharpCode.SharpZipLib.Zip.Compression
                                                                         .Deflater.BEST_COMPRESSION);

            f.SetInput(data);
            f.Finish();

            var o = new MemoryStream(data.Length);

            try
            {
                var buf = new byte[1024];
                while (!f.IsFinished)
                {
                    var got = f.Deflate(buf);
                    o.Write(buf, 0, got);
                }
            }
            finally
            {
                o.Close();
            }

            return(o.ToArray());
        }
Example #2
0
        /// <summary>
        /// Compress the input data to an output stream. The first four bytes
        /// is a UInt32 (in LSB format) that contains the size of the *decompressed*
        /// data.
        /// </summary>
        /// <param name="inputData">An array of bytes to compress</param>
        /// <param name="nNumInputBytes">The number of input bytes from inputData to compress</param>
        /// <param name="outStream">The stream to write the compressed bytes to.</param>
        /// <returns>The length of the compressed data (not including the length value
        /// that is written to the beginning of the stream)</returns>
        public static int Compress(byte[] inputData, int nNumInputBytes, Stream outStream)
        {
            if (inputData.Length == 0 || nNumInputBytes == 0)
            {
                throw new InvalidDataException("ZLib.Compress: No input data to compress.");
            }

            long nMaxCompressedDataLen = nNumInputBytes + ((nNumInputBytes / 1000) + 1) + 12 + sizeof(int);

            byte[] outBuff = new byte[nMaxCompressedDataLen];
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater compresser =
                new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
            compresser.SetInput(inputData);
            compresser.Finish();

            int nCompressedDataLength = compresser.Deflate(outBuff);

            BinaryWriter writer = new BinaryWriter(outStream);

            writer.Write(nNumInputBytes);
            writer.Write(outBuff, 0, nCompressedDataLength);

            writer.Flush();

            return(nCompressedDataLength);
        }
Example #3
0
        public LegacyBBeBObject CreateLegacyTextObject()
        {
            m_PageBuffer.putTag(TagId.EndPage);

            byte[]      output = m_PageBuffer.GetBuffer();
            ObjectFlags flags  = 0;
            int         len    = (int)m_PageBuffer.Position;

            if (len > k_MinUncompressedLen)
            {
                // Allocate a buffer to compress into
                output = new byte[len];

                // Stash uncompressed size
                ByteBuffer.PackInt(output, 0, len);

                // Deflate text
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater compresser =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
                compresser.SetInput(m_PageBuffer.GetBuffer(), 0, len);
                compresser.Finish();
                len   = compresser.Deflate(output, 4, output.Length - 4) + 4;
                flags = ObjectFlags.COMPRESSED;
            }

            return(new LegacyBBeBObject(m_Book, ObjectType.Text, flags, output, len));
        }
Example #4
0
        public static byte[] LowLevelCompress(byte[] bytes, Shell.EndianFormat byteOrder)
        {
            Contract.Requires <ArgumentNullException>(bytes != null);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            byte[] result = new byte[sizeof(int)];
            // Setup the decompressed size header
            byte[] size_bytes = BitConverter.GetBytes(bytes.Length);
            if (!byteOrder.IsSameAsRuntime())
            {
                Bitwise.ByteSwap.SwapInt32(size_bytes, 0);
            }
            Array.Copy(size_bytes, result, size_bytes.Length);

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, false);

            {
                zip.SetInput(bytes);
                zip.Finish();
                byte[] temp            = new byte[bytes.Length];
                int    compressed_size = zip.Deflate(temp);

                Contract.Assert(compressed_size <= bytes.Length);
                Array.Resize(ref result, sizeof(int) + compressed_size);
                Array.Copy(temp, 0, result, sizeof(int), compressed_size);
            }
            return(result);
        }
Example #5
0
File: Utils.cs Project: biuken/eAnt
        private void CompresNowZip()
        {
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
                new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                    ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
            Stream aux = new MemoryStream();

            byte[] data = new byte[this.m_streaminput.Length];           //minimo el tamaño sin comprimir.
            int    size = 0;

            try
            {
                def.SetInput(this.m_byteinput);
                def.Finish();
                size = def.Deflate(data);
                if (size == 0)
                {
                    while (def.IsFinished == false)
                    {
                        if (def.IsNeedingInput)
                        {
                            Exception e = new Exception("Tamaño muy pequeño para comprimir");
                            break;
                        }
                        else
                        {
                            size = def.Deflate(data);
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                def.Flush();
            }
            catch (ZipException e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            finally
            {
                this.m_byteoutput   = null;
                this.m_byteoutput   = new byte[size];
                this.m_streamoutput = new MemoryStream(size);
                this.m_streamoutput.Write(data, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
                this.m_streamoutput.Read(this.m_byteoutput, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
            }
        }
Example #6
0
 /// <summary>
 /// deflater压缩
 /// </summary>
 /// <param name="inputByte"></param>
 /// <returns></returns>
 public static byte[] deflater(byte[] inputByte)
 {
     byte[] temp = new byte[1024];
     MemoryStream memory = new MemoryStream();
     ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
     def.SetInput(inputByte);
     def.Finish();
     while (!def.IsFinished)
     {
         int extracted = def.Deflate(temp);
         if (extracted > 0)
         {
             memory.Write(temp, 0, extracted);
         }
         else
         {
             break;
         }
     }
     return memory.ToArray();
 }
Example #7
0
        /// <summary>
        /// deflater压缩
        /// </summary>
        /// <param name="inputByte"></param>
        /// <returns></returns>
        public static byte[] deflater(byte[] inputByte)
        {
            byte[]       temp   = new byte[1024];
            MemoryStream memory = new MemoryStream();

            ICSharpCode.SharpZipLib.Zip.Compression.Deflater def = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater();
            def.SetInput(inputByte);
            def.Finish();
            while (!def.IsFinished)
            {
                int extracted = def.Deflate(temp);
                if (extracted > 0)
                {
                    memory.Write(temp, 0, extracted);
                }
                else
                {
                    break;
                }
            }
            return(memory.ToArray());
        }
Example #8
0
        public static byte[] LowLevelCompress(byte[] bytes, int level,
                                              out uint adler, byte[] compressedBytes,
                                              bool trimCompressedBytes = true, bool noZlibHeaderOrFooter = true)
        {
            int compressed_size;

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(level, noZlibHeaderOrFooter);

            {
                zip.SetInput(bytes);
                zip.Finish();
                compressed_size = zip.Deflate(compressedBytes);
                adler           = (uint)zip.Adler;

                if (trimCompressedBytes)
                {
                    byte[] cmp_data = compressedBytes;
                    Array.Resize(ref cmp_data, compressed_size);
                    compressedBytes = cmp_data;
                }
            }

            return(compressedBytes);
        }
Example #9
0
 private void CompresNowZip()
 {
     ICSharpCode.SharpZipLib.Zip.Compression.Deflater def =
             new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
             ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, true);
     Stream aux = new MemoryStream();
     byte[] data = new byte[this.m_streaminput.Length]; //minimo el tamaño sin comprimir.
     int size = 0;
     try
     {
         def.SetInput(this.m_byteinput);
         def.Finish();
         size = def.Deflate(data);
         if (size == 0)
             while (def.IsFinished == false)
             {
                 if (def.IsNeedingInput)
                 {
                     Exception e = new Exception("Tamaño muy pequeño para comprimir");
                     break;
                 }
                 else
                 {
                     size = def.Deflate(data);
                     System.Threading.Thread.Sleep(2000);
                 }
             }
         def.Flush();
     }
     catch (ZipException e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     catch (Exception e)
     {
         this.m_byteoutput = null;
         this.m_streamoutput = null;
         Debug.WriteLine(e.Message);
     }
     finally
     {
         this.m_byteoutput = null;
         this.m_byteoutput = new byte[size];
         this.m_streamoutput = new MemoryStream(size);
         this.m_streamoutput.Write(data, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
         this.m_streamoutput.Read(this.m_byteoutput, 0, size);
         this.m_streamoutput.Seek(0, SeekOrigin.Begin);
     }
 }