Zip contents of a document library…

Hi Folks,

So here I am with another very interesting post which is about zipping the contents of a document library and saving them on a network or local location. Actually I am currently working on a huge MOSS based project for Trubiquity, it a huge application with very large number of employees. The basic objective of the application is to facilitate the clients (with multiple teams)  to manage their projects on a central location with all project management functionality, so SharePoint (MOSS) platform was chosen for it and rightly so, because of the fact that we need project management template with some out of the box feature including KPI, dashboards and many more….

Now the scanerio: There was a client request that whenever, he closes a project, he wants to zip the contents of the document library (in future, it may expand to any library) to any downloadable location. So I used SharePoint’s OM (object model) to achieve the functionality as this piece of code will eventually get wrapped inside a web-service so that it can be called from a remote location as well.

First the pre-Req: As the requirement is to zip all files and as document library can consist of multiple folder hierarchies, so first we need to create the same folder structure on any location (may be remote as well) from where  I will zip it. So I have used some J# classes as well to zip the folders.

Now the Solution with code and explanation: So here it is, the solution and code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.SharePoint;

using System.Collections;
using System.IO;

namespace CloseSiteFeature
    public partial class frmMain : Form
        SPSite aSite = new SPSite("http://vm-winserver");
        Hashtable ListsTable = new Hashtable(); // to bind a combo box with a datasource which will be a hashtable in this case.
        bool IsLoaded = false;

        string SavePath = @"\mac-ali3My Shared";
        string RootPath = string.Empty;

        BindingSource bs = new BindingSource();
        SPWeb aWeb;

        public frmMain()

        private void frmMain_Load(object sender, EventArgs e)
            aWeb = aSite.OpenWeb();

            foreach (SPList aList in aWeb.Lists)
                if (aList.Hidden == false && aList.Title.ToString() != "Form Templates")
                    if (aList.BaseType.ToString() == "DocumentLibrary" && aList.BaseTemplate.ToString() == "DocumentLibrary")
                        ListsTable.Add(aList.Title.ToString(), aList.RootFolder.ServerRelativeUrl.ToString());

            bs.DataSource = ListsTable;

            cmbDocLibs.DataSource = bs;
            cmbDocLibs.DisplayMember = "Key";
            cmbDocLibs.ValueMember = "Value";

            IsLoaded = true;


        System.IO.DirectoryInfo DirInfo = null;
        SPFolder aFolder;

        private void cmbDocLibs_SelectedIndexChanged(object sender, EventArgs e)
            if (IsLoaded)
                    aFolder = aWeb.Folders[cmbDocLibs.SelectedValue.ToString()];
                    Directory.CreateDirectory(SavePath + aFolder.Name.ToString());
                    DirInfo = new DirectoryInfo(SavePath + aFolder.Name.ToString());
                    RootPath = SavePath + "\" + aFolder.Name.ToString();
                    GetSPFolderContents(aFolder, RootPath);

           fos = new + aFolder.Name.ToString() +".zip");
           zos = new;
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@SavePath + aFolder.Name.ToString() + "\");
                    ZipDirectory(fos, zos, di, @SavePath + aFolder.Name.ToString() +"\");

                    MessageBox.Show("Successfully done!");

                catch (Exception ex)
                    DirInfo = new DirectoryInfo(SavePath + aFolder.Name.ToString());

        private void ZipDirectory( fos, zos,System.IO.DirectoryInfo di,String SRootDir)
            //to get file info from the directory
            System.IO.FileInfo[] fInfos = di.GetFiles();
            foreach(System.IO.FileInfo fInfo in fInfos)
                //Debug.Write(string.Format("Zipping {0}rn", fInfo.FullName));
                //give the zip entry or the folder arrangement for the file
                ze = new"\") + 1));
                //The DEFLATED method is the one of the methods to zip a file
                //Input stream for the file to zip
                fis = new;
                //Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
                CopyStream(fis, zos);
            //If the directory contains the sub directory the call the same procedure
            System.IO.DirectoryInfo[] dinfos = di.GetDirectories();
            foreach (System.IO.DirectoryInfo dinfo in dinfos)
                //Debug.Write(string.Format("Zipping directory {0}rn", dinfo.FullName));
                ZipEntry dirEntry = new ZipEntry(dinfo.FullName.Substring(SRootDir.LastIndexOf("\") + 1) + Path.DirectorySeparatorChar);
                ZipDirectory(fos, zos, dinfo, SRootDir);

        void CopyStream( src, dest)
   reader = new;
   writer = new;

            while (reader.ready())


        private void GetSPFolderContents(SPFolder Folder, string path)
            byte[] binFile;

            foreach (SPFile aFile in Folder.Files)
                binFile = aFile.OpenBinary();
                if (binFile.Length >= 0)
                    FileStream fs = new FileStream(path + "\" + aFile.Name.ToString(), FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(binFile, 0, binFile.Length);

            if (Folder.SubFolders.Count > 0)
                foreach (SPFolder subFolder in Folder.SubFolders)
                    if (subFolder.Name != "Forms" && subFolder.ParentFolder.Name.ToString() == path.ToString().Substring(path.LastIndexOf("\") + 1))
                        DirInfo = new DirectoryInfo(path);
                        RootPath = path;
                        GetSPFolderContents(subFolder, path + "\" + subFolder.Name.ToString());
                path = RootPath;

Happy SharePointing and please keep me posted so that I can make any further changes/enhancement if required. Thanks



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: