Public Function GZipCompress(ByVal compressData As String) Try Dim buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(compressData) Dim sr As New IO.MemoryStream() Dim compressedzipStream As New GZipStream(sr, CompressionMode.Compress)
compressedzipStream.Write(buffer, 0, buffer.Length)
Return
sr.ToArray sr.Flush()compressedzipStream.Flush()
compressedzipStream.Close()
sr.Close()
sr.Dispose()
destinationStream.Flush()
destinationStream.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try End Function'''''''''''''''''Decompressing
Public
Function decomp(ByVal decompExp As Object) Try Dim abyt() As Byteabyt =
CType(decompExp, Byte()) Dim ms As New IO.MemoryStream(abyt) Dim objZS As System.IO.Compression.GZipStream = New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress) Dim mem As New MemoryStream Dim buffer As Byte() = New Byte(32767) {} Dim bytesRead As Integer = 0 While TruebytesRead = objZS.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Thenmem.Write(buffer, 0, bytesRead)
Else Exit While End If End While Dim sa As New IO.StringReader(System.Text.ASCIIEncoding.ASCII.GetString(mem.ToArray)) Return sams.Flush()
ms.Close()
ms.Dispose()
mem.Flush()
mem.Close()
mem.Dispose()
objZS.Flush()
objZS.Close()
objZS.Dispose()
sa.Close()
sa.Dispose()
Catch ex As ExceptionMsgBox(ex.ToString)
End Try End Function
Stream Not writing last byte?
mike22
Note that you don't need to flush the stream if you close it because Close will automatically flush the stream first. You also don't need to call Dispose if you have already called Close. They both invoke the same method. For classes that expose both you should normally just call Close as it makes more logical sense.
Note also that your code is not exception safe. If an exception occurs while operating on the stream you will leak memory and potentially lock a resource. Here is my recommended update to your function. You should modify your second function as well.
Public Function GZipCompress(ByVal compressData As String)
Dim sr As IO.MemoryStream
Dim compressedzipStream As GZipStream
Dim results() As Byte
Try
Dim buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(compressData)sr = New IO.MemoryStream()
compressedzipStream = New GZipStream(sr, CompressionMode.Compress)
compressedzipStream.Write(buffer, 0, buffer.Length)
sr.Flush()
Return sr.ToArray
'sr.Flush()
'compressedzipStream.Flush()
'compressedzipStream.Close()
'sr.Close()
'sr.Dispose()
'destinationStream.Flush()
'destinationStream.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
'Close stream if it is opened - will automatically flush data
If Not compressedzipStream Is Nothing
compressedzipStream.Close()
'Close extra memory stream
If Not sr Is Nothing
sr.Close()
End Try
End Function
My VB isn't too great so you might need to make changes but in a nutshell it cleans up the resources (streams) in the finally block to guarantee that they are always properly cleaned up. Notice that the return statement stays in this case because this is the last thing you are doing in the function. However you have to flush the sr stream first otherwise you might not get all the data.Hope this helps,
Michael Taylor - 11/11/05
Scottie J
I included the code that i try out (download through internet) as below. Hope that someone can help me.
Public Function compresserFichier(ByVal cheminSource As String, ByVal cheminDestination As String) As Boolean Try Dim monFileStream As FileStream
monFileStream =
New FileStream(cheminSource, FileMode.Open) Dim monBuffer As Byte()monBuffer =
New Byte(monFileStream.Length) {}monFileStream.Read(monBuffer, 0, System.Convert.ToInt32(monFileStream.Length))
monFileStream.Close()
monFileStream =
New FileStream(cheminDestination, FileMode.Create) Dim monGZipStream As GZipStreammonGZipStream =
New GZipStream(monFileStream, CompressionMode.Compress, False)monGZipStream.Write(monBuffer, 0, monBuffer.Length)
monGZipStream.Close()
monFileStream.Close()
Return True Catch ex As ExceptionMessageBox.Show(ex.Message)
Return False End Try End FunctionJohn1984
this is where I set the data to the dataset
Me
.InventorydbDataSet.Tables("webXml").Rows.Find(saveName).Item("xml9") = sr.toarray'sr.toarray is the byte array
here is the update code
Me.WebXmlBindingSource.EndEdit() Me.WebXmlTableAdapter.Update(Me.InventorydbDataSet.webXml) Me.InventorydbDataSet.webXml.AcceptChanges() Me.WebXmlTableAdapter.Fill(Me.InventorydbDataSet.webXml)
acplus
Thank you for your reply. I changed the my code according to what you said, and also changed my decompression code to what I believe matches, but the problem still exists, which sucks lol. so here is my updated decompress code. Thanks again!
Public Function decomp(ByVal decompExp As Object) As String Dim objZS As System.IO.Compression.GZipStream Dim mem As New MemoryStream Dim abyt() As Byte
abyt =
CType(decompExp, Byte()) Dim ms As New IO.MemoryStream(abyt) TryobjZS =
New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress)
Dim buffer As Byte() = New Byte(32767) {} Dim bytesRead As Integer = 0 While TruebytesRead = objZS.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Thenmem.Write(buffer, 0, bytesRead)
Else Exit While End If End Whilems.Flush()
bind.Filter =
Nothing Return System.Text.ASCIIEncoding.ASCII.GetString(mem.ToArray) Catch ex As ExceptionMsgBox(ex.ToString)
Finally If Not objZS Is Nothing ThenobjZS.Close()
End If If Not ms Is Nothing Thenms.Close()
End If If Not mem Is Nothing Thenmem.Close()
End If End Try End FunctionJerseyGRL1970
I'm not sure what the WebXml thing you are using is other than perhaps a DataSet. Did you verify that the column in the row contained all the data before you flushed it to the DB It seems that maybe it isn't going to like the array. What type is the column
Why do you call Fill again Is the row valid before you call Fill and invalid after If so then maybe the underlying DataSet you are using is not pushing all the data out.
Michael Taylor - 11/11/05
Nino Crudele
I think I have solve the problem that I post. For the last post I call the compresserFichier function through:
m_objFile.compresserFichier(
"C:\\Test.txt", "D:\\Test.gz")Now I change the call to:
m_objFile.compresserFichier(
"C:\\Test.txt", "D:\\Test.txt.gz")So for the zip name I want to use, I change it to original file name + .gz extension. So the Text.txt keep it's original file name and extension. But I don't really understand why it is like that, can someone help explain to me.
Thanks all.