Esempio n. 1
0
        /// <summary>
        /// 异步解压缩
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <param name="strUnpackDir"></param>
        /// <returns></returns>
        public async static Task <bool> UnGzipAsync(string strFilePath, string strUnpackDir)
        {
            if (!File.Exists(strFilePath))
            {
                return(false);
            }

            strUnpackDir = strUnpackDir.Replace("/", "\\");
            if (!strUnpackDir.EndsWith("\\"))
            {
                strUnpackDir += "\\";
            }

            if (!Directory.Exists(strUnpackDir))
            {
                Directory.CreateDirectory(strUnpackDir);
            }

            try
            {
                byte[] cbytes = await ReadFileToBytesAsync(strFilePath);

                using (FileStream streamWriter = File.Create(strUnpackDir + "\\11.json"))
                {
                    using (MemoryStream cms = new MemoryStream(cbytes))
                    {
                        using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Decompress))
                        {
                            byte[] bytes = new byte[1024];
                            int    len   = 0;
                            //读取压缩流,同时会被解压
                            while ((len = await gzip.ReadAsync(bytes, 0, bytes.Length)) > 0)
                            {
                                await streamWriter.WriteAsync(bytes, 0, len);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        public static async Task <byte[]> DownloadData(this HttpClient client, string address, bool handleGzip)
        {
            if (!handleGzip)
            {
                return(await client.GetByteArrayAsync(address));
            }

            var result = await client.GetByteArrayAsync(address);

            if (result != null && result.Length > 3 && result[0] == GZipStarter[0] && result[1] == GZipStarter[1] && result[2] == GZipStarter[2])
            {
                // GZIP:
                using (var stream = new System.IO.Compression.GZipStream(new MemoryStream(result), System.IO.Compression.CompressionMode.Decompress))
                {
                    var buffer = new byte[4096];
                    using (var memory = new MemoryStream())
                    {
                        while (true)
                        {
                            var count = await stream.ReadAsync(buffer, 0, buffer.Length);

                            if (count > 0)
                            {
                                await memory.WriteAsync(buffer, 0, count);
                            }
                            else
                            {
                                break;
                            }
                        }

                        return(memory.ToArray());
                    }
                }
            }
            else
            {
                return(result);
            }
        }