Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

XML compression and decompression Expand / Collapse
Author
Message
Posted Sunday, June 8, 2008 6:05 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14, 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?


Post #513491
Posted Monday, June 9, 2008 1:12 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, December 15, 2014 9:36 AM
Points: 652, Visits: 1,901
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.
Post #513561
Posted Monday, June 9, 2008 1:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 9, 2013 8:07 AM
Points: 1,220, Visits: 2,614
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
Post #513564
Posted Monday, June 9, 2008 12:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14, 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?
Post #513923
Posted Monday, June 9, 2008 12:32 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 9, 2013 8:07 AM
Points: 1,220, Visits: 2,614
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
Post #513928
Posted Monday, June 9, 2008 12:38 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, December 4, 2014 7:52 AM
Points: 9,294, Visits: 9,495
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."
Post #513933
Posted Monday, June 9, 2008 1:35 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 15, 2009 12:32 AM
Points: 14, 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)).

Post #513972
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse