3

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
e-on
  • 1,547
  • 8
  • 32
  • 65

1 Answers1

13

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

Teletha
  • 603
  • 1
  • 11
  • 21
  • Hi Teletha - making progress, but is now bringing up an error saying "ZipEntry::ReadDirEntry(): Bad signature " when I try and go into the Zip file to loop through the contents. When I try and open the downloaded zip file just in explorer, it brings up an error saying "The compressed folder... is invalid". Do you not need a different download method for Zip files? – e-on Jun 23 '11 at 09:31
  • You are correct. It worked for normal files and I assumed it would also work for zip files. I'm sorry. Looking at a solution now. – Teletha Jun 23 '11 at 09:51