Question FTP download zip files are corrupted

Alderweireldt Erik

New member
Joined
Apr 14, 2010
Messages
1
Programming Experience
3-5
Hi, I wonder if anyone could help me on this?
Following code corrupts zip files, found out to do it with a FILESTREAM in stead of a stream writer bit i can not figure out how to do it.

Dim Request As FtpWebRequest
Dim Response As FtpWebResponse
Dim Writer As StreamWriter

Request = WebRequest.Create("ftp://ftp2.techdata-it-emea.com/fees.zip")

Request.Credentials = New NetworkCredential("login", "password")

Request.Method = WebRequestMethods.Ftp.DownloadFile
Request.UseBinary = True

Response = Request.GetResponse()

Writer = New StreamWriter(Server.MapPath("~/files/feesN.zip"))
Writer.Write(New StreamReader(Response.GetResponseStream()).ReadToEnd)
Writer.Close()

litResponse.Text = Response.StatusDescription

Response.Close()

TNX
 
StreamWriter/StreamReader is for writing/reading strings only, a zip file is a binary file. While you can use BinaryReader/Writer it is not any different from using the Stream/FileStream directly when dealing with Byte data. There is no 'ReadToEnd' but instead you read some bytes from source and write them to target, until all is read.

Easier is to use My.Computer.Network.DownloadFile or WebClient.DownloadFile method.
 
Back
Top