Beispiel #1
0
        public static string ZipCompress(this string value)
        {
            //Transform string into byte[]  
            byte[] byteArray = new byte[value.Length];
            int indexBA = 0;
            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for compress
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string
            byteArray = ms.ToArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                sB.Append((char)item);
            }
            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return sB.ToString();
        }
Beispiel #2
0
        /// <summary>
        /// Gzip格式解压缩
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] UnZip(byte[] byteArray)
        {
            //Prepare for decompress
            System.IO.MemoryStream           ms   = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream gZip = new System.IO.Compression.GZipStream(ms,
                                                                                         System.IO.Compression.CompressionMode.Decompress);
            //Reset variable to collect uncompressed result

            MemoryStream source = new MemoryStream();

            //从压缩流中读出所有数据
            byte[] tmpByteArray = new byte[1024];
            int    n;

            while ((n = gZip.Read(tmpByteArray, 0, tmpByteArray.Length)) != 0)
            {
                source.Write(tmpByteArray, 0, n);
            }


            gZip.Close();
            ms.Close();
            gZip.Dispose();
            ms.Dispose();
            return(source.ToArray());
        }
Beispiel #3
0
        public string Zip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = this.StrToByteArray(value);

            //Prepare for compress
            System.IO.MemoryStream           ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(
                ms,
                System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string

            // C# to convert a byte array to a string.
            byteArray = ms.ToArray();
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            string str = enc.GetString(byteArray);

            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return(str);
        }
Beispiel #4
0
        public static string UnZip(string value)
        {
            byte[] array = new byte[value.Length];
            int    num   = 0;

            char[] array2 = value.ToCharArray();
            for (int i = 0; i < array2.Length; i++)
            {
                char c = array2[i];
                array[num++] = (byte)c;
            }
            MemoryStream memoryStream = new MemoryStream(array);

            System.IO.Compression.GZipStream gZipStream = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Decompress);
            array = new byte[array.Length];
            int           num2          = gZipStream.Read(array, 0, array.Length);
            StringBuilder stringBuilder = new StringBuilder(num2);

            for (int j = 0; j < num2; j++)
            {
                stringBuilder.Append((char)array[j]);
            }
            gZipStream.Close();
            memoryStream.Close();
            gZipStream.Dispose();
            memoryStream.Dispose();
            return(stringBuilder.ToString());
        }
            public void WillDecodeGZipedResponse()
            {
                var testable        = new TestableJavaScriptReducer();
                var mockWebResponse = new Mock <WebResponse>();
                var ms     = new MemoryStream();
                var stream =
                    new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);

                stream.Write(new UTF8Encoding().GetBytes("my response"), 0, "my response".Length);
                stream.Close();
                var encodedArray = ms.ToArray();

                ms.Close();
                stream.Dispose();
                ms.Dispose();
                mockWebResponse.Setup(x => x.GetResponseStream()).Returns(new MemoryStream(encodedArray));
                mockWebResponse.Setup(x => x.Headers).Returns(new WebHeaderCollection()
                {
                    { "Content-Encoding", "gzip" }
                });
                testable.Mock <IWebClientWrapper>().Setup(x => x.Download <JavaScriptResource>("http://host/js1.js")).Returns(mockWebResponse.Object);
                testable.Mock <IMinifier>().Setup(x => x.Minify <JavaScriptResource>("my response;\r\n")).Returns("min");

                var result = testable.ClassUnderTest.Process("http://host/js1.js::");

                testable.Mock <IStore>().Verify(
                    x =>
                    x.Save(Encoding.UTF8.GetBytes("min").MatchEnumerable(), result,
                           "http://host/js1.js::"), Times.Once());
                ms.Dispose();
            }
Beispiel #6
0
        public string UnZip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = this.StrToByteArray(value);

            //Prepare for decompress
            System.IO.MemoryStream           ms = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
                                                                                       System.IO.Compression.CompressionMode.Decompress);

            //Reset variable to collect uncompressed result
            byteArray = new byte[byteArray.Length];

            //Decompress
            int rByte = sr.Read(byteArray, 0, byteArray.Length);

            //Transform byte[] unzip data to string
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            string str = enc.GetString(byteArray);

            sr.Close();
            ms.Close();
            sr.Dispose();
            ms.Dispose();
            return(str);
        }
