Welcome to AspAdvice Sign in | Join | Help

Generate Zip Files With MSBuild and Web Deployment Projects

Create your Class Library. This is not any different than any class library with the exception that you reference the Microsoft.Build.Utilities and Microsoft.Build.Framework and your class must inherit Task Class. You will need ShapZibLib as well.

Example CreateZipFile Class:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using ICSharpCode.SharpZipLib.Zip;

namespace RadDev.MSBuildClassLibrary
{
    public class CreateZipFile : Task
    {
        private const string STR_TheSpecifiedPathWasNotFound = "The specified path was not found!";
        private string _pathToZipFile = string.Empty;
        private string _ziFileName = string.Empty;

        [Required]
        public string PathToZipFile
        {
            get
            {
                return _pathToZipFile;
            }
            set
            {
                _pathToZipFile = value;
            }
        }

        [Required]
        public string ZiFileName
        {
            get
            {
                return _ziFileName;
            }
            set
            {
                _ziFileName = value;
            }
        }

        public override bool Execute()
        {
            bool shouldReturn;
            bool result = ValidateDirectoryExist(out shouldReturn);
            if (shouldReturn)
                throw new Exception(STR_TheSpecifiedPathWasNotFound);

            try
            {
                string[] fileNames = Directory.GetFiles(_pathToZipFile);
                ZipOutputStream stream = new ZipOutputStream(File.Create(_pathToZipFile + _ziFileName));
                stream.SetLevel(9); // Max Compression

                byte[] bufferSize = new byte[4096];

                foreach (string file in fileNames)
                {
                    ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
                    zipEntry.DateTime = DateTime.Now.ToUniversalTime();
                    stream.PutNextEntry(zipEntry);

                    try
                    {
                        FileStream fileStream = File.OpenRead(file);
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fileStream.Read(bufferSize, 0, bufferSize.Length);
                            stream.Write(bufferSize, 0, sourceBytes);
                        }
                        while(sourceBytes > 0);
                        PerformCleanUp(fileStream);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                PerformCleanUp(stream);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        /// <summary>
        /// Validates the directory exist.
        /// </summary>
        /// <param name="shouldReturn">if set to <c>true</c> [should return].</param>
        /// <returns>true or false</returns>
        private bool ValidateDirectoryExist(out bool shouldReturn)
        {
            shouldReturn = false;
            if (!Directory.Exists(_pathToZipFile))
            {
                shouldReturn = true;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Performs the clean up.
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        private static void PerformCleanUp(FileStream fileStream)
        {
            fileStream.Close();
            fileStream.Dispose();
        }

        /// <summary>
        /// Performs the clean up.
        /// </summary>
        /// <param name="stream">The stream.</param>
        private static void PerformCleanUp(ZipOutputStream stream)
        {
            stream.Finish();
            stream.Close();
            stream.Dispose();
        }
    }
}

Next open the Web Deployment Project file for editing with Visual Studio 2005 and at the following:

<UsingTask TaskName="RadDev.MSBuildClassLibrary.CreateZipFile" AssemblyFile="RadDev.MSBuildClassLibrary.dll"/>
    <Target Name="AfterBuild">
        <CreateZipFile PathToZipFile="C:\MyDevelopment\RadDev\RadDev_deploy\Release" ZiFileName="currentbuild.zip" />
    </Target>
Sponsor
Published Sunday, June 10, 2007 2:16 PM by sswafford

Comments

New Comments to this post are disabled