Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

GZip Compression :(

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlo Razzeto - 26 Mar 2008 21:51 GMT
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.FileNotFound)
       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)
Cowboy (Gregory A. Beamer) - 27 Mar 2008 16:36 GMT
Look at these lines:

Dim TempData(FStream.Length - 1) As Byte
Compressor.Write(TempData, 0, TempData.Length)

You are asking the compressor to write TempData, but at the full length of
TempData, which is the length of the original file. Nothing wrong here,
necessarily, as you are writing to a MemoryStream, but you will have to get
rid of the end bits before writing to a file.

Personally, rather than tackle this, I would look at:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

That lib is open source, stable, and encapsulates zip, gzip and bzip very
well.

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************

| Think outside the box!

*************************************************
> Hello there,
>
[quoted text clipped - 47 lines]
>        FileDataNode.InnerText = Base64
>        XmlResponse.Save(Response.OutputStream)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.