Skip to content

Commit fba99b6

Browse files
feat: additional method GetOriginalFileName
1 parent d69dde8 commit fba99b6

File tree

1 file changed

+28
-1
lines changed
  • application/DotNetMeshClient/NHS.Mesh.Client/Helpers/ContentHelpers

1 file changed

+28
-1
lines changed

application/DotNetMeshClient/NHS.Mesh.Client/Helpers/ContentHelpers/GZIPHelpers.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace NHS.MESH.Client.Helpers.ContentHelpers;
44

55
public class GZIPHelpers
66
{
7-
public static byte[] CompressBuffer(byte[] data)
7+
public static byte[] CompressBuffer(byte[] data)
88
{
99
using (var compressedStream = new MemoryStream())
1010
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
@@ -25,4 +25,31 @@ public static byte[] DeCompressBuffer(byte[] byteArray)
2525
}
2626
}
2727

28+
public static string? GetOriginalFileName(byte[] gzipBuffer)
29+
{
30+
using (var compressedStream = new MemoryStream(gzipBuffer))
31+
using (var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
32+
{
33+
// Read the GZIP header
34+
byte[] header = new byte[10];
35+
compressedStream.Read(header, 0, 10);
36+
37+
// Check if the original file name is present
38+
if ((header[3] & 0x08) != 0)
39+
{
40+
using (var reader = new BinaryReader(compressedStream))
41+
{
42+
var fileNameBytes = new List<byte>();
43+
byte b;
44+
while ((b = reader.ReadByte()) != 0)
45+
{
46+
fileNameBytes.Add(b);
47+
}
48+
return System.Text.Encoding.UTF8.GetString(fileNameBytes.ToArray());
49+
}
50+
}
51+
}
52+
return null;
53+
}
54+
2855
}

0 commit comments

Comments
 (0)