We had to download zip files from a FTP server, it was found that if the files were of small size there was no problem, however for big files (larger than around 4MB), after downloading them, it could not be opened and kept saying “the compressed zip folder is invalid or corrupted”.
The original code was:

FtpWebRequest reqFTP;
Response.Buffer = true;
Response.Clear();
Response.AddHeader(“Content-Disposition”, “inline; filename=” + filename);
Response.ContentType = “application/x-zip-compressed”;
Response.Flush();
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(“ftp://” + ftpServerIP + “/” + filename));
reqFTP.Proxy = null;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword, ftpDomain);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
MemoryStream MemStream = new MemoryStream();
int bufferSize = 1024;
byte[] respBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length);
while (bytesRead > 0)
{
MemStream.Write(respBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length);
}
byte[] finalByte = MemStream.GetBuffer();
Response.BinaryWrite(finalByte);

So, I changed the above 2 lines and wrote the below line which solved my problem.
Response.BinaryWrite(MemStream.ToArray());
Advertisements