Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET How to zip a dot net 2.0 in-memory datatable or string?

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

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

    Imported from legacy forums. Posted by dv (had 2191 views)

    User (Old forums)
    Member
    Post count: 23064

    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 ) );

         }

    Imported from legacy forums. Posted by André (had 1460 views)

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