Beispiel #1
0
 public static byte[] decompress(byte[] data)
 {
     try
     {
         MemoryStream        ms = new MemoryStream(data);
         InflaterInputStream gz = new InflaterInputStream(ms);
         //DeflateStream gz = new DeflateStream(ms, CompressionMode.Decompress, true);
         //GZipStream gz = new GZipStream(ms, CompressionMode.Decompress, true);
         MemoryStream msreader = new MemoryStream();
         int          count    = 0;
         byte[]       buffer   = new byte[0x1000];
         while ((count = gz.Read(buffer, 0, buffer.Length)) != 0)
         {
             msreader.Write(buffer, 0, count);
         }
         gz.Close();
         ms.Close();
         msreader.Position = 0;
         byte[] depress = msreader.ToArray();
         msreader.Close();
         return(depress);
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
Beispiel #2
0
        public static byte[] Decompress(byte[] bytInput)
        {
            MemoryStream ms          = new MemoryStream();
            int          totalLength = 0;

            byte[] writeData = new byte[4096];
            Stream s2        = new InflaterInputStream(new MemoryStream(bytInput));

            try
            {
                while (true)
                {
                    int size = s2.Read(writeData, 0, writeData.Length);
                    if (size > 0)
                    {
                        ms.Write(writeData, 0, size);
                        totalLength += size;
                        //strResult += System.Text.Encoding.ASCII.GetString(writeData, 0, size);
                    }
                    else
                    {
                        break;
                    }
                }
                s2.Close();
                return((byte[])ms.ToArray());
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="compressedString"></param>
        /// <returns></returns>
        public string DeCompress(string compressedString, Enum_Encoding eEncoding)
        {
            string sEncoding = EncodingIn(eEncoding);

            string uncompressedString = "";
            int    totalLength        = 0;

            byte[] bytInput = System.Convert.FromBase64String(compressedString);

            byte[] writeData = new byte[bytInput.Length];
            Stream s2        = new InflaterInputStream(new MemoryStream(bytInput));

            while (true)
            {
                int size = s2.Read(writeData, 0, writeData.Length);
                if (size > 0)
                {
                    totalLength        += size;
                    uncompressedString += Encoding.
                                          GetEncoding(sEncoding).GetString(writeData, 0, size);
                }
                else
                {
                    break;
                }
            }
            s2.Close();
            return(uncompressedString);
        }
Beispiel #4
0
        protected void addDecompressedProgramChecksum(
            FileStream pFileStream,
            ref Crc32 pChecksum,
            ref CryptoStream pMd5CryptoStream,
            ref CryptoStream pSha1CryptoStream)
        {
            if (this.compressedProgramLength > 0)
            {
                InflaterInputStream inflater;
                int    read;
                byte[] data = new byte[READ_CHUNK_SIZE];

                pFileStream.Seek((long)(RESERVED_SECTION_OFFSET + this.reservedSectionLength), SeekOrigin.Begin);
                inflater = new InflaterInputStream(pFileStream);

                while ((read = inflater.Read(data, 0, READ_CHUNK_SIZE)) > 0)
                {
                    pChecksum.Update(data, 0, read);
                    pMd5CryptoStream.Write(data, 0, read);
                    pSha1CryptoStream.Write(data, 0, read);
                }

                inflater.Close();
                inflater.Dispose();
            }
        }
Beispiel #5
0
        public static byte[] FlateDecode(byte[] inb, bool strict)
        {
            MemoryStream        stream = new MemoryStream(inb);
            InflaterInputStream zip    = new InflaterInputStream(stream);
            MemoryStream        ostr   = new MemoryStream();

            byte[] b = new byte[strict ? 4092 : 1];
            try {
                int n;
                while ((n = zip.Read(b, 0, b.Length)) > 0)
                {
                    ostr.Write(b, 0, n);
                }
                zip.Close();
                ostr.Close();
                return(ostr.ToArray());
            }
            catch (Exception e) {
                e.GetType();
                if (strict)
                {
                    return(null);
                }
                return(ostr.ToArray());
            }
        }
Beispiel #6
0
        public bool uncompress()
        {
            bool result;

            try
            {
                InflaterInputStream inflaterInputStream = new InflaterInputStream(new MemoryStream(this.m_data));
                DynamicArray <byte> dynamicArray        = new DynamicArray <byte>();
                byte[] array = new byte[2048];
                while (true)
                {
                    int  num  = inflaterInputStream.Read(array, 0, array.Length);
                    bool flag = num > 0;
                    if (!flag)
                    {
                        break;
                    }
                    dynamicArray.pushBack(array, num);
                }
                inflaterInputStream.Close();
                base.clear();
                this.m_data    = dynamicArray.data;
                this.m_size    = dynamicArray.length;
                this.m_capcity = dynamicArray.capcity;
            }
            catch (Exception)
            {
                DebugTrace.print("Failed to uncompress ByteArray");
                result = false;
                return(result);
            }
            result = true;
            return(result);
        }
Beispiel #7
0
        /// <summary>
        ///   对ins解压,输出到outs
        /// </summary>
        /// <param name="inStream"></param>
        /// <param name="outStream"></param>
        /// <returns></returns>
        public static void Decompress(Stream inStream, Stream outStream)
        {
            var iis = new InflaterInputStream(inStream);

            StdioUtil.CopyStream(iis, outStream);
            iis.Close();
        }
Beispiel #8
0
        /// <summary>
        /// Decompresses an array of bytes.
        /// </summary>
        /// <param name="_pBytes">An array of bytes to be decompressed.</param>
        /// <returns>Decompressed bytes</returns>
        public static byte[] DeCompress(byte[] _pBytes)
        {
            InflaterInputStream inputStream = new InflaterInputStream(new MemoryStream(_pBytes));

            MemoryStream ms = new MemoryStream();
            Int32        mSize;

            byte[] mWriteData = new byte[4096];

            while (true)
            {
                mSize = inputStream.Read(mWriteData, 0, mWriteData.Length);
                if (mSize > 0)
                {
                    ms.Write(mWriteData, 0, mSize);
                }
                else
                {
                    break;
                }
            }

            inputStream.Close();
            return(ms.ToArray());
        }
Beispiel #9
0
 public static void Decompress(Stream stream, byte[] input)
 {
     using (var zlib = new InflaterInputStream(stream)) {
         zlib.IsStreamOwner = false;
         zlib.Write(input, 0, input.Length);
         zlib.Flush();
         zlib.Close();
     }
 }
        private byte[] DecompressBinary(byte[] input)
        {
            var inputBuffer  = new MemoryStream(input, 0, input.Length);
            var decompressor = new InflaterInputStream(inputBuffer);
            var outputBuffer = new MemoryStream();

            StreamUtils.PumpStream(decompressor, outputBuffer);
            decompressor.Close();
            outputBuffer.Close();
            return(outputBuffer.ToArray());
        }
Beispiel #11
0
 public static byte[] Decompress(byte[] input)
 {
     using (var mem = new MemoryStream()) {
         using (var zlib = new InflaterInputStream(mem)) {
             zlib.Write(input, 0, input.Length);
             zlib.Flush();
             zlib.Close();
             return(mem.ToArray());
         }
     }
 }
Beispiel #12
0
 public static byte[] Decompress(string data)
 {
     byte[] output = null;
     if (data.StartsWith("ZipStream:"))
     {
         var m      = new MemoryStream(Convert.FromBase64String(data.Substring(10)));
         var z      = new InflaterInputStream(m);
         var br     = new BinaryReader(m);
         var length = br.ReadInt32();
         output = new byte[length];
         z.Read(output, 0, length);
         z.Close();
         m.Close();
     }
     return(output);
 }
Beispiel #13
0
        public virtual void ZipDecompress(Stream compressed, Stream decompressed)
        {
            compressed.Seek(0, SeekOrigin.Begin);

            InflaterInputStream zipStream =
                new InflaterInputStream(compressed);

            zipStream.IsStreamOwner = false;
            int b = zipStream.ReadByte();

            while (b != -1)
            {
                decompressed.WriteByte((byte)b);
                b = zipStream.ReadByte();
            }

            decompressed.Flush();
            zipStream.Close();
        }
Beispiel #14
0
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="inputdata"></param>
        /// <param name="ds"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool Decompress(string inputdata, ref DataSet ds, ref string error)
        {
            try
            {
                //inputdata = inputdata.Replace("-", "+").Replace("_", "/").Replace(".", "=");
                byte[]       bb   = Convert.FromBase64String(inputdata);
                MemoryStream data = new MemoryStream(bb);

                //int s1 = (int)data.Length;

                using (MemoryStream m = new MemoryStream())
                {
                    using (InflaterInputStream mem = new InflaterInputStream(data))
                    {
                        byte[] buffer = new byte[4096];
                        while (true)
                        {
                            int size = mem.Read(buffer, 0, buffer.Length);
                            m.Write(buffer, 0, size);
                            if (size == 0)
                            {
                                break;
                            }
                        }
                        mem.Close();
                        mem.Dispose();
                    }

                    m.Seek(0, SeekOrigin.Begin);
                    BinaryFormatter bf = new BinaryFormatter();
                    ds = bf.Deserialize(m) as DataSet;
                    m.Close();
                    m.Dispose();
                }
                return(true);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
        }
Beispiel #15
0
        public byte[] Decompress(byte[] bytInput)
        {
            var ms = new MemoryStream(bytInput, 0, bytInput.Length);

            byte[] bytResult = null;
            string strResult = String.Empty;
            var    writeData = new byte[4096];
            Stream s2        = new InflaterInputStream(ms);

            try
            {
                bytResult = ReadFullStream(s2);
                s2.Close();
                return(bytResult);
            }
            catch
            {
                throw;
            }
        }
Beispiel #16
0
        public static MemoryStream ProcessData(byte[] data)
        {
            if (data == null)
            {
                return(null);
            }
            MemoryStream        baseInputStream = null;
            MemoryStream        stream2;
            InflaterInputStream stream3 = null;

            try
            {
                baseInputStream = new MemoryStream(data);
                stream2         = new MemoryStream();
                stream3         = new InflaterInputStream(baseInputStream);
                byte[] buffer = new byte[data.Length];
                while (true)
                {
                    int count = stream3.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        break;
                    }
                    stream2.Write(buffer, 0, count);
                }
                stream2.Flush();
                stream2.Seek(0L, SeekOrigin.Begin);
            }
            finally
            {
                if (baseInputStream != null)
                {
                    baseInputStream.Close();
                }
                if (stream3 != null)
                {
                    stream3.Close();
                }
            }
            return(stream2);
        }
Beispiel #17
0
        public List<String> TD_Request_NonAsyncChart_Snapshot(string _streamStockSymbol, string chartSource, int nDays)
        {

            List<String> cSortedLines = new List<string>();

            try
            {
                string _streamerCommand = "S=" + chartSource.ToUpper() + "&C=GET&P=" + _streamStockSymbol.ToUpper() + ",0,610," + nDays.ToString() + "d,1m";

                if (this.TD_loginStatus == true)
                {

                    XMLHTTP xmlHttp_ = new XMLHTTP();
                    StringBuilder cpostdata = new StringBuilder();
                    string lcPostUrl = string.Empty;

                    cpostdata.Append("!U=" + _accountid);
                    cpostdata.Append("&W=" + _token);
                    cpostdata.Append("&A=userid=" + _accountid);
                    cpostdata.Append("&token=" + _token);
                    cpostdata.Append("&company=" + _company);
                    cpostdata.Append("&segment=" + _segment);
                    cpostdata.Append("&cddomain=" + _cdDomain);
                    cpostdata.Append("&usergroup=" + _usergroup);
                    cpostdata.Append("&accesslevel=" + _accesslevel);
                    cpostdata.Append("&authorized=" + _authorized);
                    cpostdata.Append("&acl=" + _acl);
                    cpostdata.Append("&timestamp=" + _timestamp);
                    cpostdata.Append("&appid=" + _appid);
                    cpostdata.Append("|" + _streamerCommand);

                    string encodedString = Encode_URL(cpostdata.ToString());
                    cpostdata = new StringBuilder();
                    cpostdata.Append(encodedString);

                    lcPostUrl = "http://" + this._streamerurl + "/" + cpostdata;


                    /*/
                     *   Read the response and decompress the chart data
                     * 
                    /*/


                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(lcPostUrl);
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Accept = "Accept image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
                    req.Method = "GET";
                    req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                    req.Timeout = 60000;
                    req.ServicePoint.ConnectionLimit = 50;



                    //req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                    Stream respStream = resp.GetResponseStream();


                    byte[] chunk = new byte[65535 * 10];
                    byte[] ChartByteArray = new byte[65535 * 10];

                    int bytesRead = respStream.Read(ChartByteArray, 0, ChartByteArray.Length);
                    string compressedText = Convert.ToBase64String(ChartByteArray);
                    byte[] gzBuffer = Convert.FromBase64String(compressedText);

                    respStream.Flush();
                    resp.Close();
                    respStream.Close();


                    MemoryStream ms = new MemoryStream();


                    int nFieldNDX = 0;
                    int nStartPos = 21;
                    int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                    ms.Write(gzBuffer, 64, gzBuffer.Length - 64);


                    byte[] nMsg = new byte[sizeof(Int32)];


                    /*/
                     * S = Streaming
                     * N = Snapshot
                    /*/

                    nMsg[0] = gzBuffer[nFieldNDX++];
                    string cRequestType = System.Text.Encoding.ASCII.GetString(nMsg, 0, 1);

                    // Skip these next 4 bytes
                    nFieldNDX = nFieldNDX + 4;


                    // Get message length
                    nMsg = new byte[sizeof(Int32)];
                    nMsg[0] = gzBuffer[nFieldNDX++];
                    nMsg[1] = gzBuffer[nFieldNDX++];
                    nMsg[2] = gzBuffer[nFieldNDX++];
                    nMsg[3] = gzBuffer[nFieldNDX++];
                    Array.Reverse(nMsg);
                    string nTotalMessageLength = TD_GetResponseValue(100, nMsg, nStartPos, 0);


                    /*/
                     * 52 / 82 - NASDAQ Chart
                     * 53 / 83 - NYSE Chart
                     * 55 / 85 - Indices Chart
                    /*/
                    nMsg = new byte[sizeof(short)];
                    nMsg[1] = gzBuffer[nFieldNDX++];
                    nMsg[0] = gzBuffer[nFieldNDX++];
                    int nSID = BitConverter.ToInt16(nMsg, 0);
                    string cStreamingRequestChart = string.Empty;
                    switch (nSID)
                    {

                        case 82:
                            cStreamingRequestChart = " NASDAQ Chart";
                            break;
                        case 83:
                            cStreamingRequestChart = " NYSE Chart";
                            break;
                        case 85:
                            cStreamingRequestChart = " Indices Chart";
                            break;
                    }


                    // Get stock symbol length
                    nMsg = new byte[sizeof(short)];
                    nMsg[1] = gzBuffer[nFieldNDX++];
                    nMsg[0] = gzBuffer[nFieldNDX++];
                    int nSymbolLength = BitConverter.ToInt16(nMsg, 0);


                    // Get stock symbol
                    nStartPos = nFieldNDX;
                    nMsg = new byte[nSymbolLength];
                    Array.Copy(gzBuffer, nStartPos, nMsg, 0, nSymbolLength);
                    string cSymbol = TD_GetResponseValue(0, nMsg, 0, nSymbolLength);

                    nFieldNDX = nFieldNDX + nSymbolLength;


                    // Get status
                    nMsg = new byte[sizeof(short)];
                    nMsg[1] = gzBuffer[nFieldNDX++];
                    nMsg[0] = gzBuffer[nFieldNDX++];
                    int nStatus = BitConverter.ToInt16(nMsg, 0);


                    // Get length of compressed data
                    nMsg = new byte[sizeof(Int32)];
                    nMsg[0] = gzBuffer[nFieldNDX++];
                    nMsg[1] = gzBuffer[nFieldNDX++];
                    nMsg[2] = gzBuffer[nFieldNDX++];
                    nMsg[3] = gzBuffer[nFieldNDX++];
                    Array.Reverse(nMsg);
                    string nTotalLenOfCompressedData = TD_GetResponseValue(100, nMsg, nStartPos, 0);

                    ms.Close();

                    byte[] CompressedData = new byte[Convert.ToInt32(nTotalLenOfCompressedData)];
                    Array.Copy(gzBuffer, nFieldNDX, CompressedData, 0, Convert.ToInt32(nTotalLenOfCompressedData));


                    StringBuilder cTempData = new StringBuilder();
                    int totalLength = 0;
                    byte[] writeData = new byte[65535 * 10];
                    Stream s2 = new InflaterInputStream(new MemoryStream(CompressedData));

                    try
                    {
                        while (true)
                        {
                            int size = s2.Read(writeData, 0, writeData.Length);
                            if (size > 0)
                            {
                                totalLength += size;
                                cTempData.Append(System.Text.Encoding.ASCII.GetString(writeData, 0, size));
                            }
                            else
                            {
                                break;
                            }
                        }
                        s2.Close();

                        string[] cLines = cTempData.ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string cs in cLines)
                        {
                            cSortedLines.Add(cs);
                        }


                    }
                    catch (Exception) { s2.Close(); return cSortedLines; }

                }


                GC.Collect();
            }
            catch (Exception) { }


            return cSortedLines;
        }
Beispiel #18
0
            public void process_AsyncChartSnapshot(IAsyncResult asyncResult)
            {

                try
                {


                    // Get the RequestState object from the asyncresult
                    RequestState rs = (RequestState)asyncResult.AsyncState;

                    if (rs.lNewStockSymbol == true)
                    {
                        rs.CloseStream(rs);

                        return;

                    }


                    if (rs.Request.ServicePoint.CurrentConnections > 0)
                    {

                        List<String> cSortedLines = new List<string>();


                        // Pull out the ResponseStream that was set in RespCallback
                        Stream responseStream = rs.ResponseStream;

                        // At this point rs.BufferRead should have some data in it.
                        // Read will tell us if there is any data there

                        int read = responseStream.EndRead(asyncResult);


                        if (read > 0)
                        {
                            // Make sure we store all the incoming data until we reach the end.

                            Array.Copy(rs.BufferRead, 0, ChartByteArray2, ChartByteArray2NDx, read);
                            ChartByteArray2NDx = ChartByteArray2NDx + read;

                            // Make another call so that we continue retrieving any all incoming data                                    
                            IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);

                        }
                        else
                        {

                            ChartByteArray2NDx = 0;

                            // If we have not more bytes read, then the server has finished sending us data.
                            if (read == 0)
                            {

                                string compressedText = Convert.ToBase64String(ChartByteArray2);
                                byte[] gzBuffer = Convert.FromBase64String(compressedText);


                                MemoryStream ms = new MemoryStream();


                                int nFieldNDX = 0;
                                int nStartPos = 21;
                                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                                ms.Write(gzBuffer, 64, gzBuffer.Length - 64);


                                byte[] nMsg = new byte[sizeof(Int32)];


                                /*/
                                 * S = Streaming
                                 * N = Snapshot
                                /*/

                                nMsg[0] = gzBuffer[nFieldNDX++];
                                string cRequestType = System.Text.Encoding.ASCII.GetString(nMsg, 0, 1);

                                // Skip these next 4 bytes
                                nFieldNDX = nFieldNDX + 4;


                                // Get message length
                                nMsg = new byte[sizeof(Int32)];
                                nMsg[0] = gzBuffer[nFieldNDX++];
                                nMsg[1] = gzBuffer[nFieldNDX++];
                                nMsg[2] = gzBuffer[nFieldNDX++];
                                nMsg[3] = gzBuffer[nFieldNDX++];
                                Array.Reverse(nMsg);
                                string nTotalMessageLength = TD_GetResponseValue(100, nMsg, nStartPos, 0);


                                /*/
                                 * 52 / 82 - NASDAQ Chart
                                 * 53 / 83 - NYSE Chart
                                 * 55 / 85 - Indices Chart
                                /*/

                                nMsg = new byte[sizeof(short)];
                                nMsg[1] = gzBuffer[nFieldNDX++];
                                nMsg[0] = gzBuffer[nFieldNDX++];
                                int nSID = BitConverter.ToInt16(nMsg, 0);
                                string cStreamingRequestChart = string.Empty;
                                switch (nSID)
                                {

                                    case 82:
                                        cStreamingRequestChart = " NASDAQ Chart";

                                        break;
                                    case 83:
                                        cStreamingRequestChart = " NYSE Chart";

                                        break;
                                    case 85:
                                        cStreamingRequestChart = " Indices Chart";

                                        break;
                                }


                                // Get stock symbol length
                                nMsg = new byte[sizeof(short)];
                                nMsg[1] = gzBuffer[nFieldNDX++];
                                nMsg[0] = gzBuffer[nFieldNDX++];
                                int nSymbolLength = BitConverter.ToInt16(nMsg, 0);


                                // Get stock symbol
                                nStartPos = nFieldNDX;
                                nMsg = new byte[nSymbolLength];
                                Array.Copy(gzBuffer, nStartPos, nMsg, 0, nSymbolLength);
                                string cSymbol = TD_GetResponseValue(0, nMsg, 0, nSymbolLength);

                                nFieldNDX = nFieldNDX + nSymbolLength;


                                // Get status
                                nMsg = new byte[sizeof(short)];
                                nMsg[1] = gzBuffer[nFieldNDX++];
                                nMsg[0] = gzBuffer[nFieldNDX++];
                                int nStatus = BitConverter.ToInt16(nMsg, 0);


                                // Get length of compressed data
                                nMsg = new byte[sizeof(Int32)];
                                nMsg[0] = gzBuffer[nFieldNDX++];
                                nMsg[1] = gzBuffer[nFieldNDX++];
                                nMsg[2] = gzBuffer[nFieldNDX++];
                                nMsg[3] = gzBuffer[nFieldNDX++];
                                Array.Reverse(nMsg);
                                string nTotalLenOfCompressedData = TD_GetResponseValue(100, nMsg, nStartPos, 0);


                                ms.Close();

                                byte[] CompressedData = new byte[Convert.ToInt32(nTotalLenOfCompressedData)];
                                Array.Copy(gzBuffer, nFieldNDX, CompressedData, 0, Convert.ToInt32(nTotalLenOfCompressedData));


                                StringBuilder cTempData = new StringBuilder();
                                int totalLength = 0;
                                byte[] writeData = new byte[BUFFER_SIZE];
                                Stream s2 = new InflaterInputStream(new MemoryStream(CompressedData));

                                try
                                {
                                    while (true)
                                    {
                                        int size = s2.Read(writeData, 0, writeData.Length);
                                        if (size > 0)
                                        {
                                            totalLength += size;
                                            cTempData.Append(System.Text.Encoding.ASCII.GetString(writeData, 0, size));
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    s2.Close();

                                    string[] cLines = cTempData.ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (string cs in cLines)
                                    {
                                        cSortedLines.Add(cs);
                                    }

                                    rs.SendHistoricalChartData(cSortedLines, rs.stockSymbol, rs.ServiceName);

                                }
                                catch (Exception)
                                {
                                    s2.Close();
                                }

                            }
                        }
                    }
                }
                catch (Exception exc) { }
            }