how to dynamically unpacks the zip(xml files) stream into xml for processing

May 8, 2011 at 11:10 PM

I am trying to implement "a WCF service that receives a zip file as an attachment, and dynamically unpacks the zip to a stream for analysis" . 

Does anybody has sample to read the stream file on the fly?



1) Zip (Xml Files) Stream comes from client to wcf service

2) Iterate through all the files

3) Read one xml stream to string xml (??) without unzipping the files to the filesystem..

May 9, 2011 at 3:48 AM
Edited May 9, 2011 at 3:49 AM

Hmm, interesting.  Not sure exactly what you are planning.  it sounds like WCF Service that receives a stream,
and then unzips the stream as it is being received.

I think it should be straightforward.  Enable streaming as per this page: How to: Enable Streaming in WCF

In the server-side code, you have a method that receives a readable stream as input.  Create a ZipInputStream() around that Stream, and simply read/extract entries from the zip file.

    byte[] buffer= new byte[2048];
    int n;
    using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
        using (var input= new ZipInputStream(raw))
            ZipEntry e;
            while (( e = input.GetNextEntry()) != null)
                if (e.IsDirectory) continue;
                string outputPath = Path.Combine(extractDir, e.FileName);
                using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite))
                    while ((n= input.Read(buffer, 0, buffer.Length)) > 0)

The code above shows how to create a file for each entry in the received zip stream.  You said you didn't want to do that, so you'd need to modify that but.  What you decide to do with the data, once you extract it, is up to you.

ps: There was a related question a while ago about a WCF service that emits a ZIP stream back to the client.  This required a nifty trick. See this post for details:

You don't need to use anonymous pipes in your scenario though - for a client sending a zip to the server.


May 9, 2011 at 11:34 AM
Edited May 9, 2011 at 11:34 AM
Cheeso wrote:

Thanks your reply. Basically,

Upload Call: I get xml file in the zip format and have to deserialize into the data object (created by xsd) and pass it on to the business layer API.

Download Call: I get xml as a string from business layer and here, I have to convert the xml string into xml file, zip it and convert into a stream to return as a response back to the client.

Note: In the above two calls, we do not want to write anything to filesystem nor memory on the service side (Hosted on IIS Website ).

The link you have provided has useful information. I will try the code and will let you know.