Beispiel #7
0
        public string UnZip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = new byte[value.Length];
            int    indexBA   = 0;

            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }
            //Prepare for decompress
            System.IO.MemoryStream           ms = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
            //Reset variable to collect uncompressed result
            byteArray = new byte[byteArray.Length];
            //Decompress
            int rByte = sr.Read(byteArray, 0, byteArray.Length);

            //Transform byte[] unzip data to string
            System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
            //Read the number of bytes GZipStream red and do not a for each bytes in
            //resultByteArray;
            for (int i = 0; i < rByte; i++)
            {
                sB.Append((char)byteArray[i]);
            }
            sr.Close();
            ms.Close();
            sr.Dispose();
            ms.Dispose();

            return(sB.ToString());
        }
        public static string UnZip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = new byte[value.Length];
            int indexBA = 0;
            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for decompress
            System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Decompress);

            //Reset variable to collect uncompressed result
            byteArray = new byte[byteArray.Length];

            //Decompress
            int rByte = sr.Read(byteArray, 0, byteArray.Length);

            //Transform byte[] unzip data to string
            System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
            //Read the number of bytes GZipStream red and do not a for each bytes in
            //resultByteArray;
            for (int i = 0; i < rByte; i++)
            {
                sB.Append((char)byteArray[i]);
            }
            sr.Close();
            ms.Close();
            sr.Dispose();
            ms.Dispose();
            return sB.ToString();
        }
        public static string Zip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = new byte[value.Length];
            int    indexBA   = 0;

            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for compress
            System.IO.MemoryStream           ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                                                                                       System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string
            byteArray = ms.ToArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                sB.Append((char)item);
            }
            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return(sB.ToString());
        }
Beispiel #10
0
        public static string lerror = ""; // Gets the last error that occurred in this struct. Similar to the C perror().

        public static bool Decompress(byte[] indata, string outfile)
        {
            try
            {
                System.IO.MemoryStream           ms  = new System.IO.MemoryStream(indata);
                System.IO.StreamWriter           sw  = new System.IO.StreamWriter(outfile);
                System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
                int lbyte = gzs.ReadByte();
                while (lbyte != -1)
                {
                    sw.BaseStream.WriteByte((byte)lbyte);
                    lbyte = gzs.ReadByte();
                }
                gzs.Close();
                gzs.Dispose();
                sw.Close();
                sw.Dispose();
            }
            catch (System.Exception ex)
            {
                lerror = ex.Message;
                return(false);
            }
            return(true);
        }
Beispiel #11
0
        void StopCore(string msg)
        {
            coredead = true;
            Console.WriteLine("Core stopped.");
            for (int i = 0; i < soundbuffer.Length; i++)
            {
                soundbuffer[i] = 0;
            }

            var gz = new System.IO.Compression.GZipStream(
                new MemoryStream(Convert.FromBase64String(dispfont), false),
                System.IO.Compression.CompressionMode.Decompress);

            byte[] font = new byte[2048];
            gz.Read(font, 0, 2048);
            gz.Dispose();

            // cores aren't supposed to have bad dependencies like System.Drawing, right?

            int scx = 0;
            int scy = 0;

            foreach (char c in msg)
            {
                if (scx == 240 || c == '\n')
                {
                    scy += 8;
                    scx  = 0;
                }
                if (scy == 160)
                {
                    break;
                }
                if (c == '\r' || c == '\n')
                {
                    continue;
                }
                if (c < 256 && c != ' ')
                {
                    int fpos = c * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if ((font[fpos] >> i & 1) != 0)
                            {
                                videobuffer[(scy + j) * 240 + scx + i] = unchecked ((int)0xffff0000);
                            }
                            else
                            {
                                videobuffer[(scy + j) * 240 + scx + i] = unchecked ((int)0xff000000);
                            }
                        }
                        fpos++;
                    }
                }
                scx += 8;
            }
        }
Beispiel #12
0
 public void Dispose()
 {
     LinePtr.Dispose();
     if (UnGZPtr is object)
     {
         UnGZPtr.Dispose();
     }
 }
