Hello,
I tried to compress a byte array in C# using Zip Library, but it seems I a missing something because every time I call compress sending a byte array parameter, I cannot decompress it correctly please see code as follows to compress
and the following is how I decompress it back and the function will return byte array as follows:
Thanks,
Levi
I tried to compress a byte array in C# using Zip Library, but it seems I a missing something because every time I call compress sending a byte array parameter, I cannot decompress it correctly please see code as follows to compress
private
string
Compress(
byte
[] data)
{
MemoryStream memoryStream =
new
MemoryStream();
ZipCompression method = ZipCompression.Default;
ZipOutputStream zipOutputStream =
new
ZipOutputStream(memoryStream, method);
StreamWriter writer =
new
StreamWriter(zipOutputStream);
writer.Write(data
);
writer.Flush();
return
Convert.ToBase64String(memoryStream.ToArray());
}
and the following is how I decompress it back and the function will return byte array as follows:
private
byte
[] UnCompress(
string
str)
{
MemoryStream memoryStream =
new
MemoryStream(Convert.FromBase64String(str));
ZipInputStream input =
new
ZipInputStream(memoryStream);
StreamReader reader =
new
StreamReader(input);
return
memoryStream.ToArray();
}
Thanks,
Levi