I have an issue which I am banging my head against, I am trying to decompress text in this format:
eJx7v3t/QWJxcXl+UQoAJ94F3Q==
The problem I am having is that it works awesome on this site: http://www.unit-conversion.info/texttools/compress/
But I can't seem to get it to work with C#, I have tried Gzip and Zip, but it they both throw invalid data errors.
using (Stream fs = GenerateStreamFromString("eJx7v3t/QWJxcXl+UQoAJ94F3Q=="))
{
using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read))
{
//Do stuff
}
}
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
it thows an error on the ZipArchive line for the invalid data, it should decompress to "password" but I am unsure why it wont work.
If anyone knows of why or another library that will work, I would love to know!
Thanks!
EDIT
I tried the LZW algorithm with no luck, I figured it was zip because the header stated it was gzipped, but I am not sure how the data is compressed due to lack of documentation.
here is my LZW example code.
byte[] decodedBytes = Convert.FromBase64String("eJx7v3t/QWJxcXl+UQoAJ94F3Q==");
String text = System.Text.Encoding.UTF8.GetString(decodedBytes);
SharpLZW.LZWDecoder test = new SharpLZW.LZWDecoder();
string testval = test.Decode(text);
Decode is where I get the error, I tried with and without Base64 conversion, also with every type of encoding I could think of.
Any ideas?
That is a Base-64 encoding of a zlib stream, not gzip, nor zip. You can use zlib to decode it. It decompresses to ef bb bf 70 61 73 73 77 6f 72 64
. (The last eight bytes are "password".)
A quick perusal of the documentation indicates that .NET doesn't have a zlib decoder. You could write your own zlib header and trailer processing code using RFC 1950, and then the DeflateStream class to decompress the raw compressed data. Though you probably shouldn't use .NET for compression.
I would recommend looking at DotNetZip.