Beispiel #13
0
        static void HttpPost()
        {
            string url  = ConfigurationManager.AppSettings["url"];
            string data = ConfigurationManager.AppSettings["data"];
            //命名空间System.Net下的HttpWebRequest类
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            //参照浏览器的请求报文 封装需要的参数 这里参照ie9
            //浏览器可接受的MIME类型
            request.Accept = "text/plain, */*; q=0.01";
            //浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用
            request.UserAgent   = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            request.ContentType = "application/json;charset=UTF-8";
            //请求方式
            request.Method = "POST";
            //是否保持长连接
            request.KeepAlive = false;
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            //表示请求消息正文的长度
            //request.ContentLength = data.Length;
            Stream postStream = request.GetRequestStream();

            byte[] postData = Encoding.UTF8.GetBytes(data);
            //将传输的数据,请求正文写入请求流
            postStream.Write(postData, 0, postData.Length);
            postStream.Dispose();
            //响应
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.ContentEncoding == "gzip")
            {//判断响应的信息是否为压缩信息 若为压缩信息解压后返回
                MemoryStream ms = new MemoryStream();
                System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                byte[] buffer = new byte[1024];
                int    l      = zip.Read(buffer, 0, buffer.Length);
                while (l > 0)
                {
                    ms.Write(buffer, 0, l);
                    l = zip.Read(buffer, 0, buffer.Length);
                }
                ms.Dispose();
                zip.Dispose();
                Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
            }
            else
            {
                System.IO.Stream s;
                s = response.GetResponseStream();
                string        StrDate = "";
                StringBuilder sb      = new StringBuilder();
                StreamReader  Reader  = new StreamReader(s, Encoding.UTF8);
                while ((StrDate = Reader.ReadLine()) != null)
                {
                    sb.Append(StrDate + "\r\n");
                }
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff:::") + sb.ToString());
            }
        }
Beispiel #14
0
        private void Decompress()
        {
            if (this._Compressed.Length == 0)
            {
                this._UnCompressed = string.Empty;
                return;
            }

            string Result = string.Empty;

            try
            {
                byte[] ZippedData = null;

                //Convert the compressed string into a byte array and decrypt the array if required
                if (this._Passphrase.Length > 0)
                {
                    ZippedData = Decrypt(System.Convert.FromBase64String(this._Compressed));
                }
                else
                {
                    ZippedData = System.Convert.FromBase64String(this._Compressed);
                }

                //Decompress the byte array
                System.IO.MemoryStream           objMemStream  = new System.IO.MemoryStream(ZippedData);
                System.IO.Compression.GZipStream objGZipStream = new System.IO.Compression.GZipStream(objMemStream, System.IO.Compression.CompressionMode.Decompress);
                byte[] sizeBytes = new byte[4];

                objMemStream.Position = objMemStream.Length - 4;
                objMemStream.Read(sizeBytes, 0, 4);

                int iOutputSize = BitConverter.ToInt32(sizeBytes, 0);

                objMemStream.Position = 0;

                byte[] UnZippedData = new byte[iOutputSize];

                objGZipStream.Read(UnZippedData, 0, iOutputSize);

                objGZipStream.Dispose();
                objMemStream.Dispose();

                //Convert the decompressed byte array back to a string
                Result = this._TextEncoding.GetString(UnZippedData);
            }
            catch (Exception)
            {
            }
            finally
            {
                this._UnCompressed = Result;
            }
        }
