Create a Zip file from memorystream – save it in memory – then save to users system IN ASP.NET

4

Category : ASP.Net, VB.Net

Building on my last post (http://toddvance.com/?p=231), we now know how to get data from a datatable and save it to a memoryStream.
To take this one step further, lets say we wanted to create a Zip file out of that data BUT didnt want to save it to our server for download. Let’s says we wanted to keep save it to memory and then out to the user of the site.

To do this, we first need a handy library available at codeplexhere : http://www.codeplex.com/DotNetZip – it’s called DotNetZip and provides some handy features for saving into zip format.

Stick that dll in your bin and reference it  ( Imports Ionic.Utils.Zip ).

Now – from the last post, we have written the datatable to a memstream and then using a stringReader created a string out of that memoryStream.

Dim sr As New StreamReader(ms)
        ms.Seek(0, SeekOrigin.Begin)
        Dim mystring As String = sr.ReadToEnd
        sr.Close()

So now we have a string – from here we create a NEW memoryStream (we will need that to eventually write the file out from the browser) then using our new library, create a ZIP FILE that uses the memoryStream.

            Dim memStr As MemoryStream = New MemoryStream
            Dim Zip As ZipFile = New ZipFile(memStr)
            Zip.AddFileFromString("myDataTable.txt", "", myString)
            Zip.Save()
 
            'Set memoryStream to beginning position
            memStr.Position = 0
 
            'Convert the Memory Stream into a Byte Array that we can stream to the browser
            'send the memstr to the byte array by the .toArray
            Dim byteArray() As Byte
            byteArray = memStr.ToArray
            With HttpContext.Current.Response
                .ClearContent()
                .ClearHeaders()
                .AddHeader("Content-Disposition", "attachment;filename=ResearchExport.zip")
                .ContentType = "application/x-zip-compressed"
                .BinaryWrite(byteArray)
                .Flush()
                .Close()
            End With

This could be extended to allow people to save data they are viewing in your datagrid to a zip-text on their own computer….very nice.

Writing a DataTable to a MemoryStream and then out to a TextBox

Category : VB.Net

Here is an example of writing a datatable into a direct Memorystream and then writing that out to a textBox — this can all be done from an .aspx page and does not require a file to be involved.

 
        'CREATE A DATATABLE AND FILL IT WITH SOME DATA
        Dim dt As DataTable = New DataTable
        Dim dcID As New DataColumn("ID")
        Dim dcName As New DataColumn("Name")
 
        dt.Columns.Add(dcID)
        dt.Columns.Add(dcName)
 
        Dim row1, row2, row3 As DataRow
 
        row1 = dt.NewRow()
        row1.Item("ID") = "1232444"
        row1.Item("Name") = "Todd Eric Vance"
        dt.Rows.Add(row1)
 
        row2 = dt.NewRow()
        row2.Item("ID") = "154656232444"
        row2.Item("Name") = "Tod5454d Eric gggVance"
        dt.Rows.Add(row2)
 
        row3 = dt.NewRow()
        row3.Item("ID") = "1232444"
        row3.Item("Name") = "Tgfgfg fggfric Vyttance"
        dt.Rows.Add(row3)
 
 
        'CREATE MEMORYSTREAM AND STREAMWRITER
        Dim ms As MemoryStream = New MemoryStream
        Dim sw As StreamWriter = New StreamWriter(ms)
 
        'WRITE COLUMN NAMES TO STREAM
        Dim iColCount As Integer = dt.Columns.Count
 
        For i As Integer = 0 To iColCount - 1
 
            sw.Write(dt.Columns(i))
 
            If i < iColCount - 1 Then sw.Write(",")
 
        Next
 
        sw.WriteLine()
 
        'WRITE ROWS TO STREAM
        For Each dr As DataRow In dt.Rows
 
            For i As Integer = 0 To iColCount - 1
 
                If Not dr(i) Is DBNull.Value Then sw.Write(dr(i).ToString())
 
                If i < iColCount - 1 Then sw.WriteLine()
 
            Next
 
        Next
 
        sw.WriteLine()
 
        sw.Flush()
 
        'OPEN A READER - SET THE MEMORYSTREAM TO THE BEGINNING
        'WRITE IT TO A TEXTBOX
        Dim sr As New StreamReader(ms)
 
        ms.Seek(0, SeekOrigin.Begin)
 
        Dim mystring As String = sr.ReadToEnd
 
        tb1.Text = mystring
 
        sr.Close()