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