GZipStream manipulation

L

Lloyd Spencer

Hi,

I have been unable to find and am therefore trying to write code to do the
following:
get a GZip file from the internet
unzip it
use it as a text file

The problem I am having is that the output from the GZipStream is the same
as the input, ie: I don't end up with more or different bytes. If anyone has
any suggestions for what I am doing wrong, they would be most welcome. This
appears to be relatively new in .net and I can't find out much about it.
Apologies in advance if this is an inappropriate forum, etc. Any direction
at all would be great.

Code follows:


Public Function ReadFtpZip(ByVal url As String) As Byte()
' first get the data from the ftp site
Dim objRequest As WebRequest = FtpWebRequest.Create(url)
objRequest.Method = WebRequestMethods.Ftp.DownloadFile
Dim objResponse As FtpWebResponse = objRequest.GetResponse()
Dim objStreamReader As New
StreamReader(objResponse.GetResponseStream())
' next define the raw data stream and write the ftp stream to it
Dim objStream As New MemoryStream
Dim objWriter = New StreamWriter(objStream)
objWriter.AutoFlush = True
objWriter.Write(objStreamReader.ReadToEnd)
' now we define the zipstream mystery object
Dim zipStream As New GZipStream(objStream, CompressionMode.Decompress)
zipStream.BaseStream.Position = 0
' now we have to read through this stream and output it to an answer
stream somehow
Dim theByte As Integer = zipStream.BaseStream.ReadByte()
Dim outputStream As New MemoryStream
While theByte <> -1
outputStream.WriteByte(CType(theByte, Byte))
theByte = zipStream.BaseStream.ReadByte()
End While
Return outputStream.ToArray()
End Function
 
J

John Mishefske

Lloyd said:
I have been unable to find and am therefore trying to write code to do the
following:
get a GZip file from the internet
unzip it
use it as a text file

Here is *sample* code with references that may help you find a solution.
It takes a String URL and returns the (uncompressed) text. The
LogMessage() routine is mine and can be commented out. Basic sample but
hopefully it helps some. Might have some line wrap in the code.


'
' http://csharpfeeds.com/post.aspx?id=5518
'
' <summary>
' Simple routine to retrieve HTTP Content as a string
' </summary>
' <param name="Url"></param>
' <returns></returns>
'
' Caching info:
http://msdn.microsoft.com/en-us/library/ekx461f3.aspx
'
http://msdn.microsoft.com/en-us/library/system.net.webrequest.cachepolicy.aspx
'
Public Function GetPostedData(ByVal Url As String) As String

Dim Html As String

Html = ""

Try

Dim Http As HttpWebRequest = CType(WebRequest.Create(Url),
HttpWebRequest)

' don't use cache version; ask server for new copy
Dim cachePolicy As HttpRequestCachePolicy = New
HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore)
Http.CachePolicy = cachePolicy
Http.Headers.Add(HttpRequestHeader.AcceptEncoding,
"gzip,deflate")

Dim WebResponse As Net.HttpWebResponse =
CType(Http.GetResponse, HttpWebResponse)

Dim responseStream As Stream = WebResponse.GetResponseStream()
responseStream = New GZipStream(responseStream,
CompressionMode.Decompress)

Dim Reader As New StreamReader(responseStream,
Encoding.Default)
Html = Reader.ReadToEnd()

WebResponse.Close()
responseStream.Close()
Reader.Close()

Catch wex As WebException
LogMessage("Web Error: " & wex.Message)
Html = ""

Catch ex As Exception
LogMessage("Error: " & ex.Message)
Html = ""
End Try

Return Html

End Function




--
John Mishefske, Microsoft MVP 2007 - 2009
UtterAccess Editor
Tigeronomy Software
web: http://www.tigeronomy.com
email: sales ~at~ tigeronomy.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top