Results 1 to 3 of 3

Thread: [RESOLVED] [2005] GZipStream Decompress

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [RESOLVED] [2005] GZipStream Decompress

    Hi Guys,

    I must be having a moment ppl, for the life of me I can't figure out why this is occurring. Let me detail my problem; I wish to unzip a file and write its decompressed contents to another file on the hard disk. Not too complex right? Well....

    Here is my code:

    VB Code:
    1. Private Sub InflateData()
    2.  
    3.         Dim inData As System.IO.FileStream = Nothing
    4.         Dim outData As System.IO.FileStream = Nothing
    5.         Dim compressedData As System.IO.Compression.GZipStream = Nothing
    6.  
    7.         Dim buffer(1024 - 1) As Byte
    8.         Dim totalBytes As Integer = 0
    9.         Dim bytesRead As Integer = 0
    10.         Dim offset As Integer = 0
    11.  
    12.         Try
    13.             'Get zipped data file stream
    14.             inData = New System.IO.FileStream(My.Settings.ExchangeDataFolder & My.Settings.ExchangeData, IO.FileMode.Open)
    15.  
    16.             'Get the compressed stream
    17.             compressedData = New System.IO.Compression.GZipStream(inData, IO.Compression.CompressionMode.Decompress)
    18.  
    19.             'Open output data file
    20.             outData = New System.IO.FileStream(txtPath.Text & My.Settings.Exchange & "Exchange.mdf", IO.FileMode.Create, IO.FileAccess.Write)
    21.  
    22.             'Read the data off the stream and write it through to a file
    23.             While True
    24.  
    25.                 bytesRead = compressedData.Read(buffer, offset, 1024)                           'Attempt a 1K read
    26.  
    27.                 If bytesRead = 0 Then                                                           'Fail if no data left
    28.                     Exit While
    29.                 End If
    30.  
    31.                 outData.Write(buffer, offset, 1024)                                             'Write the 1K to file
    32.  
    33.                 offset += bytesRead
    34.                 totalBytes += bytesRead
    35.             End While
    36.  
    37.         Catch ex As Exception
    38.             MessageBox.Show("Error:" & ex.Message)
    39.         Finally
    40.  
    41.             If inData IsNot Nothing Then
    42.                 inData.Close()
    43.                 inData.Dispose()
    44.             End If
    45.  
    46.             If outData IsNot Nothing Then
    47.                 outData.Close()
    48.                 outData.Dispose()
    49.             End If
    50.  
    51.             If compressedData IsNot Nothing Then
    52.                 compressedData.Close()
    53.                 compressedData.Dispose()
    54.             End If
    55.  
    56.         End Try
    57.  
    58.     End Sub

    Simply: Read 1K of data uncompressed data from GZipStream and write to file stream. Do until 0 bytes read.

    On the second iteration of the while loop during the read I am greeted with an error: "Offset plus count is larger than the length of target array."

    I must be missing something VERY basic here people. Does the GZipStream.Read() routine append data to the buffer or simply write from the start each time? I would have assumed and hoped that it would write each time to the beginning but from a little debugging we have found that it must be appending and therefore running out of space - hence the error.

    Now the MS examples just declare a buffer big enough for the whole file, this will not suffice for me - I need to write files of unknown size, maybe around 600-800MB. If I were to do this how could I estimate the size of the uncompressed file assuming I had the RAM.

    Its going to be something so stupid! I know it....

    Thanks Guys,
    Matt.
    Last edited by GottaGetITDone; Jan 29th, 2007 at 01:12 AM.

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

    Re: [2005] GZipStream Decompress

    The second parameter to Read and Write are the offset within the array, not the offset within the file. Every time you read data you want to place it in the array starting from the first element, so that parameter value should be zero every time.

    Also, the third parameter to Write is the number of bytes to write. You're trying to write 1024 every time but what if you didn't read 1024 bytes in the first place. I suggest that you change this:
    VB Code:
    1. While True
    2.  
    3.                 bytesRead = compressedData.Read(buffer, offset, 1024)                           'Attempt a 1K read
    4.  
    5.                 If bytesRead = 0 Then                                                           'Fail if no data left
    6.                     Exit While
    7.                 End If
    8.  
    9.                 outData.Write(buffer, offset, 1024)                                             'Write the 1K to file
    10.  
    11.                 offset += bytesRead
    12.                 totalBytes += bytesRead
    13.             End While
    to somethinglike this:
    VB Code:
    1. bytesRead = compressedData.Read(buffer, 0, buffer.Length)
    2.  
    3. While bytesRead > 0
    4.     outData.Write(buffer, 0, bytesRead)
    5.     bytesRead = compressedData.Read(buffer, 0, buffer.Length)
    6. End While
    Also, you don't need that Finally block at all. You should get to grips with the Using statement, which will implicitly dispose your objects whether there's an exception thrown or not.
    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

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] GZipStream Decompress

    Hey JM!

    Dude! Theres that "something stupid" bit I was talking about! Offset within the array not the file!

    We're off and running!

    Thank you and apologies JM. I constantly seem to miss the those simple things that make all the difference.

    I will look further into the 'using' statement JM....

    Matt.

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