473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

GZip Compression :(

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
2 3178
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Xah Lee | last post by:
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in unix admin and perl, i have quickly located the...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
4
by: Anders K. Jacobsen [DK] | last post by:
Hi Does anyone have experience using gZip compression on the IIS server with a ASP.NET application. How much can a page be compressed. As i see it it must have a huge impact on the total size....
1
by: sameer | last post by:
Hi All, I am adding a custom header (Gzip header for compression) to the request when calling a webservice( sitting on a webserver) over the internet from my application ( If interested in the...
2
by: .nLL | last post by:
hi. i have recently enabled gzip compression on my server and it works fine. but i have noticed something wierd. To protect my self from hotlinking i use a simple code to send my download with...
2
by: Chaos | last post by:
I have tried to search Google, but I cannot seem to find a library to decompress a gzip string or char to a string or char. I want to write something that allows libcurl to access a page, save the...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
6
by: Giorgio Parmeggiani | last post by:
Hi I'm using the gzip compression found in WCG samples kit. It works well, but how can I set the SendTimeout and ReceiveTimeout parameters? Thank in advance Giorgio
6
by: pooppoop | last post by:
Hi, and thanks for viewing my post. i have an odd result when trying to compress and decompress a string. it seems that when i replace the Zero's in the input stream it works, if not the string...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.