XML compression and decompression

  • I am getting Source as XML (more than 5MB size) and It has to compress and store it in DataBase and while getting back from the database, it has to decompress and return the XML data.

    Is there any functions available in sql server2005? OR Can I do it in code behind (C#) itself?

  • You should get a certain amount of compression if you use a typed xml column i.e. define a schema for your xml.

    It won't be very compressed, but better than nothing. However the xml in output will be equivalent but slightly different (in terms of whitespace and certain other differences.)

    Otherwise for real compression which conserves the file as was, you'll have to do it in c# (it seems to me.)

    Hope this helps,

    David.

  • If you do it in code, you could convert the xml to a dataset and use the following compression functions.

    http://www.icsharpcode.net/OpenSource/SharpZipLib/

    http://www.icsharpcode.net/OpenSource/SD/forum/forum.asp?FORUM_ID=16

    Imports ICSharpCode.SharpZipLib.GZip

    Imports System.IO

    Public Function CompressDataset(ByVal ds As DataSet)

    Dim BaseStream As System.IO.MemoryStream = New System.IO.MemoryStream

    Dim OutMemoryStream As System.IO.Stream = New GZipOutputStream(BaseStream)

    Dim btary() As Byte

    ds.WriteXml(OutMemoryStream, XmlWriteMode.WriteSchema)

    OutMemoryStream.Close()

    btary = BaseStream.ToArray()

    BaseStream.Close()

    Return btary

    End Function

    Public Function DecompressDataset(ByVal btary() As Byte)

    Dim InStream As Stream = New System.IO.MemoryStream

    InStream.Write(btary, 0, btary.Length)

    InStream.Position = 0

    Dim CompStream As Stream = New GZipInputStream(InStream)

    Dim fs As Stream = New System.IO.MemoryStream

    Dim size As Integer = 2048

    Dim writedata As Byte() = New Byte(2048) {}

    While True

    size = CompStream.Read(writedata, 0, size)

    If size > 0 Then

    fs.Write(writedata, 0, size)

    Else

    Exit While

    End If

    End While

    fs.Position = 0

    Dim ds As New DataSet

    ds.ReadXml(fs, XmlReadMode.ReadSchema)

    CompStream.Close()

    fs.Flush()

    fs.Close()

    InStream.Close()

    return ds

    End Function

    End Class

  • This is fine.

    But How can I store this compressed data in Database. Database column either ntext or nvarchar(max).

    Doubt is now How to store byte array in Database?

  • The byte array is just a string of compressed data. You should be able to store it in a varchar(max) column.

  • If the compressed stream returned is really binary data (and it usually is) then it should be stored as VarBinary(..) or Binary(..).

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • My doubt how to Convert Byte Array to string

    I can not pass directly Byte Array to database.

    because input to database is string (i.e., nvarchar(max)).

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply