[RESOLVED] Decompression doesnt work properly
Hi,
I am inserting BLOBS into my database(basically rich text), to enhance the network speed, I am compressing them before insert. However when I decompress it, I am not getting the content... see the following logic....
I get a "file format invalid" error
Code:
Friend Function CompressBytes(ByVal rtfString As String) As Byte()
Try
Dim Compressor As System.IO.Compression.GZipStream
Dim EC As New System.Text.UTF8Encoding
Dim bytes() As Byte=EC.GetBytes(rtfString)
Dim ms As New System.IO.MemoryStream
ms.Write(bytes, 0, bytes.Length)
ms.Position = 0
Compressor = New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Compress, True)
Compressor.Write(bytes, 0, bytes.Length)
ms.ToArray()
Catch ex As Exception
DE(ex)
End Try
End Function
Friend Function DecompressBytes(ByVal bytes() As Byte) As String
Try
Dim ms As New System.IO.MemoryStream
Dim EC As New System.Text.UTF8Encoding
ms.Write(bytes, 0, bytes.Length)
ms.Position = 0
Dim DeCompressor As IO.Compression.GZipStream = New System.IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress, True)
Dim offset As Integer = 0
Dim totalbytes As Integer = 0
Dim smallBuffer(100) As Byte
Dim bytesread As Integer = 0
bytesread = DeCompressor.Read(smallBuffer, 0, 100)
ms.Position = 0
While (True)
bytesread = DeCompressor.Read(smallBuffer, 0, 100)
If (bytesread = 0) Then
Exit While
End If
offset += bytesread
totalbytes += bytesread
End While
Dim byt(totalbytes) As Byte
ms.Position = 0
DeCompressor.Read(byt, 0, totalbytes)
ms.Close()
ms.Dispose()
Return EC.GetString(byt)
Catch ex As Exception
DE(ex)
End Try
End Function
Thanks
Vijay S
Re: Decompression doesnt work properly
You shouldn't have to mess around with the raw bytes. A GZipStream is a Stream. How do you usually write text to a stream? With a StreamWriter. How do you usually read text from a stream? With a StreamReader. Usually you're reading and writing files, so the StreamReader and StreamWrite sit on top of a FileStream. They don't care what type of stream it is though.
You can simply sit a StreamWriter on top of the GZipStream on top of the MemoryStream. You write text using the StreamWriter and get compressed bytes from the MemoryStream. If the direction of the GZipStream is reversed then you can sit a StreamReader on top of the GZipStream on top of the MemoryStream. You can then read the entire text in one go.
Re: Decompression doesnt work properly
I haven't tested it but I'm fairly sure that the following will do the job:
vb.net Code:
Private Function CompressData(ByVal text As String) As Byte()
Using store As New MemoryStream
Using compressor As New GZipStream(store, CompressionMode.Compress)
Using writer As New StreamWriter(compressor, Encoding.UTF7)
writer.Write(text)
Return store.GetBuffer()
End Using
End Using
End Using
End Function
Private Function DecompressData(ByVal data As Byte()) As String
Using store As New MemoryStream(data)
Using decompressor As New GZipStream(store, CompressionMode.Decompress)
Using reader As New StreamReader(decompressor, Encoding.UTF7)
Return reader.ReadToEnd()
End Using
End Using
End Using
End Function
Re: Decompression doesnt work properly
Hi,
Thank you for the response... However the weird thing I see here is , If I send a rtf text containing image or image+text, its properly compressed and inserted into table. and while retrieving it is properly decompressed and shown in the RTF textbox
But if it contains only text, it is compressed and inserted... while retrieving...
reader.ReadToEnd is retrieving 0 length string.. not sure why!
Re: Decompression doesnt work properly
Is there a particular reason you chose UTF7 encoding? If not then just use Default encoding and see if that fixes it.
Re: Decompression doesnt work properly
I was using UTF8, to keep it consistent with my database encoding... Nothing beyond that..
I tried using DefaultEncoding too... but in vain.
Re: Decompression doesnt work properly
I just whipped up that code quickly at work. I'll have a closer look when I get home and I can spend some time on it.
Re: Decompression doesnt work properly
Thank you! your time is respected
Re: Decompression doesnt work properly
I changed the code to the following and it worked for me, TextBox to TextBox:
vb.net Code:
Private Function CompressData(ByVal text As String) As Byte()
Using store As New MemoryStream
Using compressor As New GZipStream(store, CompressionMode.Compress)
Using writer As New StreamWriter(compressor)
writer.Write(text)
End Using
End Using
Return store.GetBuffer()
End Using
End Function
Private Function DecompressData(ByVal data As Byte()) As String
Using store As New MemoryStream(data)
Using decompressor As New GZipStream(store, CompressionMode.Decompress)
Using reader As New StreamReader(decompressor)
Return reader.ReadToEnd()
End Using
End Using
End Using
End Function
By closing the StreamWriter and GZipStream before reading the buffer from the MemoryStream you ensure that the two upper layers have both flushed any write buffers that may have been storing data that hadn't been written to the lowest layer.
Re: Decompression doesnt work properly
To my bad luck its not working for my Richtextbox! :(
But thank you verymuch for giving some easy direction.. I will try to get things work!
Re: Decompression doesnt work properly
Quote:
Originally Posted by sridharavijay
To my bad luck its not working for my Richtextbox! :(
But thank you verymuch for giving some easy direction.. I will try to get things work!
Then you're doing it wrong. I just tested and formatted text and an image copied from one RTB to another without issue. I created a new project and added two RichTextBoxes and a Button and then added this code:
vb.net Code:
Imports System.IO
Imports System.IO.Compression
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.RichTextBox2.Rtf = Me.DecompressData(Me.CompressData(Me.RichTextBox1.Rtf))
End Sub
Private Function CompressData(ByVal text As String) As Byte()
Using store As New MemoryStream
Using compressor As New GZipStream(store, CompressionMode.Compress)
Using writer As New StreamWriter(compressor)
writer.Write(text)
End Using
End Using
Return store.GetBuffer()
End Using
End Function
Private Function DecompressData(ByVal data As Byte()) As String
Using store As New MemoryStream(data)
Using decompressor As New GZipStream(store, CompressionMode.Decompress)
Using reader As New StreamReader(decompressor)
Return reader.ReadToEnd()
End Using
End Using
End Using
End Function
End Class
I then ran the project and fied up WordPad. I entered some text into WordPad and pasted in an image. I then formatted various portions of the text in various ways. I then copied the entire contents of WordPad into my first RTB and then clicked the Button on my form. Hey presto! The same contents appeared in the second RTB.
Re: Decompression doesnt work properly
Quote:
Originally Posted by sridharavijay
To my bad luck its not working for my Richtextbox! :(
But thank you verymuch for giving some easy direction.. I will try to get things work!
You know you could always just create a Textbox then change Multi Line to True. Then it basically is almost the same!:)
Re: Decompression doesnt work properly
Quote:
Originally Posted by VB6Learner
You know you could always just create a Textbox then change Multi Line to True. Then it basically is almost the same!:)
Except that it won't support formatted text or images.
Re: Decompression doesnt work properly
Hi... you took pains to test... however as I said before
the (image+text) works ; only text doesn't work (Note that in both the contexts it is rich text box only)
Thank you
Vijay -S
Re: Decompression doesnt work properly
The code I posted earlier works perfectly for me whether the RTB contains an image or just text. Did you test that code specifically?
Re: Decompression doesnt work properly
Well I am using PostgreSQL as backend... and here is the code I am using... let me also save it to a file and check it instead of table...
vb.net Code:
Friend Function CompressBytes(ByVal text As String) As Byte()
Try
Using store As New IO.MemoryStream
Using compressor As New System.IO.Compression.GZipStream(store, IO.Compression.CompressionMode.Compress)
Using writer As New IO.StreamWriter(compressor)
store.Position = 0
writer.Write(text)
Return store.GetBuffer()
End Using
End Using
End Using
Catch ex As Exception
DE(ex)
End Try
End Function
Friend Function DecompressBytes(ByVal bytes() As Byte) As String
Try
Using store As New IO.MemoryStream(bytes)
Using decompressor As New IO.Compression.GZipStream(store, IO.Compression.CompressionMode.Decompress)
store.Position = 0
Using reader As New IO.StreamReader(decompressor)
Return reader.ReadToEnd
End Using
End Using
End Using
Catch ex As Exception
DE(ex)
End Try
End Function
Re: Decompression doesnt work properly
You are right... it works on the fly... I am not sure why it isnt working in a DB scenario...
Re: Decompression doesnt work properly
Did you read post #9? I changed the code and you're still using the old version.
[RESOLVED]Decompression doesnt work properly
Thank you ! I didnt notice that there was much a change. But yes, the changed code worked for me. :thumb: :wave:
Re: [RESOLVED] Decompression doesnt work properly
I know it can be easy to overlook things at times but if you ask for help and people provide it, it's best to read what they've posted CAREFULLY. We both wasted time trying to find a solution that was already provided. I'm glad it worked out in the end though.
Re: [RESOLVED] Decompression doesnt work properly
Hey Sorry dude...
I didn't realize the small thing. But I am thankful much
Vijay S