DeflateStream & MemoryStream?

G

Guest

Hi,

I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The 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(ms, XmlWriteMode.WriteSchema);

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


Thanks for any help,
Asaf
 
G

Guest

Asaf,
your code seems to give the impression that you think a dataset,after being
compressed ,is "still a dataset". It is not.

Also, you will be better served by using the binary format which is much
more compact than xml.
Here is a short article with code that illustrates the technique:

http://www.eggheadcafe.com/articles/20060220.asp

Peter
 
J

Jon Skeet [C# MVP]

Asaf said:
I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The 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(ms, XmlWriteMode.WriteSchema);

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

You're never using the deflating or inflating streams. You should be
calling
WriteXml (dfs, XmlWriteMode.WriteSchema).

You should then Flush dfs before using the MemoryStream. After that,
you should be able to call ReadXml but with dfsDecompress rather than
MS.

I'm slightly concerned that without having closed the DeflateStream to
start with, there may be problems. You may need to close dfs instead of
just flushing it, then create a new MemoryStream with the byte array
from the first MemoryStream, just so it knows where it ends properly.
 
G

Guest

Hi Peter, Hi Jon,

Thanks for your reply.

Let's start again :)


The code below is working fine when using FileStream:

//Compress to and save the file to disk
FileStream msf = new
FileStream(@"C:\VSProjects2005\ZIP_Compress\TheXML.xmd",
FileMode.Create, FileAccess.Write);
dfs = new DeflateStream(msf, CompressionMode.Compress, false);
dsOrginal.WriteXml(dfs);

dfs.Close();
msf.Close();

MessageBox.Show("Compress to file, Done!");

//Read Compressed file from disk, decompress data and show rows
count for dataset
FileStream infile = new
FileStream(@"C:\VSProjects2005\ZIP_Compress\TheXML.xmd", FileMode.Open,
FileAccess.Read);
DeflateStream DefStream = new DeflateStream(infile,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(DefStream);
infile.Close();

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


Same code but using MemoryStream does not work:

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
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(dfsDecompress);

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

If using dfs.Close(); then I am getting the error:
"The base stream is not readable.
Parameter name: stream"

If dfs.Close(); is omitted then I am getting the error:
"Root element is missing" when trying to do:
dsAfterDecompress.ReadXml(dfsDecompress); my guess is because the Stream is
at the end Position but Position property can't be use with DeflateStream so
I can't rewind the DeflateStream to zero position before doing
dsAfterDecompress.ReadXml(dfsDecompress);

Help Please!

Regards,
Asaf
 
M

Marc Gravell

In the file code you use two separate streams, which means the position is
effictively reset between the two methods; in the second block you are using
a single stream, but are *not* resetting the position.

Just set Position = 0 before trying to read what you have written

Marc
 
M

Marc Gravell

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
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

Asaf said:
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
 
M

Marc Gravell

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();
}
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top