示例#1
0
        public static byte[] Uncompress(byte[] src)
        {
            MemoryStream memoryStream = new MemoryStream();
            Stream       stream       = new ZOutputStream(memoryStream);

            stream.Write(src, 0, src.Length);
            stream.Close();
            return(memoryStream.ToArray());
        }
示例#2
0
    public static byte[] Uncompress(byte[] src)
    {
        MemoryStream md = new MemoryStream();
        Stream       d  = new ZOutputStream(md);

        d.Write(src, 0, src.Length);
        d.Close();
        return(md.ToArray());
    }
示例#3
0
 public byte[] DecompressData(byte[] inData)
 {
     using (MemoryStream outMemoryStream = new MemoryStream()) {
         using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream)){
             outZStream.Write(inData, 0, inData.Length);
             return(outMemoryStream.ToArray());
         }
     }
 }
示例#4
0
        public static byte[] Compress(byte[] src, int offset, int length)
        {
            MemoryStream ms = new MemoryStream();
            Stream       s  = new ZOutputStream(ms, 9);

            s.Write(src, offset, length);
            s.Close();
            return(ms.ToArray());
        }
示例#5
0
 public static byte[] Decompress(Byte[] inputBytes)
 {
     using (MemoryStream ms = new MemoryStream()) {
         using (ZOutputStream zOut = new ZOutputStream(ms)) {
             zOut.Write(inputBytes, 0, inputBytes.Length);
             zOut.finish();
             return(ms.ToArray());
         }
     }
 }
示例#6
0
        private byte[] UnCompress(byte[] Packet)
        {
            InStream.SetLength(0);
            InCompress.Write(Packet, 0, Packet.Length);
            InStream.Position = 0;

            byte[] Result = new byte[InStream.Length];
            InStream.Read(Result, 0, Result.Length);
            return(Result);
        }
示例#7
0
        /// <summary>
        /// 拷贝流
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        private static void CopyStream(MemoryStream input, ZOutputStream output)
        {
            byte[] buffer = new byte[2000];
            int    len;

            while ((len = input.Read(buffer, 0, 2000)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            output.Flush();
        }
示例#8
0
        /// <summary>
        /// Compresses the given <paramref name="input" />
        /// </summary>
        /// <param name="input">The data to compress</param>
        /// <returns></returns>
        public static byte[] Compress(byte[] input)
        {
            var sourceStream = new MemoryStream();
            var stream       = new ZOutputStream(sourceStream, 1);

            // write zlib header
            stream.Write(input);
            stream.Finish();

            return(sourceStream.GetBuffer());
        }
 public override void Add(byte[] data, int offset = 0, int length = -1)
 {
     length = length < 0 ? data.Length : length;
     try
     {
         if (_IsFirst)
         {
             _IsFirst = false;
             _ZOutStream.Write(data, 24, length - 24);
         }
         else
         {
             _ZOutStream.Write(data, 0, length);
         }
     }
     catch (IOException e)
     {
         throw new ApplicationException("Backup to file: Writing local file failed!", e);
     }
 }
示例#10
0
文件: Ruby.cs 项目: terabin/Gemini
 /// <summary>
 /// Deflate a string using zlib.net library since IronRuby's Deflate module is buggy, f**k
 /// </summary>
 /// <param name="data">The string to compress</param>
 /// <returns>The decompressed string as a Ruby string</returns>
 public static MutableString ZlibDeflate(string data)
 {
     using (MemoryStream outMemoryStream = new MemoryStream())
         using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_BEST_COMPRESSION))
         {
             byte[] bytes = Encoding.UTF8.GetBytes(data);
             outZStream.Write(bytes, 0, bytes.Length);
             outZStream.finish();
             return(MutableString.CreateBinary(outMemoryStream.ToArray()));
         }
 }
        private void StoreLayerDataBase64(List <int> tileIndices, XmlWriter xmlWriter, string dataCompression)
        {
            byte[] tileBytes = ConvertTileIndicesToBytes(tileIndices);

            if (dataCompression == "none")
            {
                string base64Data = Convert.ToBase64String(tileBytes);
                xmlWriter.WriteString(base64Data);
            }
            else if (dataCompression == "gzip")
            {
                MemoryStream inMemoryStream  = new MemoryStream(tileBytes);
                MemoryStream outMemoryStream = new MemoryStream();
                GZipStream   outGZipStream   = new GZipStream(outMemoryStream, CompressionMode.Compress);

                byte[] buffer = new byte[1024];
                while (true)
                {
                    int bytesRead = inMemoryStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    outGZipStream.Write(buffer, 0, bytesRead);
                }
                outGZipStream.Close();

                byte[] compressedBytes = outMemoryStream.ToArray();
                string base64Data      = Convert.ToBase64String(compressedBytes);
                xmlWriter.WriteString(base64Data);
            }
            else if (dataCompression == "zlib")
            {
                MemoryStream  inMemoryStream  = new MemoryStream(tileBytes);
                MemoryStream  outMemoryStream = new MemoryStream();
                ZOutputStream zOutputStream   = new ZOutputStream(outMemoryStream, 9);

                byte[] buffer = new byte[1024];
                while (true)
                {
                    int bytesRead = inMemoryStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    zOutputStream.Write(buffer, 0, bytesRead);
                }
                zOutputStream.Close();

                byte[] compressedBytes = outMemoryStream.ToArray();
                string base64Data      = Convert.ToBase64String(compressedBytes);
                xmlWriter.WriteString(base64Data);
            }
        }
        /// <summary>
        /// Compresses the data array.
        /// </summary>
        /// <param name="data">The data to compress.</param>
        /// <returns>The compressed data.</returns>
        private static byte[] CompressData(byte[] data)
        {
            using (var compressedStream = new MemoryStream())
            {
                using (var outputStream = new ZOutputStream(compressedStream, zlibConst.Z_DEFAULT_COMPRESSION))
                {
                    outputStream.Write(data, 0, data.Length);
                }

                return(compressedStream.ToArray());
            }
        }
        public void CopyStream(Stream input, ZOutputStream output)
        {
            // Read compressed data and send it to the decompressor
            var buffer = new byte[2000];
            int len;

            while ((len = input.Read(buffer, 0, 2000)) > 0)
            {
                output.Write(buffer, 0, len);
            }
            output.Flush();
        }
