【问题标题】:Extracting the contents of a .JSON file inside of a .GZ file在 .GZ 文件中提取 .JSON 文件的内容
【发布时间】:2019-10-10 20:41:22
【问题描述】:

使用下面的代码,我可以下载一个包含 .json 文件的 .gz 文件,但我无法将 .json 文件中的内容提取到内存中。当我期望它包含存储在 .json 文件中的 JSON 时,下面的代码导致 text 变量为空字符串。

文件结构

  1. example.gz

    1a。例子.json

源代码

// Identify the location of the .gz file containing the results of the export

            string jobOutputGzipLocation = null;
            for (var i = 0; i < 5; i++)
            {
                var httpResponse2 = await _httpClient.GetAsync($"api/v2/jobs/{jobId}");
                if (httpResponse2.IsSuccessStatusCode)
                {
                    var httpMessage2 = await httpResponse2.Content.ReadAsStringAsync();
                    var httpJsonMessage2 = JObject.Parse(httpMessage2);
                    if (string.Equals(httpJsonMessage2.Value<string>("status"), "completed", StringComparison.OrdinalIgnoreCase)) {
                        jobOutputGzipLocation = httpJsonMessage2.Value<string>("location");
                        break;
                    }
                }

                await Task.Delay(3000);
            }
            if (string.IsNullOrEmpty(jobOutputGzipLocation))
                throw new Exception("Unable to identify jobOutputGzipLocation from the job results.");

// Extract the contents of the .gz file into memory

            var httpResponse3 = await _httpClient.GetAsync(jobOutputGzipLocation);
            if (!httpResponse3.IsSuccessStatusCode) throw new Exception("Shoot3");
            var httpMessage3 = await httpResponse3.Content.ReadAsStreamAsync();

            using (var ms = new MemoryStream())
            {
                using (var gZipStream = new GZipStream(httpMessage3, CompressionMode.Decompress))
                {
                    gZipStream.CopyTo(ms);
                }
                using (var sr = new StreamReader(ms, Encoding.UTF8))
                {
                    var text = sr.ReadToEnd();
                }
            }

【问题讨论】:

  • 哪个部分不工作?
  • @tymtam 不幸的是,ZipArchive 不适用于 .gz 文件。
  • @tymtam 我更新了我的帖子。希望这更清楚一点。如果您有任何具体问题,请告诉我。
  • 其实它确实是stackoverflow.com/questions/22145836/…的副本(我关闭然后错误地重新打开):From Stream.CopyTo 复制从当前流中的当前位置开始

标签: c# json gzip


【解决方案1】:

此代码正在运行:

using (var ss = new FileStream("log.txt.gz", FileMode.Open))
        using (var ms = new MemoryStream())
        {
            using (var gZipStream = new GZipStream(ss, CompressionMode.Decompress))
            {
                gZipStream.CopyTo(ms);
            }
            ms.Seek(0, SeekOrigin.Begin);  // so I put the stream to the initial position
            using (var sr = new StreamReader(ms, Encoding.UTF8))
            {
                var text = sr.ReadToEnd();
            }
        }

根据我的测试,如果我不重置名为“ms”的流的位置,我会得到变量“text”作为空字符串。

如果我将流放在初始位置,我会得到想要的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多