Bad Compressing / Decompressing ASCII string from memory.

Sep 22, 2013 at 6:38 PM
Hello,

I used this in order to compress a file :
var libPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "7-zip", "7z.dll");
SevenZip.SevenZipCompressor.SetLibraryPath(libPath);
SevenZip.SevenZipCompressor compressor = new SevenZipCompressor();
compressor.CompressFiles(@"C:\myTestFile.mdf", new string[] { @"C:\myTestFileCompressed.7z" });
With this, my file is compressed whitout problem. I can decompressed it.

Now...i would like to compress the same file, but, instead of compress directly the file, i would like to :
  • Read the file in a string. Yes, in a string, and not in a byte[].
  • Convert my string to byte[]
  • Compress the byte[] to another byte[].
Here is my try
string strToCompress = File.ReadAllText(@"C:\myTestFile.mdf");
SevenZipCompressor compressor = new SevenZipCompressor();
byte[] byteArrayToCompress = Encoding.ASCII.GetBytes(text);
MemoryStream stream = new MemoryStream(byteArrayToCompress);
MemoryStream streamOut = new MemoryStream();
compressor.CompressStream(stream, streamOut);
string strcompressed = Encoding.ASCII.GetString(streamOut.ToArray());
File.WriteAllText(@"C:\myfileCompressed.7z",strcompressed);
My problem is very simple :

If i compare the size produced by these 2 methods, it's 3 603 443 bytes vs 3 604 081 bytes.
In addition, i cannot uncompressed the file produced by the second method.

Maybe it's because i used ASCII encoding, but my file to compress is not a Text, it's a binary file.

Anyone could explain me how solving it please ? I need to read my file to a string and compress it. ( i don't want to read the file directly to a byte[]).

Thanks a lot,

Best regards,

Nixeus