Get/Read files from SharePoint document library and ZIP them using DotNetZip?

Nov 15, 2011 at 8:27 AM

I have files uploaded to sharepoint document library. Trying to use DotNetZip to get those files from document library, zip them and render the zip file. (Files in the document library are in PDF format).

    Response.Clear();      
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "filename=" + "MyFiles.zip");

    using (ZipFile zip = new ZipFile())

    {
    //Query the sharepoint document library and get SPFolder (folder in this case)

      foreach (SPFolder folder in userFolder.SubFolders)
                                    {
                                        foreach (SPFile file in folder.Files)
                                        {
                                            byte[] br = file.OpenBinary();

                                            using (MemoryStream stream = new MemoryStream())
                                            {
                                                stream.Write(br, 0, br.Length);
                                                zip.AddEntry(file.Name, stream);
                                            }                                           
                                        }
                                    }      
        zip.Save(Response.OutputStream);

After downloading the zip file I am not able to open the zip file. It shows "zip file is invalid".

Any thoughts?

Nov 15, 2011 at 9:29 AM

I got it figured it out from this http://dotnetzip.codeplex.com/discussions/259231

Thanks

Sep 18, 2012 at 5:33 PM

@naveengopisetty - would you mind posting your final code to accomplish this? I have reviewed the link provided but can not seem to accomplish this. I appreciate any assistance you might be able to provide.

Sep 18, 2012 at 6:59 PM

@jonesej I am trying to find the code for you. Been long time.  Mean while I find the code see if this link helps http://stackoverflow.com/questions/838397/stream-zip-file-mvc-net-start-streaming

What is the error you are getting? 

Sep 18, 2012 at 7:29 PM

@jonesej - I found the code.

                    using (SPSite site = new SPSite("URL"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
       
                            Response.Clear();
                            Response.ContentType = "application/zip";
                            Response.AddHeader("content-disposition", "attachment; filename=My.zip");

                            SPList docList = web.Lists["Docs"];
                            if (docList.RootFolder.SubFolders[folderName].Exists)
                            {
                                SPFolder userFolder = docList.RootFolder.SubFolders["foldername"];

                                using (ZipOutputStream zipOStream = new ZipOutputStream(Response.OutputStream))
                                {                                

                                    foreach (SPFolder folder in userFolder.SubFolders)
                                    {
                                        foreach (SPFile file in folder.Files)
                                        {
                                            byte[] br = file.OpenBinary();
                                            zipOStream.PutNextEntry(folder.Name + "\\" + file.Name);
                                            zipOStream.Write(br, 0, br.Length);
                                        }
                                    }
                                    zipOStream.Flush();
                                }
                               

                                Response.OutputStream.Flush();
                            }
                        }
                    }