How to download a compressed file (.zip) via FTP using C #?

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

Here is the code I use to download. Just highlight if I upload a .txt file, it works great. If I download a ZIP file, it downloads a ZIP file, but I cannot open it. He complains that the .zip is in the wrong format. I have doubts about how I write a file on a local drive.

reference

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; 
+4
source share
5 answers
 using System.Net; // ... new WebClient().DownloadFile("ftp://ftp.someurl.com/file.zip", "C:\\downloadedFile.zip"); 

Answer to the updated question:

The way you save the stream to disk is wrong. You treat the stream as a sequence of characters that distorts the ZIP file in the process. Open FileStream instead of StreamWriter and copy the return value of GetResponseStream() directly to this FileStream , using something like my CopyStream ) .

+9
source

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

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

+1
source

You might want to use the FtpWebRequest class to load the .zip file, then System.IO.Packaging to extract its contents.

0
source

A good alternative to unpacking is http://www.codeplex.com/DotNetZip .

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

0
source

For anyone who found these answers useless, I found the best answer here:

Download a ZIP file from FTP and copy to a folder inside the website

0
source

Source: https://habr.com/ru/post/1301155/


All Articles