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

2 Answers 2

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");
10
  • 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? Dec 26, 2014 at 10:21
  • you should reference to System.IO.Compression.dll. Dec 26, 2014 at 10:26
  • for the MemoryString(file). this file parameter is the file name or what? Dec 26, 2014 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? Dec 26, 2014 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 Dec 26, 2014 at 10:48
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.