Hello there,
I'm having an odd issue with GZIP compression (having followed example code
found on MSDN). Basically, after running through the compression routine I
end up with a byte array several times larger than the source text file,
full of zero data. Below is the code used to do the compression, it's a part
of a web service to retreive a file, there's a compress option prior to
base64 encoding the data. In the following code all undeclared variables you
see are properties, compress repersents a compress attribute specified in
the xml request, FileName is a relitive path to the file on the server
inside the webroot.
Response.Conten tType = "text/xml"
If Not File.Exists(Ser ver.MapPath(Fil eName)) Then
Throw New GetBinaryFileEx ception(FileNam e,
GetBinaryFileEx ception.GetBina ryFileError.Fil eNotFound)
End If
Dim FileData() As Byte = Nothing
Dim FStream As New FileStream(Serv er.MapPath(File Name),
FileMode.Open, FileAccess.Read , FileShare.ReadW rite)
If Compress Then
Dim TempData(FStrea m.Length - 1) As Byte
FStream.Read(Te mpData, 0, FStream.Length)
Dim MStream As New MemoryStream
Dim Compressor As New GZipStream(MStr eam,
CompressionMode .Compress, True)
Compressor.Writ e(TempData, 0, TempData.Length )
ReDim FileData(MStrea m.Length - 1)
Dim BytesRead As Integer = MStream.Read(Fi leData, 0,
MStream.Length)
MStream.Close()
MStream.Dispose ()
Compressor.Clos e()
Compressor.Disp ose()
Else
ReDim FileData(FStrea m.Length - 1)
FStream.Read(Fi leData, 0, FStream.Length)
End If
FStream.Close()
FStream.Dispose ()
Dim Base64 As String = Convert.ToBase6 4String(FileDat a)
Dim FileDataNode As XmlNode =
XmlExchangeLib. GetOrSetXmlNode ("FileData", Root)
XmlExchangeLib. AddAttributeWit hValue(FileData Node, "Compressed ",
Compress.ToStri ng().ToLower())
FileDataNode.In nerText = Base64
XmlResponse.Sav e(Response.Outp utStream)
I'm having an odd issue with GZIP compression (having followed example code
found on MSDN). Basically, after running through the compression routine I
end up with a byte array several times larger than the source text file,
full of zero data. Below is the code used to do the compression, it's a part
of a web service to retreive a file, there's a compress option prior to
base64 encoding the data. In the following code all undeclared variables you
see are properties, compress repersents a compress attribute specified in
the xml request, FileName is a relitive path to the file on the server
inside the webroot.
Response.Conten tType = "text/xml"
If Not File.Exists(Ser ver.MapPath(Fil eName)) Then
Throw New GetBinaryFileEx ception(FileNam e,
GetBinaryFileEx ception.GetBina ryFileError.Fil eNotFound)
End If
Dim FileData() As Byte = Nothing
Dim FStream As New FileStream(Serv er.MapPath(File Name),
FileMode.Open, FileAccess.Read , FileShare.ReadW rite)
If Compress Then
Dim TempData(FStrea m.Length - 1) As Byte
FStream.Read(Te mpData, 0, FStream.Length)
Dim MStream As New MemoryStream
Dim Compressor As New GZipStream(MStr eam,
CompressionMode .Compress, True)
Compressor.Writ e(TempData, 0, TempData.Length )
ReDim FileData(MStrea m.Length - 1)
Dim BytesRead As Integer = MStream.Read(Fi leData, 0,
MStream.Length)
MStream.Close()
MStream.Dispose ()
Compressor.Clos e()
Compressor.Disp ose()
Else
ReDim FileData(FStrea m.Length - 1)
FStream.Read(Fi leData, 0, FStream.Length)
End If
FStream.Close()
FStream.Dispose ()
Dim Base64 As String = Convert.ToBase6 4String(FileDat a)
Dim FileDataNode As XmlNode =
XmlExchangeLib. GetOrSetXmlNode ("FileData", Root)
XmlExchangeLib. AddAttributeWit hValue(FileData Node, "Compressed ",
Compress.ToStri ng().ToLower())
FileDataNode.In nerText = Base64
XmlResponse.Sav e(Response.Outp utStream)
Comment