I'm using RserveCLI2 on C#. I tried to pass gzip byte array generated by R to C# so that C# can decompress it. However I'm not able to get it work. I did some comparison of the byte array generated by both R and C# when doing gzip compression of string "ABCDEF". Here are the results.
# R gzip compression command and result
> as.numeric(memCompress(charToRaw("ABCDEF"),"gzip"))
[1] 120 156 115 116 114 118 113 117 3 0 5 126 1 150
# C-sharp gzipstream compress result
byte[] data1 = Encoding.ASCII.GetBytes("ABCDEF");
var memoryStream = new MemoryStream();
using (var gz = new GZipStream(memoryStream, CompressionMode.Compress))
{
gz.Write(data1, 0, data1.Length);
}
memoryStream.ToArray()
{byte[26]}
[0]: 31
[1]: 139
[2]: 8
[3]: 0
[4]: 0
[5]: 0
[6]: 0
[7]: 0
[8]: 4
[9]: 0
[10]: 115
[11]: 116
[12]: 114
[13]: 118
[14]: 113
[15]: 117
[16]: 3
[17]: 0
[18]: 105
[19]: 254
[20]: 118
[21]: 187
[22]: 6
[23]: 0
[24]: 0
[25]: 0
I did some study and gzip byte array should starts with 31 and 139 (Hexadecimal 1F and 8B). In this case seems like C# byte array is correct. So I wonder why is it R byte array so different compared to C#? Is there any way that make R generate byte array similar to C#? Thanks.
This should provide you with the results you need. It's a modified version of a function in the now defunct
Rcompression
package. I've found the built inmem*
functions to be nigh useless:You can see that it produces saner results:
If there's sufficient demand, I can resurrect and make a modern version of the
Rcompression
package (I've had to resurrect it for myself and work projects but would be glad to have a more publicly available version that would be easier to use than inline C code).I threw it into a tiny package that you can install with:
Now, all you have to do is:
There will be a much better version of
mem_inflate()
later this week.