Created
December 20, 2023 12:02
-
-
Save vandanrohatgi/60ff5d5507df3129c54c9b1353e204fa to your computer and use it in GitHub Desktop.
C# code to return decompressed gzip data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Script: ScriptBase{ | |
public override async Task < HttpResponseMessage > ExecuteAsync(){ | |
var response = new HttpResponseMessage(); | |
var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false); | |
var contentAsJson = JObject.Parse(contentAsString); | |
var file = (string) contentAsJson("file"); | |
byte[] data = Convert.FromBase64String(file); | |
string decompressedData = DecompressGzip(data); | |
response.Content = CreateJsonContent(new JObject { | |
["gzipBody"] = decompressedData, | |
}.ToString()); | |
return response; | |
} | |
private string DecompressGzip(byte[] compressedData) | |
{ | |
using (MemoryStream compressedStream = MemoryStream(compressedData)) | |
using (GZipStream gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) | |
using (MemoryStream decompressedStream = new MemoryStream()) | |
{ | |
gzipStream.CopyTo(decompressedStream); | |
return Encoding.UTF8.GetString(decompressedStream.ToArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment