1

I have a FTP that contains zip files and insdie each zip file there is an xml file with the same name as the zip file.

I wanna read these xml files.

I done almost everything but now i have a question, which is not a syntax question but an approach question.

question:

should I download these zip fiels to a folder outside of the FTP folder ? and then parse the xml files inside them?

or there is a better approach to reach the xml file directly without having to download the zip files.

I am working with .net 4.5

many thanks

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

2 Answers2

2

You can open and decompress zip entries solely on memory, you can download the file and save it on MemoryStream, then pass it on to ZipArchive (or even pass FTP Stream directly, but I don't recommend it). Here is a little example of opening a zip container on Memory:

        var ms = new MemoryStream(file);
        var archive = new ZipArchive(ms, ZipArchiveMode.Read);
        var entry=archive.GetEntry("Sample.XML");

        var doc=XDocument.Load(entry.Open());

Here is a complete sample of downloading a zip archive from a FTP host and open an entry of it, I used ftp.dll library for FTP communication which you can obtain from nuget through

PM> Install-Package Ftp.dll

in the sample entry is Stream of an image file (banner.jpg) but opening a XML file would be the same (described above);

        var ms = new MemoryStream();
        var ftp = new Limilabs.FTP.Client.Ftp();

        ftp.Connect("ftp.microsoft.com");
        ftp.Login("anonymous");
        ftp.Download("softlib/MSLFILES/aspwebwiz2k.zip", ms);

        var archive = new ZipArchive(ms, ZipArchiveMode.Read);
        var entry=archive.GetEntry("banner.jpg");
user3473830
  • 7,165
  • 5
  • 36
  • 52
  • I kind of find a very similar solution to yours, i am testing it, but this `ziparchive` is not recognize by my visual studio 2012. which dll should i include please? – Marco Dinatsoli Dec 26 '14 at 10:21
  • you should reference to `System.IO.Compression.dll`. – user3473830 Dec 26 '14 at 10:26
  • for the `MemoryString(file)`. this `file` parameter is the file name or what? – Marco Dinatsoli Dec 26 '14 at 10:31
  • maybe it is the `zip file` from the ftp? I have the name of the zip file. could you help me please to pass it to this memeory stream? – Marco Dinatsoli Dec 26 '14 at 10:36
  • @MarcoDinatsoli , file is a `byte[]`, I updated the answer with a real ftp sample from Microsoft server, Unfortunately I couldn't fin a zip file containing a XML file, so I opened an image instead – user3473830 Dec 26 '14 at 10:48
  • thanks for the update. is this a third library please? – Marco Dinatsoli Dec 26 '14 at 10:51
  • @MarcoDinatsoli, yes the FTP part is from a third party library, which you can download from nuget by searching `ftp.dll` or by executing the Nuget command I included in the answer. – user3473830 Dec 26 '14 at 10:53
  • in the line `ftp.Connect(FTPAddress);` I got this error: `The requested name is valid, but no data of the requested type was found` what is that please? – Marco Dinatsoli Dec 26 '14 at 11:06
  • problem should be with DNS, try something like `IPHostEntry hostInfo = Dns.GetHostByName(@"server.address");` and pass the IPHostEntry.HostName instead. – user3473830 Dec 26 '14 at 11:59
  • plus one and I will accpet your answer, thanks but i still have the problem, i don't want to use this third library, or lets say I wanna use it as the last changes. i posted a new question, could you check it please? go to my account pleaes and check the last question i asked – Marco Dinatsoli Dec 26 '14 at 12:02
0

If by "without downloading" you mean not saving them on your system and reading them directly, then yes, there is a way of doing that. and if it means not even downloading it into your system and directly reading singly content of zip present at server, then as per my knowledge, no.

If former is the thing you meant, then you can download the content into you temporary memory, which you can access through MemoryStream Class and read the content directly from there without having to save it in your system or hardisk

Details of MemoryStream

Community
  • 1
  • 1
Codeek
  • 1,624
  • 1
  • 11
  • 20