如何通过FTP使用C#下载压缩文件(.zip文件)? [英] how to download compressed file (.zip) through FTP using c#?

查看:1044
本文介绍了如何通过FTP使用C#下载压缩文件(.zip文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#代码下载.zip文件格式?



下面是代码,我使用的下载。只是要突出,如果我下载的.txt文件,它工作正常。如果我下载.zip文件,它下载.zip文件,但我无法打开此。它抱怨.zip文件是格式不正确。我有疑问,怎么我写回本地驱动器上的文件。



帮助?



 字符串ftpServerIP = FTPServer的; 
串ftpUserID =名为ftpuser;
串ftpPassword = FTPPwd;
FileInfo的fileInf =新的FileInfo(文件名);
字符串URI =FTP://+ ftpServerIP +/+ fileInf.Name;
的FtpWebRequest reqFTP =(的FtpWebRequest)FtpWebRequest.Create(URI); //新的URI(FTP://+ ftpServerIP + DestinationFolder + fileInf.Name));
reqFTP.Credentials =新的NetworkCredential(ftpUserID,ftpPassword);
reqFTP.EnableSsl = TRUE;
reqFTP.KeepAlive = FALSE;
reqFTP.UseBinary = TRUE;
//reqFTP.UsePassive = TRUE;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
ServicePointManager.ServerCertificateValidationCallback =新System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
//流STRM = reqFTP.GetRequestStream();
StreamReader的读者=新的StreamReader(reqFTP.GetResponse()GetResponseStream());
StreamWriter的作家=新的StreamWriter(Path.Combine(FolderToWriteFiles,文件名),FALSE);
writer.Write(reader.ReadToEnd());
返回真;


解决方案

 使用系统。净; 
// ...

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






答到更新的问题:



您正在保存的流盘的方式是错误的。你正在处理该流作为一个字符序列,从而破坏过程中的ZIP文件。打开的的FileStream 代替的StreamWriter 并复制 GetResponseStream()返回值直接到的FileStream 使用类似的我的 CopyStream 函数从这里


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; 

解决方案

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.

这篇关于如何通过FTP使用C#下载压缩文件(.zip文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