views:

824

answers:

4

Hi

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; 
+6  A: 
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.

Mehrdad Afshari
Can i do something using FtpWebRequest class?
Novice
This assumes the FTP server needs no login. The FtpWebRequest class would be better suited for an ftp server that requires a login.
rally25rs
@user144842: Yes, but `WebClient` abstracts it away. If you just need to download a file without any more control, use `WebClient`.
Mehrdad Afshari
@rally25rs. My server needs login authentication.
Novice
@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
@Mehrdad: See my updated question/code above, and give me your best suggestions. Thanks
Novice
@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
okie dokie.. give a sec
Novice
@user144842: I guess I noticed the problem. See my updated answer.
Mehrdad Afshari
:( gtg. its already 5 here. i will try it next week. have a good weekend.
Novice
@Mehrdad. I am still stuck here. I could not use your example. Help?
Novice
@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
@Mehrdad: I tried..but i really don't know how? I am Sorry if i am asking little too much, I need code :(?
Novice
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
@Mehrdad: Thanks very much.
Novice
+1  A: 

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
A: 

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.

rally25rs
A: 

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.

ebpower