Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML compression and decompression


XML compression and decompression

Author
Message
jagadish_sds
jagadish_sds
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 45
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?
David McKinney
David McKinney
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 2090
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.
KenSimmons
KenSimmons
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1414 Visits: 2614
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

Ken Simmons
http://twitter.com/KenSimmons
jagadish_sds
jagadish_sds
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 45
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?
KenSimmons
KenSimmons
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1414 Visits: 2614
The byte array is just a string of compressed data. You should be able to store it in a varchar(max) column.

Ken Simmons
http://twitter.com/KenSimmons
RBarryYoung
RBarryYoung
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11742 Visits: 9518
If the compressed stream returned is really binary data (and it usually is) then it should be stored as VarBinary(..) or Binary(..).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
jagadish_sds
jagadish_sds
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 45
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)).
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search