4

How to download .zip file format using c# code?

Here is the code, i am using to download. Just to highlight, If i download .txt file, it works fine. If i download .zip file, it downloads the .zip file but i can't open this. It complains that .zip is in incorrect format. I have doubt in how i am writing back the file on local drive.

Help?

string ftpServerIP = FTPServer;
string ftpUserID = FTPUser;
string ftpPassword = FTPPwd;
FileInfo fileInf = new FileInfo(FileName);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri); //new Uri("ftp://" + ftpServerIP + DestinationFolder + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.EnableSsl = true;
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
//reqFTP.UsePassive = true;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
//Stream strm = reqFTP.GetRequestStream();
StreamReader reader = new StreamReader(reqFTP.GetResponse().GetResponseStream());
StreamWriter writer = new StreamWriter(Path.Combine(FolderToWriteFiles, FileName), false);
writer.Write(reader.ReadToEnd());
return true; 
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
Jango
  • 5,375
  • 14
  • 57
  • 63

5 Answers5

9
using System.Net;
// ...

new WebClient().DownloadFile("ftp://ftp.someurl.com/file.zip",
                             "C:\\downloadedFile.zip");

Answer to the updated question:

The way you are saving the stream to disk is wrong. You are treating the stream as a character sequence, which corrupts the ZIP file in the process. Open a FileStream instead of a StreamWriter and copy the GetResponseStream() return value directly to that FileStream using something like my CopyStream function from here.

Community
  • 1
  • 1
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Can i do something using FtpWebRequest class? – Jango Feb 12 '10 at 21:35
  • This assumes the FTP server needs no login. The FtpWebRequest class would be better suited for an ftp server that requires a login. – CodingWithSpike Feb 12 '10 at 21:35
  • @user144842: Yes, but `WebClient` abstracts it away. If you just need to download a file without any more control, use `WebClient`. – Mehrdad Afshari Feb 12 '10 at 21:35
  • @rally25rs. My server needs login authentication. – Jango Feb 12 '10 at 21:36
  • @user144842: Then `WebClient` won't work for you. You need to use `WebRequest.Create` or `FtpWebRequest` directly (`WebClient` actually uses `WebRequest` internally, which is an abstract class that creates `XXXWebRequest` classes for different protocols.) – Mehrdad Afshari Feb 12 '10 at 21:40
  • @Mehrdad: See my updated question/code above, and give me your best suggestions. Thanks – Jango Feb 12 '10 at 21:43
  • @user144842: commented on the question. Can you *download* the zip file with a FTP client (lets say Internet Explorer) and try opening it? – Mehrdad Afshari Feb 12 '10 at 21:46
  • @user144842: I guess I noticed the problem. See my updated answer. – Mehrdad Afshari Feb 12 '10 at 21:49
  • :( gtg. its already 5 here. i will try it next week. have a good weekend. – Jango Feb 12 '10 at 22:03
  • @Mehrdad. I am still stuck here. I could not use your example. Help? – Jango Feb 16 '10 at 15:13
  • @user144842: Why couldn't you use it? What's the problem? Instead of creating a StreamWriter, create a FileStream and copy GetResponseStream to the filestream. – Mehrdad Afshari Feb 16 '10 at 15:59
  • @Mehrdad: I tried..but i really don't know how? I am Sorry if i am asking little too much, I need code :(? – Jango Feb 16 '10 at 16:06
  • Replace the last lines with `using (var fs = File.Create(Path.Combine(FolderToWriteFiles, FileName))) CopyStream(fs, reqFTP.GetResponse().GetResponseStream());` The `CopyStream` function is linked in my answer. – Mehrdad Afshari Feb 16 '10 at 16:52
1

The .NET Framework's System.Net namespace offers the FTPWebRequest class. Here's an article explaining how to use it:

http://www.vcskicks.com/download-file-ftp.php

lance
  • 16,092
  • 19
  • 77
  • 136
0

For all of you who found these answers unhelpful, I found a better answer here:

Downloading ZIP file from FTP and copying to folder within website

Community
  • 1
  • 1
Carlos Martinez T
  • 6,458
  • 1
  • 34
  • 40
0

You would probably want to make use of the FtpWebRequest class to download the .zip file, then the System.IO.Packaging class to extract its contents.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
0

A good alternative for unzipping is http://www.codeplex.com/DotNetZip.

If you need to download SSH or SSL encryption then I recommend this component: http://www.weonlydo.com/index.asp?showform=FtpDLX.NET. Also great for plain FTP.

Ed Power
  • 8,310
  • 3
  • 36
  • 42