c# - File Retrieved via FtpWebRequest Is Different Than Source File On Server -


i retrieving gzip files ftp server using example code provided in microsoft page:

https://msdn.microsoft.com/en-us/library/ms229711(v=vs.110).aspx

the code works great! connection established, file retrieved , saved target path.

the problem file created code not same file on source ftp site. example, may have source file 288,936 bytes, file created code 532,550 bytes. maybe fine if dealing plain text files, since these gzip files not able unzip them because corrupted, yielding following error:

the magic number in gzip header not correct. make sure passing in gzip stream.

i know error somewhere in ftp download code because can use different ftp client download file , upzip file correctly.

here exact source code. have created own "helper" methods make connection ftp server , download remote file local folder destination.

    public static void transferfilefromftp(string uri, string username, string password, string foldername, string filename, directoryinfo importfolder)     {         ftpwebrequest ftp = getftpconnection(uri, username, password, foldername + "/" + filename);         ftp.keepalive = false;         ftp.method = webrequestmethods.ftp.downloadfile;         ftpwebresponse response = (ftpwebresponse)ftp.getresponse();          stream responsestream = response.getresponsestream();         streamreader reader = new streamreader(responsestream);          fileinfo file = new fileinfo(path.combine(importfolder.fullname, filename));         using (stream filestream = file.openwrite())         using (streamwriter filewriter = new streamwriter(filestream))         {             filewriter.write(reader.readtoend());         }          reader.close();         response.close();     }       //helper method create ftp connection...     private static ftpwebrequest getftpconnection(string uri, string username, string password, string foldername)     {         ftpwebrequest ftp;          if (foldername == null || foldername.length == 0) ftp = (ftpwebrequest)ftpwebrequest.create(uri);         else ftp = (ftpwebrequest)ftpwebrequest.create(uri + "/" + foldername);         ftp.credentials = new system.net.networkcredential(username, password);          return ftp;     } 

what missing? how can accurate download of files?

ftpwebrequest , ftpwebresponse painful work with.

system.net.webclient easier use simple downloads.

here's example downloads gzip file sec's edgar ftp site:

using system.net; ... var webclient = new webclient(); webclient.credentials = new networkcredential("anonymous", "janedoe@contoso.com"); webclient.downloadfile("ftp://ftp.sec.gov/edgar/daily-index/2012/qtr4/company.20121003.idx.gz", @"c:\temp\destination.gz"); 

for more advanced ftp operations, system.net.ftpclient project on codeplex choice. can installed via nuget.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -