Addfile() adds recursive subdirectories

Apr 30, 2009 at 6:58 PM
I have the following code which I want to add various files (in the files_to_zip ArrayList) to a zip stream.

                Response.Clear();
                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "filename=Reports.zip");
                using (ZipFile zip = new ZipFile())
                {
                    foreach (string filepath in files_to_zip)
                    {
                        zip.AddFile(filepath);
                    }
                    zip.Save(Response.OutputStream);
                }
                Response.End();

It works fine except that it adds the subdirectories meaning that if one of the files was located in C:\Temp\File1.txt, when you would open up the zip file you would see a folder Temp in which File1.txt would be located.  I want all of my zipped files to be located on the root of the zip file.

In one of the examples (CreateZip.cs), I read the following comment which AFAIK mean that my code is correct (I'm using AddFile() rather than AddDirectory()):
                    // This logic does not recurse through sub-directories.
                    // If you are zipping up a directory, you may want to see the AddDirectory() method,
                    // which operates recursively.

Help?


Apr 30, 2009 at 7:11 PM
I figured it out.

foreach (string filepath in files_to_zip)
{
    FileInfo myFile = new FileInfo(filepath);
    zip.AddFile(myFile.FullName, "");
}
Coordinator
Apr 30, 2009 at 10:44 PM
Glad you figured it out.