Downloading ZIP file from FTP and copying to folder within website

14,273

Here is an example that downloads a file from an ftp.

try
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddr + "test.zip");
    request.Credentials = new NetworkCredential(userName, password);
    request.UseBinary = true; // Use binary to ensure correct dlv!
    request.Method = WebRequestMethods.Ftp.DownloadFile;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    FileStream writer = new FileStream("test.zip", FileMode.Create);

    long length = response.ContentLength;
    int bufferSize = 2048;
    int readCount;
    byte[] buffer = new byte[2048];

    readCount = responseStream.Read(buffer, 0, bufferSize);
    while (readCount > 0)
    {
        writer.Write(buffer, 0, readCount);
        readCount = responseStream.Read(buffer, 0, bufferSize);
    }

    responseStream.Close();
    response.Close();
    writer.Close();

}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

Edit I'm sorry for the error in previous code.

When correcting my previous code I found the following resource useful: example

Share:
14,273
e-on
Author by

e-on

Updated on June 28, 2022

Comments

  • e-on
    e-on almost 2 years

    Finding some problems copying a zip file from an FTP location. It is just copying and empty file so I think there is something wrong with my use of StreamReader or StreamWriter.

    Here is the code:

    //read through directory details response
    string line = reader.ReadLine();
    while (line != null)
    {
        if (line.EndsWith("zip")) //"d" = dir don't need "." or ".." dirs
        {
            FtpWebRequest downloadRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftpHost + line); //new Uri("ftp://" + ftpServerIP + DestinationFolder + fileInf.Name));
            downloadRequest.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FilesUser"], ConfigurationManager.AppSettings["FilesPass"]);
            downloadRequest.KeepAlive = false;
            downloadRequest.UseBinary = true;
            downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    
            string folderToWrite = HttpContext.Current.Server.MapPath("~/Routing/RoutingFiles/");
            string folderToSave = HttpContext.Current.Server.MapPath("~/Routing/");
    
            StreamReader downloadRequestReader = new StreamReader(downloadRequest.GetResponse().GetResponseStream());
            DirectoryInfo downloadDirectory = new DirectoryInfo(folderToWrite);
    
            FileInfo file = new FileInfo(Path.Combine(downloadDirectory.FullName, line));
            if (!file.Exists)
            {
                StreamWriter writer = new StreamWriter(Path.Combine(folderToWrite, line), false);
                writer.Write(downloadRequestReader.ReadToEnd());
    
                using (var downloadResponseStream = response.GetResponseStream())
                {
                }
            }
        }
    }
    

    By the time it gets to the bottom of that section, the file has been copied but is empty so I don't think I'm reading the stream correctly for a zip file. Anyone any ideas? I've seen talk of FileStream being better for downloading Zip files, but I couldn't get that to work either.

    Thanks.