Beispiel #15
0
        /// <summary>
        /// In Memory GZip Decompressor
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DecompressGZip(this byte[] data)
        {
            int length = 10000; //10Kb

            byte[] Ob     = new byte[length];
            byte[] result = null;

            MemoryStream ms = null;

            System.IO.Compression.GZipStream gz = null;

            try
            {
                using (ms = new MemoryStream(data))
                {
                    using (gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                    {
                        int a = 0;
                        while ((a = gz.Read(Ob, 0, length)) > 0)
                        {
                            if (a == length)
                            {
                                //result = Concat(result, Ob);
                                result = result.Concat(Ob);
                            }
                            else
                            {
                                //result = Concat(result, Substring(Ob, 0, a));
                                result = result.Concat(Ob.Substring(0, a));
                            }
                        }
                    }
                }
            }
            catch
            {
                result = null;
            }
            finally
            {
                if (gz != null)
                {
                    gz.Close();
                    gz.Dispose();
                }
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
            }

            return(result);
        }
Beispiel #16
0
        private void Compress()
        {
            if (this._UnCompressed.Length == 0)
            {
                this._Compressed = string.Empty;
                return;
            }

            string Result = string.Empty;


            try
            {
                //Convert the uncompressed string into a byte array
                byte[] UnZippedData = this._TextEncoding.GetBytes(this._UnCompressed);

                //Compress the byte array
                System.IO.MemoryStream           MS   = new System.IO.MemoryStream();
                System.IO.Compression.GZipStream GZip = new System.IO.Compression.GZipStream(MS, System.IO.Compression.CompressionMode.Compress);
                GZip.Write(UnZippedData, 0, UnZippedData.Length);
                //Don't FLUSH here - it possibly leads to data loss!
                GZip.Close();

                byte[] ZippedData = null;

                //Encrypt the compressed byte array, if required
                if (this._Passphrase.Length > 0)
                {
                    ZippedData = Encrypt(MS.ToArray());
                }
                else
                {
                    ZippedData = MS.ToArray();
                }

                //Convert the compressed byte array back to a string
                Result = System.Convert.ToBase64String(ZippedData);

                MS.Close();
                GZip.Dispose();
                MS.Dispose();
            }
            catch (Exception)
            {
                //Keep quiet - in case of an exception an empty string is returned
            }
            finally
            {
                this._Compressed = Result;
            }
        }
Beispiel #17
0
 public static bool compress(string infile, string outfile)
 {
     try {
         byte[] ifdata = System.IO.File.ReadAllBytes(infile);
         System.IO.StreamWriter           sw  = new System.IO.StreamWriter(outfile);
         System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(sw.BaseStream, System.IO.Compression.CompressionMode.Compress);
         gzs.Write(ifdata, 0, ifdata.Length);
         gzs.Close();
         gzs.Dispose();
     } catch (System.Exception ex) {
         lerror = ex.Message;
         return(false);
     }
     return(true);
 }
Beispiel #18
0
        public static string lerror = ""; // Gets the last error that occurred in this struct. Similar to the C perror().

        #endregion Fields

        #region Methods

        public static bool compress(string infile, string outfile)
        {
            try {
                byte[] ifdata = System.IO.File.ReadAllBytes(infile);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(outfile);
                System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(sw.BaseStream, System.IO.Compression.CompressionMode.Compress);
                gzs.Write(ifdata, 0, ifdata.Length);
                gzs.Close();
                gzs.Dispose();
            } catch (System.Exception ex) {
                lerror = ex.Message;
                return false;
            }
            return true;
        }
Beispiel #19
0
 /// <summary>
 /// Compress contents to gzip
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 /// <remarks></remarks>
 public static byte[] GZIP(ref string s)
 {
     byte[] buffer = null;
     byte[] compressedData = null;
     MemoryStream oMemoryStream = null;
     System.IO.Compression.GZipStream compressedzipStream = null;
     oMemoryStream = new MemoryStream();
     buffer = System.Text.Encoding.UTF8.GetBytes(s);
     compressedzipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Compress, true);
     compressedzipStream.Write(buffer, 0, buffer.Length);
     compressedzipStream.Dispose();
     compressedzipStream.Close();
     compressedData = oMemoryStream.ToArray();
     oMemoryStream.Close();
     return compressedData;
 }
Beispiel #20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public string GetRealdatabase(int timeout = 50000)
        {
            string filename = string.Empty;
            var    mb       = GetBuffer(ApiFunConst.TagInfoRequest, 1 + 9);

            mb.Write(ApiFunConst.SyncRealTagConfig);
            mb.Write(LoginId);
            this.SyncDataEvent.Reset();
            SendData(mb);

            if (SyncDataEvent.WaitOne(timeout))
            {
                try
                {
                    if ((this.mRealSyncData.WriteIndex - mRealSyncData.ReadIndex) > 0)
                    {
                        try
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();

                            ms.Write(this.mRealSyncData.ReadBytes((int)(this.mRealSyncData.WriteIndex - mRealSyncData.ReadIndex)));
                            ms.Position = 0;
                            System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
                            filename = System.IO.Path.GetTempFileName();
                            var sfile = System.IO.File.Open(filename, System.IO.FileMode.OpenOrCreate);
                            gzip.CopyTo(sfile);
                            sfile.Close();

                            ms.Dispose();
                            gzip.Dispose();
                            return(filename);
                        }
                        catch
                        {
                        }
                    }
                }
                finally
                {
                    mRealSyncData?.UnlockAndReturn();
                    mRealSyncData = null;
                }
            }

            return(filename);
        }
