From: Marc Gravell on
I should have been specific; the memory stream (MS) needs to be reset - i.e.

// ...blah
dfs.Close();

MS.Position = 0; // reset the memory stream so we can read our glorious
compressed data

//DeCompressing from Memory
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
// blah...


=====

Note also that you wight want to wrap each deflating stream in a "using"
construct to ensure they are always disposed; and *technically* even
MemoryStream is disposable, so you might want to be "using" that as well -
but I don't want to get into *that* argument again, so do whatever you want
;-p

Marc


From: Asaf on
Hi Marc,

Now I am getting the error: "Cannot access a closed Stream."
Here is the complete code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();


dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Compress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(dfs);
dfs.Close();

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(dfsDecompress);

textBox1.Text = dsAfterDecompress.Tables[0].Rows.Count.ToString();

Regards,
Asaf

"Marc Gravell" wrote:

> I should have been specific; the memory stream (MS) needs to be reset - i.e.
>
> // ...blah
> dfs.Close();
>
> MS.Position = 0; // reset the memory stream so we can read our glorious
> compressed data
>
> //DeCompressing from Memory
> DeflateStream dfsDecompress = new DeflateStream(MS,
> CompressionMode.Decompress, false);
> // blah...
>
>
> =====
>
> Note also that you wight want to wrap each deflating stream in a "using"
> construct to ensure they are always disposed; and *technically* even
> MemoryStream is disposable, so you might want to be "using" that as well -
> but I don't want to get into *that* argument again, so do whatever you want
> ;-p
>
> Marc
>
>
>
From: Jon Skeet [C# MVP] on
Asaf wrote:
> Now I am getting the error: "Cannot access a closed Stream."

Use MemoryStream.GetBuffer() to get the byte array, and create a new
MemoryStream using that byte array, specifying the length as the Length
of the old stream.

Apologies if the names are slightly wrong - I'm not on a Windows box at
the moment to check.

Jon

From: Marc Gravell on
OK - look at the last parameter to the DeflateStream ctor; it is
"leaveOpen"; if you change this to true, it will leave your base-stream
open, and life will be good. By default responsibility for the base stream
is handed to the wrapper (or reader/writer) - but in this case the
base-stream must outlive the wrapper - so we need to change this setting.

The following (similar to yours, but using serialization as a simpler test)
works "as is":

static void Main(string[] args) {

string final, original = "Some object that I want to compress,
in my case a string via serialization - but could be anything that writes to
a stream";

System.Runtime.Serialization.IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Console.WriteLine(original);
using (MemoryStream baseStream = new MemoryStream()) {
using (DeflateStream compress = new
DeflateStream(baseStream, CompressionMode.Compress, true)) {
formatter.Serialize(compress, original);
}
WriteArray("Compressed", baseStream.ToArray());
baseStream.Position = 0;
using (DeflateStream decompress = new
DeflateStream(baseStream, CompressionMode.Decompress, true)) {
final = (string) formatter.Deserialize(decompress);
}
}
Console.WriteLine(final);
}

static void WriteArray(string caption, byte[] data) {
string underline = new string('=', caption.Length);
Console.WriteLine(underline);
Console.WriteLine(caption);
Console.WriteLine(data.Length + " byte(s)");
Console.WriteLine(underline);
foreach (byte b in data) {
Console.Write(b.ToString("x2") + " ");
}
Console.WriteLine();
}


From: Asaf on
Hi,

Since DeflateStream is owning the MemoryStream when compressing, the
argument "LeaveOpen" must be set to true, otherwise DeflateStream will close
(and flush) the MemoryStream automatically after writing the compressed bytes
to it.
Setting false to "LeaveOpen" argument is a must when using FileStream to
close the file that DeflateStream will write the bytes to it.

Now I am able to compress and decompress to MemoryStream :-)

Thanks you all for your help and share of knowledge!

Regards,
Asaf


"Marc Gravell" wrote:

> OK - look at the last parameter to the DeflateStream ctor; it is
> "leaveOpen"; if you change this to true, it will leave your base-stream
> open, and life will be good. By default responsibility for the base stream
> is handed to the wrapper (or reader/writer) - but in this case the
> base-stream must outlive the wrapper - so we need to change this setting.
>
> The following (similar to yours, but using serialization as a simpler test)
> works "as is":
>
> static void Main(string[] args) {
>
> string final, original = "Some object that I want to compress,
> in my case a string via serialization - but could be anything that writes to
> a stream";
>
> System.Runtime.Serialization.IFormatter formatter = new
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
> Console.WriteLine(original);
> using (MemoryStream baseStream = new MemoryStream()) {
> using (DeflateStream compress = new
> DeflateStream(baseStream, CompressionMode.Compress, true)) {
> formatter.Serialize(compress, original);
> }
> WriteArray("Compressed", baseStream.ToArray());
> baseStream.Position = 0;
> using (DeflateStream decompress = new
> DeflateStream(baseStream, CompressionMode.Decompress, true)) {
> final = (string) formatter.Deserialize(decompress);
> }
> }
> Console.WriteLine(final);
> }
>
> static void WriteArray(string caption, byte[] data) {
> string underline = new string('=', caption.Length);
> Console.WriteLine(underline);
> Console.WriteLine(caption);
> Console.WriteLine(data.Length + " byte(s)");
> Console.WriteLine(underline);
> foreach (byte b in data) {
> Console.Write(b.ToString("x2") + " ");
> }
> Console.WriteLine();
> }
>
>
>