Welcome to the Xceed Community Sign in | Join | Help
Community Search  

How to zip a dot net 2.0 in-memory datatable or string?

Sort Posts: Previous Next
  •  06-10-2008, 4:02 PM Post no. 12825

    How to zip a dot net 2.0 in-memory datatable or string?

    I would like to do the following all in memory without writing to disk on the client's machine.

    Zip an in-memory DataTable in .NET 2.0 (C#) to memory (not disk).  Then I'll send it ( string/byte/stream ..  whatever it's called) to our web service.

     I found some code to change the DataTable into a stream, so I think that's the first part. thanks!

     You can use a MemoryStream or a StringWriter with these overloads to keep it all in memory.

     

    string result;
    using (StringWriter sw = new StringWriter()) {
       dataTable.WriteXml(sw);
       result = sw.ToString();
    }

    If you don't actually need a string but read-only, process able XML, it's a better idea to use MemoryStream and XPathDocument:

    XPathDocument result;
    using (MemoryStream ms = new MemoryStream()) {
       dataTable.WriteXml(ms);
       ms.Position = 0;
       result = new XPathDocument(ms);
    }

  •  06-11-2008, 12:38 PM Post no. 12869 in reply to 12825

    Re: How to zip a dot net 2.0 in-memory datatable or string?

    You can use a MemoryFile to write the table to it, and use it to copy it to a zip archive in memory, and then use this archive to send it to the Web Service, via a stream for example.

     e.g.:

         AbstractFile dest = new MemoryFile( "ram", "DataTable" );

         dest.Create();

         using( Stream destStream = dest.OpenWrite( true ) )

         {

           string line = "Try with this text";

           System.Text.Encoding.ASCII.GetBytes( line );

           byte[] buffer = System.Text.Encoding.ASCII.GetBytes( line );

           destStream.Write( buffer, 0, buffer.Length );

         }

         AbstractFile memZip = new MemoryFile( "Ram", "archive.zip" );

         ZipArchive archive = new ZipArchive( memZip );

         dest.CopyTo( archive, true );

         using( Stream stream = memZip.OpenRead() )

         {

           //...stream it to the Web Service

         }

     

         //You can verify that it works with the code bellow

         ZippedFile zippedFile = new ZippedFile( memZip, "DataTable" );

         AbstractFile unzippedFile = new MemoryFile();

         zippedFile.CopyTo( unzippedFile, true );

         using( Stream stream = unzippedFile.OpenRead() )

         {

           byte[] buffer = new byte[ stream.Length ];

           stream.Read( buffer, 0, buffer.Length );

           Console.WriteLine( System.Text.Encoding.ASCII.GetString( buffer ) );

         }


    André
    Software Developer and Tech Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.