示例#14
0
 private void deflateEntries(out Dictionary <Entry, byte[]> entryDeflatedData, out List <uint> zLengths, uint blockSize)
 {
     entryDeflatedData = new Dictionary <Entry, byte[]>();
     zLengths          = new List <uint>();
     foreach (Entry current in this.Entries)
     {
         current.zIndex = (uint)zLengths.Count;
         current.Data.Seek(0, SeekOrigin.Begin);
         Stream data = current.Data;
         List <Tuple <byte[], int> > list = new List <Tuple <byte[], int> >();
         while (data.Position < data.Length)
         {
             byte[]        array         = new byte[2 * blockSize];
             MemoryStream  memoryStream  = new MemoryStream(array);
             byte[]        array2        = new byte[blockSize];
             int           num           = data.Read(array2, 0, array2.Length);
             ZOutputStream zOutputStream = new ZOutputStream(memoryStream, 9);
             zOutputStream.Write(array2, 0, num);
             zOutputStream.Flush();
             zOutputStream.finish();
             int num2 = (int)zOutputStream.TotalOut;
             if (num2 > num)
             {
                 list.Add(new Tuple <byte[], int>(array2, num));
             }
             else
             {
                 if (num2 < (blockSize - 1))
                 {
                     list.Add(new Tuple <byte[], int>(array, num2));
                 }
                 else
                 {
                     list.Add(new Tuple <byte[], int>(array2, num));
                 }
             }
         }
         int num3 = 0;
         foreach (Tuple <byte[], int> current2 in list)
         {
             num3 += current2.Item2;
             zLengths.Add((uint)current2.Item2);
         }
         byte[]       array3        = new byte[num3];
         MemoryStream memoryStream2 = new MemoryStream(array3);
         foreach (Tuple <byte[], int> current2 in list)
         {
             memoryStream2.Write(current2.Item1, 0, current2.Item2);
         }
         entryDeflatedData.Add(current, array3);
     }
 }
