|
-
Sep 11th, 2008, 11:30 PM
#1
Thread Starter
Fanatic Member
[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
-
Sep 12th, 2008, 12:11 AM
#2
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.
-
Sep 12th, 2008, 12:20 AM
#3
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
-
Sep 12th, 2008, 01:23 AM
#4
Thread Starter
Fanatic Member
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!
-
Sep 12th, 2008, 01:27 AM
#5
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.
-
Sep 12th, 2008, 01:53 AM
#6
Thread Starter
Fanatic Member
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.
-
Sep 12th, 2008, 02:35 AM
#7
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.
-
Sep 12th, 2008, 02:46 AM
#8
Thread Starter
Fanatic Member
Re: Decompression doesnt work properly
Thank you! your time is respected
-
Sep 12th, 2008, 10:10 AM
#9
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.
-
Sep 12th, 2008, 12:44 PM
#10
Thread Starter
Fanatic Member
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!
-
Sep 12th, 2008, 07:52 PM
#11
Re: Decompression doesnt work properly
 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.
-
Sep 12th, 2008, 11:52 PM
#12
Hyperactive Member
Re: Decompression doesnt work properly
 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!
-
Sep 13th, 2008, 12:43 AM
#13
Re: Decompression doesnt work properly
 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.
-
Sep 13th, 2008, 04:40 AM
#14
Thread Starter
Fanatic Member
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
-
Sep 13th, 2008, 05:09 AM
#15
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?
-
Sep 13th, 2008, 05:35 AM
#16
Thread Starter
Fanatic Member
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
-
Sep 13th, 2008, 05:43 AM
#17
Thread Starter
Fanatic Member
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...
-
Sep 13th, 2008, 05:49 AM
#18
Re: Decompression doesnt work properly
Did you read post #9? I changed the code and you're still using the old version.
-
Sep 14th, 2008, 01:20 AM
#19
Thread Starter
Fanatic Member
[RESOLVED]Decompression doesnt work properly
Thank you ! I didnt notice that there was much a change. But yes, the changed code worked for me.
-
Sep 14th, 2008, 05:00 AM
#20
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.
-
Sep 14th, 2008, 07:50 AM
#21
Thread Starter
Fanatic Member
Re: [RESOLVED] Decompression doesnt work properly
Hey Sorry dude...
I didn't realize the small thing. But I am thankful much
Vijay S
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|