0

Using the code below, I am able to download a .gz file containing a .json file, but I am unable to extract the contents from the .json file into memory. The code below results in the text variable being an empty string when I am expecting for it to contain the JSON stored in the .json file.

File Structure

  1. example.gz

    1a. example.json

Source Code

// 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();
                }
            }
Adam Chubbuck
  • 1,612
  • 10
  • 27

1 Answers1

1

This code is working :

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();
            }
        }

From my test, if I don't reset the position of the stream called "ms", I get the variable "text" as an empty string.

If I put the stream to the initial position I get the desidered result.