Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Error Opening Compressed DataSet

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19371 |

    Hi,

    I am trying to save a DataSet to disk using the CompressedStream from the Xceed Zip for .NET library.

    The saving seemes to be working properly and results in a much smaller file than if saved normally. However whenever I try to open the file I get the following exception:

    Xceed.Compression.CompressionException: Expected end of stream not found

    The following simple code illustrates this error (which is raised on the ds.ReadXml(compressedStream); line)

    // Create the DataSet and add some dummy data
    DataSet ds = new DataSet(“My DataSet”);
    DataTable dt = new DataTable(“My DataTable”);
    dt.Columns.Add(“Name”, typeof(string));

    DataRow r = dt.NewRow();
    r[0] = “Test”;
    dt.Rows.Add(r);

    // Save the dataset to disk
    using(FileStream fs = new FileStream(FILE_NAME, FileMode.Create))
    {
    CompressedStream compressedStream = new CompressedStream(fs);

    ds.WriteXml(compressedStream);
    }

    using(FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read))
    {
    CompressedStream compressedStream = new CompressedStream(fs);

    try
    {
    ds.ReadXml(compressedStream);
    }
    catch(CompressionException ex)
    {
    Debug.Fail(ex.ToString());

    throw ex;
    }
    }

    Any ideas how I can get this working?

    Many thanks,

    Chris

    Imported from legacy forums. Posted by chrisb (had 2574 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello Chris.

    You are not closing the CompressedStream. You must always close it, as it will write final data to the inner stream. Change your “using” blocks like this:

    <code>// Save the dataset to disk
    using(FileStream fs = new FileStream(FILE_NAME, FileMode.Create))
    {
    using( CompressedStream compressedStream = new CompressedStream(fs) )
    {
    ds.WriteXml(compressedStream);
    }
    }</code>

    Be aware that CompressedStream closes the innerstream upon closing itself. If you don’t want it to close the innerstream, you can set the Transient property to “true”. Calling Close more than once on a stream is safe.

    Same thing applies to your “reading” block, though it does not have impact on the result.

    Imported from legacy forums. Posted by Martin (had 113 views)

    User (Old forums)
    Member
    Post count: 23064

    Great, thanks that did the trick!

    Imported from legacy forums. Posted by chrisb (had 3438 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.