Reputation: 4180
Problem
What did I try to do:
1. Python
def deflate_and_base64_encode( string_val ):
zlibbed_str = zlib.compress( string_val )
compressed_string = zlibbed_str[2:-1]
return base64.b64encode(compressed_string )
2. C#
public static string Inflate(string pythonZipped)
{
byte[] gzBuffer = Convert.FromBase64String(pythonZipped);
using (var ms = new MemoryStream())
{
ms.Write(gzBuffer, 0, gzBuffer.Length);
var buffer = new byte[ms.Length];
ms.Position = 0;
using (var infS = new DeflateStream(ms,Ionic.Zlib.CompressionMode.Decompress, true))
{
infS.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
But get some weird results for example:
Python.deflate_and_base64_encode('1'*1000)
Results in MzQcBaNgFAx3AABQpr8=
C#.Inflate("MzQcBaNgFAx3AABQpr8")
Results in 11111111111111
instead of 1000 ones
Can you explain what I am doing wrong or give me other way to acheive what I am tring to do?
Upvotes: 1
Views: 1384
Reputation: 358
I had the same problem. I solved it.
"var buffer = new byte[ms.Length];" causes that problem.
The buffer size is too small.
Increase buffer size like this "var buffer = new byte[4096];" or "var buffer = new byte[ms.Length*1024];".
Upvotes: 1
Reputation: 937
The problem begins when 2 characters from the beginning and one from the end are chopped off. compressed_string = zlibbed_str[2:-1]
That line of code destroys header information in the first 2 characters, and even an immediate zlib.decompress can't make sense of what's left. (My guess: C#'s zlib implementation does its best to reconstruct what's left of the stream without a valid header. Python's, as you'll see below, is more strict.)
>>> import zlib
>>> z = zlib.compress('1'*1000)
>>> c = z[2:-1]
>>> zlib.decompress(z)
'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
>>> zlib.decompress(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
Upvotes: 1