Add string as file to subfolder

Nov 14, 2011 at 6:48 AM

Hi,

I want to create an archive with a number of subfolders and add files to these folders.
I have found out how to do it with a physical file, which is quite easy, but I want to find out how to add a document from memory to a subfolder.

I have this example code:

            ZipFile zip;
            DataTable dt = GetDataFromDataSource();
            string testdata="";


            using (StringWriter sw = new StringWriter())
            {
                dt.WriteXml(sw);
                testdata = sw.ToString();
            }

            Response.Clear();
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment;filename=test.zip");
            using (zip = new ZipFile())
            {
                zip.AddDirectoryByName("Data");                
                zip.AddFile(@"C:\TestProjects\TestFiles\readme.txt", "Data");
                zip.AddEntry("Export.xml", testdata);
                zip.Save(Response.OutputStream);
            }
            Response.End();

Here, the "testdata" XML document is added to the root of the archive as a file named Export.xml.

So, the question is, could someone please tell me,  how to add the "testdata" XML document to the Data folder as a file named Export.xml?
I'll be using this in an ASP.NET solution, where I have the data in memory, and I'll prefere to avoid the extra step of writing the data to physical files, as well as the cleanup afterwords.


Best Regards
henningtc