Thursday, 15 May 2014

ASP.NET 4.5 ZipFile class how to download and open zip from FTP -



ASP.NET 4.5 ZipFile class how to download and open zip from FTP -

my overall goal grab zip file ftp, open on fly, read content specific field in excel file (there ever 1 file in zip).

so far have been successful in connecting ftp ftpwebrequest , can download file local folder (not desired workflow taking 'baby steps' approach)

i have found in asp.net 4.5 class introduced called zipfile , seems have method open , read zip can not seem have take ftp path valid path.

here have downloading.

var file = f.element("file_path").value; string ftphost = "ftp.com"; var uri = @"ftp://" + ftphost + "/download/" + file; var username = @"ftp@mysite.com"; ftpwebrequest ftp = (ftpwebrequest)ftpwebrequest.create(uri); ftp.usepassive = true; ftp.credentials = new networkcredential(username, "password"); ftp.usebinary = true; ftp.method = webrequestmethods.ftp.downloadfile; ftpwebresponse ftpresponse = (ftpwebresponse)ftp.getresponse(); string thefile = ftpresponse.responseuri.tostring(); stream responsestream = ftpresponse.getresponsestream(); filestream author = new filestream(@"c:\websites\" + file, filemode.create); long length = response.contentlength; int buffersize = 2048; int readcount; byte[] buffer = new byte[2048]; readcount = responsestream.read(buffer, 0, buffersize); while (readcount > 0) { writer.write(buffer, 0, readcount); readcount = responsestream.read(buffer, 0, buffersize); } responsestream.close(); ftpresponse.close(); writer.close();

i have tried implement next open , read zip. placed block after line creating string "thefile" lack of knowing improve home it. returns , error path invalid.

.... ftpwebresponse ftpresponse = (ftpwebresponse)ftp.getresponse(); string thefile = ftpresponse.responseuri.tostring(); using (ziparchive archive = zipfile.openread(thefile)) { foreach (ziparchiveentry zfile in archive.entries) { var filename = zfile.name; } }

how might accomplish opening , reading file ftp?

asp.net ftp zipfile ftpwebrequest

No comments:

Post a Comment