Web based multi-file zip extract

Jul 30, 2012 at 8:45 PM
Edited Jul 31, 2012 at 9:39 PM

So far I have used this library with great satisfaction. But right now I have reviewed the documentation and for the life of me cannot get the multiple files to extract to the web location. I have changed the web location to mapped files drives but I am still not getting any response.  Basically, If I comment out the using block the debugger runs and shows that all the rest of the code functions. As soon as I uncomment the using block I never enter the code during debug mode.

I have a event receiver that starts when a new item is added to a web library. If it is a zip file then it should unzip the contents (this will generally be anywhere from 30 to 100 invoices) back to the location it originated from.

All help appreciated.

using Ionic.Zip;

namespace CL.Receiver.Invoices.InvoiceZipAdded
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class InvoiceZipAdded : SPItemEventReceiver
    {
        /// <summary>
        /// An invoice zip file was added and requires unzipping
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            // read properties from the item after adding to the library
            base.ItemAdded(properties);
            string afterURL = properties.AfterUrl;
            string fileExtension = afterURL.Substring(afterURL.LastIndexOf('.') + 1);
            string fileName = afterURL.Substring(afterURL.LastIndexOf('/') + 1);

            // Use a mapped drive location (mapped to \\localhost in case the Zip tool cannot use UNC or Web folders
            string sourceFilePath = "Q:\\Invoices\\";
            string targetFilePath = "C:\\invoices\\";

            // use the UNC path if the Zip understands them
            //string sourceFilePath = "\\\\ausnb-747218\\shared documents\\";
            //string targetFilePath = "\\\\ausnb-747218\\shared documents\\";
            string sourceFile = sourceFilePath + fileName;

            // unzip it if it is a zip file and is from the correct directory
            if (fileExtension == "zip") 
            {
                if (afterURL == "Invoices/" + fileName)
                {
                    // test code line to ensure if statements are functioning
                    // string testFileName = fileName;
                    //it's a zip so use the ionic.zip.dll known as dotnetzip
                    
                    using (ZipFile zip = ZipFile.Read(sourceFile))
                    {
                        foreach (ZipEntry entry in zip)
                        {
                            entry.Extract(targetFilePath);
                        }
                    }
                }
            }
        }
    }
}

!