How to extract file from an FTP directory?

Asked

Viewed 702 times

0

Good afternoon, everyone,

I need to extract the files that are in the FTP folders. Following the examples that are in these links:

https://stackoverflow.com/questions/3298922/how-to-list-directory-contents-with-ftp-in-c/3299039#3299039

Ftpwebresponse.Getresponsestream returning an HTML

How to Download FTP Files

I was able to extract the name of the FTP folders but now I need to know how to extract the files that are saved in those folders.

Can someone help me ?

Follow the code I’m using, which can be found in one of the links mentioned above.

Thank you.

    public static void BUSCADIRTESTE(string uri, string usuario, string senha)
    {
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
        ftpRequest.Credentials = new NetworkCredential(usuario, senha);
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;           

        FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();

        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            List<string> directories = new List<string>();

            string line = streamReader.ReadLine();

            while (!string.IsNullOrEmpty(line))
            {
                directories.Add(line);
                line = streamReader.ReadLine();
            }
        }
    }
  • What does "extract" mean? Download? Or unzip?

  • @jbueno, when I say extract, I mean to download the files that are in the ftp folders.

  • @jbueno, I managed to solve, I will post the code as an answer there in case someone needs, already has a basis for consultation.

2 answers

0


I was able to solve it this way:

    public static void StartDownloads(out string pstrMsg, out bool pbooRetorno, string pstrUrl, string pstrLocal, string pstrUsuario, string pstrSenha)
    {
        pstrMsg = string.Empty;
        pbooRetorno = false;

        try
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pstrUrl);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // Credenciais utilizadas para conectar no servidor FTP
            request.Credentials = new NetworkCredential(pstrUsuario, pstrSenha);

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();

                using (StreamReader reader = new StreamReader(responseStream))
                {
                    // Cria o arquivo no local especificado
                    using (StreamWriter writer = new StreamWriter(pstrLocal))
                    {
                        string strLinhaArquivo = reader.ReadLine();

                        while (!String.IsNullOrEmpty(strLinhaArquivo))
                        {
                            // Gravar as informações no arquivo
                            writer.Write(strLinhaArquivo);

                            // Verifica se o arquivo extraido contém linha
                            if (!String.IsNullOrEmpty((strLinhaArquivo = reader.ReadLine())))
                            {
                                // Inseri uma nova linha no arquivo
                                writer.WriteLine();
                            }
                        }
                    }
                }
            }
            pbooRetorno = true;
        }
        catch (Exception ex)
        {
            pstrMsg = string.Format("Erro:\nMétodo 'StartDownloads'\nDetalhes: {0}", ex.Message);

            pbooRetorno = false;
        }
    }

Obs: the method is only returning one file at a time. If you have to download all files at once, you have to adjust the method with necessary changes.

0

I believe that the FTP protocol is not the most suitable for file extraction. When a file is extracted, the processing done was the responsibility of the server, that is, you must send a command so that the server extracts this file. In this context, it is possible to perform the extraction using timed tasks or a programming language that interacts with the server. You can use the PHP for file extraction:

http://php.net/manual/en/ziparchive.extractto.php

It is also possible in the ASP.NET with the use of Webservices, but of course, depends on the installed server.

  • Good afternoon Ronan Silva, in the case php would not help me. because I am using c#, I would like a solution that I can use in c# same. I will continue researching some other solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.