示例#15
0
        private byte[] Compress(byte[] data)
        {
            MemoryStream  compressed_out      = new MemoryStream();
            ZOutputStream z_compressor_stream = new ZOutputStream(compressed_out, zlibConst.Z_DEFAULT_COMPRESSION);

            z_compressor_stream.Write(data, 0, data.Length);
            z_compressor_stream.Close();
            byte[] result = compressed_out.ToArray();

            compressed_out.Close();

            return(result);
        }
示例#16
0
        private byte[] Decompress(byte[] data)
        {
            MemoryStream  decompressed_out      = new MemoryStream();
            ZOutputStream z_decompressor_stream = new ZOutputStream(decompressed_out);

            z_decompressor_stream.Write(data, 0, data.Length);
            z_decompressor_stream.Close();
            byte[] result = decompressed_out.ToArray();

            decompressed_out.Close();

            return(result);
        }
示例#17
0
        public void Uncompress()
        {
            MemoryStream memoryStream = new MemoryStream();

            using (ZOutputStream zOutputStream = new ZOutputStream(memoryStream))
            {
                zOutputStream.Write(buffer, 0, buffer.Length);
                zOutputStream.Flush();
            }
            buffer     = memoryStream.ToArray();
            position   = 0;
            compressed = false;
        }
示例#18
0
        public void CompressStreamToChunk(MemoryStream aInput)
        {
            using (MemoryStream memoryStream = new MemoryStream()) {
                ZOutputStream zlibStream = new ZOutputStream(memoryStream, (int)CompressionMode);

                zlibStream.Write(aInput.ToArray(), 0, (int)aInput.Length);
                zlibStream.finish();
                zlibStream.end();

                Write((int)memoryStream.Length);
                memoryStream.WriteTo(BaseStream);
            }
        }
示例#19
0
        public void Uncompress()
        {
            MemoryStream stream = new MemoryStream();

            using (ZOutputStream stream2 = new ZOutputStream(stream))
            {
                stream2.Write(buffer, 0, buffer.Length);
                stream2.Flush();
            }
            buffer     = stream.ToArray();
            position   = 0;
            compressed = false;
        }
示例#20
0
        public void WritePacket(Stream s)
        {
            VarInt viPacketID = new VarInt(PacketId);

            if (Compression == false)
            {
                PacketLength = DataLength + viPacketID.Length;
                VarInt viPacketLength = new VarInt(PacketLength);

                s.Write(viPacketID.VarIntData, 0, viPacketID.Length);
                s.Write(viPacketLength.VarIntData, 0, viPacketLength.Length);
                s.Write(Data, 0, DataLength);
            }
            else
            {
                MemoryStream  ms = new MemoryStream();
                ZOutputStream zo = new ZOutputStream(ms);


                zo.Write(viPacketID.VarIntData, 0, viPacketID.Length);
                zo.Write(Data, 0, Data.Length);

                DataLength = Data.Length;

                int slen = (int)zo.Position;
                ms.Position = 0;
                zo.Close();

                VarInt viDataLength = new VarInt(DataLength);
                PacketLength = slen + viDataLength.Length;

                VarInt viPacketLength = new VarInt(PacketLength);

                s.Write(viPacketLength.VarIntData, 0, viPacketLength.Length);
                s.Write(viDataLength.VarIntData, 0, viDataLength.Length);
                ms.CopyTo(s);
                ms.Close();
            }
        }
示例#21
0
        /// <summary>
        /// zlib 压缩算法
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] bytes)
        {
            using (var ms = new MemoryStream())
            {
                using (ZOutputStream outZStream = new ZOutputStream(ms, zlibConst.Z_BEST_SPEED))
                {
                    outZStream.Write(bytes, 0, bytes.Length);
                    outZStream.Flush();
                }

                return(ms.ToArray());
            }
        }
示例#22
0
        private static void WriteBlueprintFileFromJson(string blueprintFile, string json)
        {
            var buffer = new MemoryStream();

            using (var output = new ZOutputStream(buffer, 9))
            {
                output.Write(Encoding.UTF8.GetBytes(json));
            }

            using var outputWriter = new StreamWriter(blueprintFile);
            outputWriter.Write('0');
            outputWriter.Write(Convert.ToBase64String(buffer.ToArray()));
            outputWriter.Flush();
        }