Beispiel #21
0
        /// <summary>
        ///  解压缩 *.Z 文件,可指定是否删除原文件。
        /// </summary>
        /// <param name="compressedFilePath"></param>
        /// <param name="destDir"></param>
        public static void DecompressLzw(string compressedFilePath, string destDir, bool deleSourse = false, bool overwrite = true)
        {
            string dest = destDir + "\\" + Path.GetFileNameWithoutExtension(compressedFilePath);

            if (File.Exists(dest))
            {
                if (overwrite)
                {
                    File.Delete(dest);
                }
                else
                {
                    return;
                }
            }

            Utils.FileUtil.CheckOrCreateDirectory(destDir);

            FileStream fs = new FileStream(compressedFilePath, FileMode.Open, FileAccess.Read);

            System.IO.Compression.GZipStream input = new  System.IO.Compression.GZipStream(fs, System.IO.Compression.CompressionMode.Decompress);
            //ICSharpCode.SharpZipLib.LZW.LzwInputStream input = new ICSharpCode.SharpZipLib.LZW.LzwInputStream(fs);

            FileStream output = new FileStream(dest, FileMode.Create);
            int        count = 0, size = 1024;

            byte[] buffer = new byte[size];
            while ((count = input.Read(buffer, 0, size)) > 0)
            {
                output.Write(buffer, 0, count); output.Flush();
            }
            output.Close();
            output.Dispose();
            input.Close();
            input.Dispose();


            //    ZipUtil.DecompressFile(compressedFilePath, dest);

            if (deleSourse)
            {
                File.Delete(compressedFilePath);
            }
        }
Beispiel #22
0
 public static bool decompress(string infile, string outfile)
 {
     try {
         System.IO.StreamWriter sw = new System.IO.StreamWriter(outfile);
         System.IO.StreamReader sr = new System.IO.StreamReader(infile);
         System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(sr.BaseStream, System.IO.Compression.CompressionMode.Decompress);
         int lbyte = gzs.ReadByte();
         while (lbyte != -1) {
             sw.BaseStream.WriteByte((byte) lbyte);
             lbyte = gzs.ReadByte();
         }
         gzs.Close();
         gzs.Dispose();
         sw.Close();
         sw.Dispose();
     } catch (System.Exception ex) {
         lerror = ex.Message;
         return false;
     }
     return true;
 }
        /// <summary>
        /// In Memory Compression with Gzip
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] CompressGZip(this byte[] data)
        {
            byte[] res = null;
            MemoryStream ms = null;
            System.IO.Compression.GZipStream gz = null;

            try
            {
                using (ms = new MemoryStream())
                {
                    using (gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, false))
                    {
                        gz.Write(data, 0, data.Length);
                        gz.Close();
                    }

                    res = ms.ToArray();

                }
            }
            catch
            {
                res = null;
            }
            finally
            {
                if (gz != null)
                {
                    gz.Close();
                    gz.Dispose();
                }
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
            }

            return res;
        }
Beispiel #24
0
        /// <summary>
        /// In Memory Compression with Gzip
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] CompressGZip(this byte[] data)
        {
            byte[]       res = null;
            MemoryStream ms  = null;

            System.IO.Compression.GZipStream gz = null;

            try
            {
                using (ms = new MemoryStream())
                {
                    using (gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, false))
                    {
                        gz.Write(data, 0, data.Length);
                        gz.Close();
                    }

                    res = ms.ToArray();
                }
            }
            catch
            {
                res = null;
            }
            finally
            {
                if (gz != null)
                {
                    gz.Close();
                    gz.Dispose();
                }
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
            }

            return(res);
        }
