By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

GZip Compression :(

P: n/a
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.ContentType = "text/xml"
If Not File.Exists(Server.MapPath(FileName)) Then
Throw New GetBinaryFileException(FileName,
GetBinaryFileException.GetBinaryFileError.FileNotF ound)
End If

Dim FileData() As Byte = Nothing
Dim FStream As New FileStream(Server.MapPath(FileName),
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
If Compress Then
Dim TempData(FStream.Length - 1) As Byte
FStream.Read(TempData, 0, FStream.Length)
Dim MStream As New MemoryStream
Dim Compressor As New GZipStream(MStream,
CompressionMode.Compress, True)
Compressor.Write(TempData, 0, TempData.Length)

ReDim FileData(MStream.Length - 1)
Dim BytesRead As Integer = MStream.Read(FileData, 0,
MStream.Length)
MStream.Close()
MStream.Dispose()
Compressor.Close()
Compressor.Dispose()
Else
ReDim FileData(FStream.Length - 1)
FStream.Read(FileData, 0, FStream.Length)
End If
FStream.Close()
FStream.Dispose()

Dim Base64 As String = Convert.ToBase64String(FileData)

Dim FileDataNode As XmlNode =
XmlExchangeLib.GetOrSetXmlNode("FileData", Root)
XmlExchangeLib.AddAttributeWithValue(FileDataNode, "Compressed",
Compress.ToString().ToLower())
FileDataNode.InnerText = Base64
XmlResponse.Save(Response.OutputStream)

Mar 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Carlo Razzeto" <cr******@hotmail.comwrote in
news:40**********************************@microsof t.com:
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.
I noticed that you're converting your binary data into Base64 text? That
will also inflate your data. Not sure why you're getting 0 data however.

--
sp**********@rogers.com (Do not e-mail)
Mar 27 '08 #2

P: n/a
Carlo Razzeto wrote:
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.ContentType = "text/xml"
If Not File.Exists(Server.MapPath(FileName)) Then
Throw New GetBinaryFileException(FileName,
GetBinaryFileException.GetBinaryFileError.FileNotF ound)
End If

Dim FileData() As Byte = Nothing
Dim FStream As New FileStream(Server.MapPath(FileName),
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
If Compress Then
Dim TempData(FStream.Length - 1) As Byte
FStream.Read(TempData, 0, FStream.Length)
Dim MStream As New MemoryStream
Dim Compressor As New GZipStream(MStream,
CompressionMode.Compress, True)
Compressor.Write(TempData, 0, TempData.Length)

ReDim FileData(MStream.Length - 1)
Dim BytesRead As Integer = MStream.Read(FileData, 0,
MStream.Length)
MStream.Close()
MStream.Dispose()
Compressor.Close()
Compressor.Dispose()
Else
ReDim FileData(FStream.Length - 1)
FStream.Read(FileData, 0, FStream.Length)
End If
FStream.Close()
FStream.Dispose()

Dim Base64 As String = Convert.ToBase64String(FileData)

Dim FileDataNode As XmlNode =
XmlExchangeLib.GetOrSetXmlNode("FileData", Root)
XmlExchangeLib.AddAttributeWithValue(FileDataNode,
"Compressed", Compress.ToString().ToLower())
FileDataNode.InnerText = Base64
XmlResponse.Save(Response.OutputStream)
It took a little while, but I was able to lookup my own use of a GZip stream.

I believe your problem stems from the state of MStream after you have filled it
with the compressed data. Unless you do MStream.Position = 0, it will not be at
the start of the stream, so your MStream.Read(FileData, ... will not put any
data into FileData, and FileData will therefore stay all 0's.

You could just do MStream.Position = 0 before reading it, and that would
probably fix it, but you could also just do FileData = MStream.GetBuffer, which
seems like a more straightforward way to get the bytes.

In my testing, I found that if the result was less than 4K, the zip was no good.
I had to test for the size, and redo it with MStream.Capacity = 4096, to get a
minimum size that would produce a useable zip version.

Mar 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.