示例#23
0
        public byte[] Encompress(byte[] data)
        {
            MemoryStream outputStream = new MemoryStream();
            //压缩
            ZOutputStream zStream = new ZOutputStream(outputStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            zStream.Write(data, 0, data.Length);
            zStream.finish();
            byte[] result = outputStream.ToArray();
            zStream.Close();
            outputStream.Close();
            outputStream.Dispose();
            return(result);
        }
示例#24
0
        public byte[] Decompress(byte[] data)
        {
            MemoryStream outStream = new MemoryStream();
            //解压
            ZOutputStream zStream = new ZOutputStream(outStream);

            zStream.Write(data, 0, data.Length);
            outStream.Flush();
            byte[] result = outStream.ToArray();
            zStream.Close();
            outStream.Close();
            outStream.Dispose();
            return(result);
        }
示例#25
0
        /// <summary>
        /// decompresses a gzipped OSD object
        /// </summary>
        /// <param name="meshBytes"></param>
        /// <returns></returns>
        static OSD DecompressOsd (byte [] meshBytes)
        {
            OSD decodedOsd = null;

            using (MemoryStream outMs = new MemoryStream ()) {
                using (ZOutputStream zOut = new ZOutputStream (outMs)) {
                    using (Stream inMs = new MemoryStream (meshBytes)) {
                        byte [] readBuffer = new byte [meshBytes.Length];
                        int readLen;

                        while ((readLen = inMs.Read (readBuffer, 0, readBuffer.Length)) > 0) {
                            zOut.Write (readBuffer, 0, readLen);
                        }
                        zOut.Flush ();
                        zOut.finish ();

                        byte [] decompressedBuf = outMs.GetBuffer (); //  ToArray();

                        decodedOsd = OSDParser.DeserializeLLSDBinary (decompressedBuf);
                    }
                }
            }

            return decodedOsd;
        }
示例#26
0
        /// <summary>
        /// Decodes mesh asset. See <see cref="OpenMetaverse.Rendering.FacetedMesh.TryDecodeFromAsset"/>
        /// to furter decode it for rendering</summary>
        /// <returns>true</returns>
        public override bool Decode()
        {
            try
            {
                MeshData = new OSDMap();

                using (MemoryStream data = new MemoryStream(AssetData))
                {
                    OSDMap header = (OSDMap)OSDParser.DeserializeLLSDBinary(data);
                    long start = data.Position;

                    foreach(string partName in header.Keys)
                    {
                        if (header[partName].Type != OSDType.Map)
                        {
                            MeshData[partName] = header[partName];
                            continue;
                        }

                        OSDMap partInfo = (OSDMap)header[partName];
                        if (partInfo["offset"] < 0 || partInfo["size"] == 0)
                        {
                            MeshData[partName] = partInfo;
                            continue;
                        }

                        byte[] part = new byte[partInfo["size"]];
                        Buffer.BlockCopy(AssetData, partInfo["offset"] + (int)start, part, 0, part.Length);

                        using (MemoryStream input = new MemoryStream(part))
                        {
                            using (MemoryStream output = new MemoryStream())
                            {
                                using (ZOutputStream zout = new ZOutputStream(output))
                                {
                                    byte[] buffer = new byte[2048];
                                    int len;
                                    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        zout.Write(buffer, 0, len);
                                    }
                                    zout.Flush();
                                    output.Seek(0, SeekOrigin.Begin);
                                    MeshData[partName] = OSDParser.DeserializeLLSDBinary(output);
                                }
                            }
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log("Failed to decode mesh asset", Helpers.LogLevel.Error, ex);
                return false;
            }
        }
 public void Deserialize(OSDMap map)
 {
     try
     {
         using (MemoryStream input = new MemoryStream(map["Zipped"].AsBinary()))
         {
             using (MemoryStream output = new MemoryStream())
             {
                 using (ZOutputStream zout = new ZOutputStream(output))
                 {
                     byte[] buffer = new byte[2048];
                     int len;
                     while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         zout.Write(buffer, 0, len);
                     }
                     zout.Flush();
                     output.Seek(0, SeekOrigin.Begin);
                     MaterialData = OSDParser.DeserializeLLSDBinary(output);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Failed to decode RenderMaterials message:", Helpers.LogLevel.Warning, ex);
         MaterialData = new OSDMap();
     }
 }