Results 1 to 21 of 21

Thread: [RESOLVED] Decompression doesnt work properly

  1. #1

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Function CompressData(ByVal text As String) As Byte()
    2.     Using store As New MemoryStream
    3.         Using compressor As New GZipStream(store, CompressionMode.Compress)
    4.             Using writer As New StreamWriter(compressor, Encoding.UTF7)
    5.                 writer.Write(text)
    6.                 Return store.GetBuffer()
    7.             End Using
    8.         End Using
    9.     End Using
    10. End Function
    11.  
    12. Private Function DecompressData(ByVal data As Byte()) As String
    13.     Using store As New MemoryStream(data)
    14.         Using decompressor As New GZipStream(store, CompressionMode.Decompress)
    15.             Using reader As New StreamReader(decompressor, Encoding.UTF7)
    16.                 Return reader.ReadToEnd()
    17.             End Using
    18.         End Using
    19.     End Using
    20. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    Re: Decompression doesnt work properly

    Thank you! your time is respected

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decompression doesnt work properly

    I changed the code to the following and it worked for me, TextBox to TextBox:
    vb.net Code:
    1. Private Function CompressData(ByVal text As String) As Byte()
    2.     Using store As New MemoryStream
    3.         Using compressor As New GZipStream(store, CompressionMode.Compress)
    4.             Using writer As New StreamWriter(compressor)
    5.                 writer.Write(text)
    6.             End Using
    7.         End Using
    8.  
    9.         Return store.GetBuffer()
    10.     End Using
    11. End Function
    12.  
    13. Private Function DecompressData(ByVal data As Byte()) As String
    14.     Using store As New MemoryStream(data)
    15.         Using decompressor As New GZipStream(store, CompressionMode.Decompress)
    16.             Using reader As New StreamReader(decompressor)
    17.                 Return reader.ReadToEnd()
    18.             End Using
    19.         End Using
    20.     End Using
    21. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Imports System.IO
    2. Imports System.IO.Compression
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Me.RichTextBox2.Rtf = Me.DecompressData(Me.CompressData(Me.RichTextBox1.Rtf))
    8.     End Sub
    9.  
    10.     Private Function CompressData(ByVal text As String) As Byte()
    11.         Using store As New MemoryStream
    12.             Using compressor As New GZipStream(store, CompressionMode.Compress)
    13.                 Using writer As New StreamWriter(compressor)
    14.                     writer.Write(text)
    15.                 End Using
    16.             End Using
    17.  
    18.             Return store.GetBuffer()
    19.         End Using
    20.     End Function
    21.  
    22.     Private Function DecompressData(ByVal data As Byte()) As String
    23.         Using store As New MemoryStream(data)
    24.             Using decompressor As New GZipStream(store, CompressionMode.Decompress)
    25.                 Using reader As New StreamReader(decompressor)
    26.                     Return reader.ReadToEnd()
    27.                 End Using
    28.             End Using
    29.         End Using
    30.     End Function
    31.  
    32. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    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!

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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:
    1. Friend Function CompressBytes(ByVal text As String) As Byte()
    2.         Try
    3.             Using store As New IO.MemoryStream
    4.                 Using compressor As New System.IO.Compression.GZipStream(store, IO.Compression.CompressionMode.Compress)
    5.                     Using writer As New IO.StreamWriter(compressor)
    6.                         store.Position = 0
    7.                         writer.Write(text)
    8.                         Return store.GetBuffer()
    9.                     End Using
    10.                 End Using
    11.             End Using
    12.         Catch ex As Exception
    13.             DE(ex)
    14.         End Try
    15.     End Function
    16.     Friend Function DecompressBytes(ByVal bytes() As Byte) As String
    17.         Try
    18.             Using store As New IO.MemoryStream(bytes)
    19.                 Using decompressor As New IO.Compression.GZipStream(store, IO.Compression.CompressionMode.Decompress)
    20.                     store.Position = 0
    21.                     Using reader As New IO.StreamReader(decompressor)
    22.                         Return reader.ReadToEnd
    23.                     End Using
    24.                 End Using
    25.             End Using
    26.         Catch ex As Exception
    27.             DE(ex)
    28.         End Try
    29.     End Function

  17. #17

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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...

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decompression doesnt work properly

    Did you read post #9? I changed the code and you're still using the old version.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    [RESOLVED]Decompression doesnt work properly

    Thank you ! I didnt notice that there was much a change. But yes, the changed code worked for me.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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
  •  



Click Here to Expand Forum to Full Width