iJay
iJay

Reputation: 4273

How to ZIP files in FTP Server Directory

In my Application I need to download some files from the FTP server..I coded to download one by one via for loop and it works fine.My Problem is it is very slow process coz for each file it will gives credential. So if I can zip those files in FTP server it can be Download quickly..If there is a way pls guide me.. here My Code:

Blockquote

    private void button1_Click(object sender, EventArgs e)
    {
        string[] list = GetFileList();
        DownloadFile(list, "e:\\sample\\");
    }

    private String[] GetFileList()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(_remoteHost));
        request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
        request.Method = WebRequestMethods.Ftp.ListDirectory;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        string FileNames = reader.ReadToEnd();
        string[] Files = Regex.Split(FileNames, "\r\n");
        return Files;
    }


    private void DownloadFile(string[] fileList, string destination)
    {
        for (int i = 2; i <= fileList.Length - 1; i++)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + fileList[i]);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            StreamWriter writer = new StreamWriter(destination + fileList[i]);
            writer.Write(reader.ReadToEnd());

            writer.Close();
            reader.Close();
            response.Close();
    }
}

Blockquote

Upvotes: 2

Views: 7355

Answers (3)

Saurabh R S
Saurabh R S

Reputation: 3177

If you are using .NET 3.0 or above you can make use of ZipPackage Class present in the System.IO.Packaging name space. I haven't yet tried to zip files present on remote machines but you can give it a try else you may have to write a small job to zip the files and deploy it on your remote(ftp) server.

Upvotes: 1

IvoTops
IvoTops

Reputation: 3531

You can re-use the credentials, which might speed you up...

private void DownloadFile(string[] fileList, string destination)
{

    var myCred = new NetworkCredential(_remoteUser, _remotePass);

    for (int i = 2; i <= fileList.Length - 1; i++)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + fileList[i]);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = myCred;
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        StreamWriter writer = new StreamWriter(destination + fileList[i]);
        writer.Write(reader.ReadToEnd());

        writer.Close();
        reader.Close();
        response.Close();
}

}

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37566

Why dont you initiate your FtpWebRequest once with the Username and Password and then use it in a foreach Loop for all files instead of creating a new one each round?

Another way would be to use a 3rd party tool (like: http://www.rebex.net/ftp-ssl.net/) which may be faster, but its something you will have to test, take a looka t this: https://stackoverflow.com/a/2343689/395659

A 3rd way could be to let a job running on the server which creates the ZIP file once in a period and you download it as a Zip file as mentioned in your Question.

Upvotes: 1

Related Questions