Download files as Zip File in SharePoint


In this post we will see how to download selected document library items as zip file. The zip file creation api is from open source code of

Skill Level – Medium

Download files as Zip

When I am working on this requirement, I happened to check which was very helpful with its api to create zip file.
The next step is to get the selected document library items and just call the api to create zip file. We need to provide one button in ribbon so that users can click it to save the zip file. In the button click we will have the code to read selected items and generate zip file.

The same logic I found in this site But, download zip is not working for subsites and also to the sites that does not have managed path.

As many users are using that code, I am only modifying the code and maintain the credibility to The feature will still have the same name ‘DeviantPoint Download Zip Feature’ and the new code will be working for subsites and also for the sites that does not have managed path.

Following are the main methods to download as zip file, other code is just to create ribbon button and call these methods when button is clicked

using(MemoryStream ms = newMemoryStream())
    using(ZipFileBuilder builder = newZipFileBuilder(ms))
        foreach(intid initemsIDs)
            SPListItem item = library.GetItemById(id);
                AddFolder(builder, item.Folder, string.Empty);
                AddFile(builder, item.File, string.Empty);
privatestaticvoidAddFile(ZipFileBuilder builder, SPFile file, stringfolder)
    using(Stream fileStream = file.OpenBinaryStream())
        builder.Add(folder + "\\"+ file.Name, fileStream);
privatevoidAddFolder(ZipFileBuilder builder, SPFolder folder, stringparentFolder)
    stringfolderPath = parentFolder == string.Empty ? folder.Name : parentFolder + "\\"+folder.Name;
    foreach(SPFile file infolder.Files)
        AddFile(builder, file, folderPath);
    foreach(SPFolder subFolder infolder.SubFolders)
        AddFolder(builder, subFolder, folderPath);
privatevoidWriteStreamToResponse(MemoryStream ms)
    if(ms.Length > 0)
        stringfilename = DateTime.Now.ToFileTime().ToString() + ".zip";
        Response.AddHeader("Content-Length", ms.Length.ToString());
        Response.AddHeader("Content-Disposition", "attachment; filename="+ filename);
        Response.ContentType = "application/octet-stream";
        byte[] buffer = newbyte[65536];
        ms.Position = 0;
            num = ms.Read(buffer, 0, buffer.Length);
            Response.OutputStream.Write(buffer, 0, num);
        while(num > 0);

ZipFileBuilder is the class file that is created to call the Zip api and it is available in the source code
‘itemsIDs’ is the array of selected items of the document library that should be filled. When Download button is clicked, it is this array we fill.

If you do not want to use Zip file code to download, then write object model code to download files in the download button click event.

To summarize,

1) Download the code
2) Deploy the wsp ( Site collection level feature ‘DeviantPoint Download Zip Feature’ will be created)
3) Activate the sitecollection feature ‘DeviantPoint Download Zip Feature’
(Tip- siteurl/_layouts/ManageFeatures.aspx?Scope=Site will show you sitecollection features)
4) ‘Download as Zip’ button will be available in the ribbon to use


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