Beispiel #25
0
		void StopCore(string msg)
		{
			coredead = true;
			Console.WriteLine("Core stopped.");
			for (int i = 0; i < soundbuffer.Length; i++)
				soundbuffer[i] = 0;

			var gz = new System.IO.Compression.GZipStream(
				new MemoryStream(Convert.FromBase64String(dispfont), false),
				System.IO.Compression.CompressionMode.Decompress);
			byte[] font = new byte[2048];
			gz.Read(font, 0, 2048);
			gz.Dispose();
			
			// cores aren't supposed to have bad dependencies like System.Drawing, right?

			int scx = 0;
			int scy = 0;

			foreach (char c in msg)
			{
				if (scx == 240 || c == '\n')
				{
					scy += 8;
					scx = 0;
				}
				if (scy == 160)
					break;
				if (c == '\r' || c == '\n')
					continue;
				if (c < 256 && c != ' ')
				{
					int fpos = c * 8;
					for (int j = 0; j < 8; j++)
					{
						for (int i = 0; i < 8; i++)
						{
							if ((font[fpos] >> i & 1) != 0)
								videobuffer[(scy + j) * 240 + scx + i] = unchecked((int)0xffff0000);
							else
								videobuffer[(scy + j) * 240 + scx + i] = unchecked((int)0xff000000);
						}
						fpos++;
					}
				}
				scx += 8;
			}
		}
        public void accept(string txtName)
        {
            textName = txtName;
            string type              = Request.ContentType;
            int    separator         = type.IndexOf("/");
            string compressionFormat = type.Substring(separator + 1);

            int len = txtName.Length;

            //compress
            byte[] stringToByte = new byte[len + 1];
            int    byteIndex    = 0;

            foreach (char c in txtName.ToCharArray())
            {
                stringToByte[byteIndex++] = (byte)c;
            }
            var bytes = Encoding.ASCII.GetBytes(txtName);

            System.IO.MemoryStream           memoryStream = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream gzipStream   = new System.IO.Compression.GZipStream(memoryStream, System.IO.Compression.CompressionMode.Compress);


            gzipStream.Write(bytes, 0, bytes.Length);
            gzipStream.Close();

            stringToByte = memoryStream.ToArray();

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(stringToByte.Length);
            foreach (byte b in stringToByte)
            {
                stringBuilder.Append((char)b);
            }
            memoryStream.Close();
            gzipStream.Dispose();
            memoryStream.Dispose();

            string s = stringBuilder.ToString();

            //Decompress
            byte[] decompressStream = new byte[s.Length];
            byteIndex = 0;
            foreach (char c in s.ToCharArray())
            {
                decompressStream[byteIndex++] = (byte)c;
            }

            System.IO.MemoryStream           ms = new System.IO.MemoryStream(decompressStream);
            System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);

            int byteRead = gs.Read(decompressStream, 0, decompressStream.Length);

            System.Text.StringBuilder builder = new System.Text.StringBuilder(byteRead);
            foreach (byte b in decompressStream)
            {
                builder.Append((char)b);
            }

            ms.Close();
            gs.Close();
            ms.Dispose();
            gs.Dispose();

            String str = builder.ToString();
            int    a   = 0;
        }
