|
-
Jan 29th, 2007, 12:19 AM
#1
Thread Starter
Lively Member
[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:
Private Sub InflateData()
Dim inData As System.IO.FileStream = Nothing
Dim outData As System.IO.FileStream = Nothing
Dim compressedData As System.IO.Compression.GZipStream = Nothing
Dim buffer(1024 - 1) As Byte
Dim totalBytes As Integer = 0
Dim bytesRead As Integer = 0
Dim offset As Integer = 0
Try
'Get zipped data file stream
inData = New System.IO.FileStream(My.Settings.ExchangeDataFolder & My.Settings.ExchangeData, IO.FileMode.Open)
'Get the compressed stream
compressedData = New System.IO.Compression.GZipStream(inData, IO.Compression.CompressionMode.Decompress)
'Open output data file
outData = New System.IO.FileStream(txtPath.Text & My.Settings.Exchange & "Exchange.mdf", IO.FileMode.Create, IO.FileAccess.Write)
'Read the data off the stream and write it through to a file
While True
bytesRead = compressedData.Read(buffer, offset, 1024) 'Attempt a 1K read
If bytesRead = 0 Then 'Fail if no data left
Exit While
End If
outData.Write(buffer, offset, 1024) 'Write the 1K to file
offset += bytesRead
totalBytes += bytesRead
End While
Catch ex As Exception
MessageBox.Show("Error:" & ex.Message)
Finally
If inData IsNot Nothing Then
inData.Close()
inData.Dispose()
End If
If outData IsNot Nothing Then
outData.Close()
outData.Dispose()
End If
If compressedData IsNot Nothing Then
compressedData.Close()
compressedData.Dispose()
End If
End Try
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.
-
Jan 29th, 2007, 12:47 AM
#2
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:
While True
bytesRead = compressedData.Read(buffer, offset, 1024) 'Attempt a 1K read
If bytesRead = 0 Then 'Fail if no data left
Exit While
End If
outData.Write(buffer, offset, 1024) 'Write the 1K to file
offset += bytesRead
totalBytes += bytesRead
End While
to somethinglike this:
VB Code:
bytesRead = compressedData.Read(buffer, 0, buffer.Length)
While bytesRead > 0
outData.Write(buffer, 0, bytesRead)
bytesRead = compressedData.Read(buffer, 0, buffer.Length)
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.
-
Jan 29th, 2007, 01:05 AM
#3
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|