Beispiel #27
0
        public static string UnGZIP(ref byte[] input)
        {
            System.IO.MemoryStream oMemoryStream = null;
            System.IO.Compression.GZipStream oGZipStream = null;
            int iLength = 0;
            byte[] byteArray = new byte[100001];
            System.Text.StringBuilder oStringBuilder = new System.Text.StringBuilder();

            oMemoryStream = new System.IO.MemoryStream(input);
            oGZipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Decompress);
            oMemoryStream.Flush();
            oGZipStream.Flush();
            do
            {
                iLength = oGZipStream.Read(byteArray, 0, byteArray.Length);
                if (iLength < 1)
                    break; // TODO: might not be correct. Was : Exit Do
                oStringBuilder.Append(System.Text.Encoding.UTF8.GetString(byteArray, 0, iLength));
            } while (true);
            oGZipStream.Close();
            oMemoryStream.Close();
            oGZipStream.Dispose();
            oMemoryStream.Dispose();
            return oStringBuilder.ToString();
        }
        public void Zlib_GZipStream_FileName_And_Comments()
        {
            // select the name of the zip file
            string FileToCompress = System.IO.Path.Combine(TopLevelDir, "Zlib_GZipStream.dat");
            Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary zip file '{0}' already exists.", FileToCompress);
            byte[] working = new byte[WORKING_BUFFER_SIZE];
            int n = -1;

            int sz = this.rnd.Next(21000) + 15000;
            TestContext.WriteLine("  Creating file: {0} sz({1})", FileToCompress, sz);
            CreateAndFillFileText(FileToCompress, sz);

            System.IO.FileInfo fi1 = new System.IO.FileInfo(FileToCompress);
            int crc1 = DoCrc(FileToCompress);

            // four trials, all combos of FileName and Comment null or not null.
            for (int k = 0; k < 4; k++)
            {
                string CompressedFile = String.Format("{0}-{1}.compressed", FileToCompress, k);

                using (Stream input = File.OpenRead(FileToCompress))
                {
                    using (FileStream raw = new FileStream(CompressedFile, FileMode.Create))
                    {
                        using (GZipStream compressor =
                               new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, true))
                        {
                            // FileName is optional metadata in the GZip bytestream
                            if (k % 2 == 1)
                                compressor.FileName = FileToCompress;

                            // Comment is optional metadata in the GZip bytestream
                            if (k > 2)
                                compressor.Comment = "Compressing: " + FileToCompress;

                            byte[] buffer = new byte[1024];
                            n = -1;
                            while (n != 0)
                            {
                                if (n > 0)
                                    compressor.Write(buffer, 0, n);

                                n = input.Read(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }

                System.IO.FileInfo fi2 = new System.IO.FileInfo(CompressedFile);

                Assert.IsTrue(fi1.Length > fi2.Length, String.Format("Compressed File is not smaller, trial {0} ({1}!>{2})", k, fi1.Length, fi2.Length));


                // decompress twice:
                // once with System.IO.Compression.GZipStream and once with Alienlab.Zlib.GZipStream
                for (int j = 0; j < 2; j++)
                {
                    using (var input = System.IO.File.OpenRead(CompressedFile))
                    {

                        Stream decompressor = null;
                        try
                        {
                            switch (j)
                            {
                                case 0:
                                    decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true);
                                    break;
                                case 1:
                                    decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
                                    break;
                            }

                            string DecompressedFile =
                                                        String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL");

                            TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile));

                            using (var s2 = System.IO.File.Create(DecompressedFile))
                            {
                                n = -1;
                                while (n != 0)
                                {
                                    n = decompressor.Read(working, 0, working.Length);
                                    if (n > 0)
                                        s2.Write(working, 0, n);
                                }
                            }

                            int crc2 = DoCrc(DecompressedFile);
                            Assert.AreEqual<Int32>(crc1, crc2);

                        }
                        finally
                        {
                            if (decompressor != null)
                                decompressor.Dispose();
                        }
                    }
                }
            }
        }
        private void Compress()
        {
            if (this._UnCompressed.Length == 0)
            {
                this._Compressed = string.Empty;
                return;
            }

            string Result = string.Empty;

            try
            {
                //Convert the uncompressed string into a byte array
                byte[] UnZippedData = this._TextEncoding.GetBytes(this._UnCompressed);

                //Compress the byte array
                System.IO.MemoryStream MS = new System.IO.MemoryStream();
                System.IO.Compression.GZipStream GZip = new System.IO.Compression.GZipStream(MS, System.IO.Compression.CompressionMode.Compress);
                GZip.Write(UnZippedData, 0, UnZippedData.Length);
                //Don't FLUSH here - it possibly leads to data loss!
                GZip.Close();

                byte[] ZippedData = null;

                //Encrypt the compressed byte array, if required
                if (this._Passphrase.Length > 0)
                {
                    ZippedData = Encrypt(MS.ToArray());
                }
                else
                {
                    ZippedData = MS.ToArray();
                }

                //Convert the compressed byte array back to a string
                Result = System.Convert.ToBase64String(ZippedData);

                MS.Close();
                GZip.Dispose();
                MS.Dispose();
            }
            catch (Exception)
            {
                //Keep quiet - in case of an exception an empty string is returned
            }
            finally
            {
                this._Compressed = Result;
            }
        }
        private void Decompress()
        {
            if (this._Compressed.Length == 0)
            {
                this._UnCompressed = string.Empty;
                return;
            }

            string Result = string.Empty;

            try
            {
                byte[] ZippedData = null;

                //Convert the compressed string into a byte array and decrypt the array if required
                if (this._Passphrase.Length > 0)
                {
                    ZippedData = Decrypt(System.Convert.FromBase64String(this._Compressed));
                }
                else
                {
                    ZippedData = System.Convert.FromBase64String(this._Compressed);
                }

                //Decompress the byte array
                System.IO.MemoryStream objMemStream = new System.IO.MemoryStream(ZippedData);
                System.IO.Compression.GZipStream objGZipStream = new System.IO.Compression.GZipStream(objMemStream, System.IO.Compression.CompressionMode.Decompress);
                byte[] sizeBytes = new byte[4];

                objMemStream.Position = objMemStream.Length - 4;
                objMemStream.Read(sizeBytes, 0, 4);

                int iOutputSize = BitConverter.ToInt32(sizeBytes, 0);

                objMemStream.Position = 0;

                byte[] UnZippedData = new byte[iOutputSize];

                objGZipStream.Read(UnZippedData, 0, iOutputSize);

                objGZipStream.Dispose();
                objMemStream.Dispose();

                //Convert the decompressed byte array back to a string
                Result = this._TextEncoding.GetString(UnZippedData);

            }
            catch (Exception)
            {
            }
            finally
            {
                this._UnCompressed = Result;
            }
        }
        public void accept(string txtName)
        {
            textName = txtName;
            string type = Request.ContentType;
            int separator = type.IndexOf("/");
            string compressionFormat = type.Substring(separator+1);

            int len = txtName.Length;

            //compress
            byte[] stringToByte=new byte[len+1];
            int byteIndex=0;
            foreach(char c in txtName.ToCharArray())
            {
                stringToByte[byteIndex++] = (byte)c;
            }
            var bytes = Encoding.ASCII.GetBytes(txtName);

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream gzipStream = new System.IO.Compression.GZipStream(memoryStream,System.IO.Compression.CompressionMode.Compress);

            gzipStream.Write(bytes,0,bytes.Length);
            gzipStream.Close();

            stringToByte = memoryStream.ToArray();

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(stringToByte.Length);
            foreach(byte b in stringToByte)
            {
                stringBuilder.Append((char)b);
            }
            memoryStream.Close();
            gzipStream.Dispose();
            memoryStream.Dispose();

            string s = stringBuilder.ToString();

            //Decompress
            byte[] decompressStream=new byte[s.Length];
            byteIndex=0;
            foreach(char c in s.ToCharArray())
            {
                decompressStream[byteIndex++]=(byte)c;
            }

            System.IO.MemoryStream ms = new System.IO.MemoryStream(decompressStream);
            System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms,System.IO.Compression.CompressionMode.Decompress);

            int byteRead = gs.Read(decompressStream,0,decompressStream.Length);

            System.Text.StringBuilder builder = new System.Text.StringBuilder(byteRead);
             foreach(byte b in decompressStream)
             {
                 builder.Append((char)b);
             }

             ms.Close();
             gs.Close();
             ms.Dispose();
             gs.Dispose();

             String str = builder.ToString();
             int a = 0;
        }
        /// <summary>
        /// In Memory GZip Decompressor 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DecompressGZip(this byte[] data)
        {
            int length = 10000; //10Kb
            byte[] Ob = new byte[length];
            byte[] result = null;

            MemoryStream ms = null;
            System.IO.Compression.GZipStream gz = null;

            try
            {
                using (ms = new MemoryStream(data))
                {
                    using (gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                    {
                        int a = 0;
                        while ((a = gz.Read(Ob, 0, length)) > 0)
                        {
                            if (a == length)
                                //result = Concat(result, Ob);
                                result = result.Concat(Ob);
                            else
                                //result = Concat(result, Substring(Ob, 0, a));
                                result = result.Concat(Ob.Substring(0, a));
                        }
                    }
                }
            }
            catch
            {
                result = null;
            }
            finally
            {
                if (gz != null)
                {
                    gz.Close();
                    gz.Dispose();
                }
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
            }

            return result;
        }
Beispiel #33
-1
        /// <summary>
        /// Compress contents to gzip
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static byte[] GZIP(ref byte[] input)
        {
            try
            {
                byte[] compressedData = null;
                MemoryStream oMemoryStream = null;
                System.IO.Compression.GZipStream compressedzipStream = null;
                oMemoryStream = new MemoryStream();
                compressedzipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Compress, false);
                compressedzipStream.Write(input, 0, input.Length);
                compressedzipStream.Close();
                compressedData = oMemoryStream.ToArray();
                compressedzipStream.Dispose();
                compressedzipStream.Close();
                oMemoryStream.Close();

                return compressedData;
            }
            catch (Exception ex)
            {

                return null;
            }
        }