Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Zipping and Emailing Project – Using DotNetZip

Posted by Paul on January 30, 2011


What about having a utility that zips and emails your project folder content? I agree that the source control tools provides backup. But this utility can be used to easily take the backup of your private projects at home.

The Challenge

The project folder usually contains many other folders and with different file extensions. The .Net inbuilt zipping library can be used to compress a file. But not the entire file/folder structure like winzip. There are multiple choces for creating zip files like using J#/ZipPackage/Third Party/DotNetZip framework.

The Solution

My chosen solution to zip entire folder content can be the use of DotNetZip which is an open source library for zipping. There are numerous examples available in the DotNetZip download, but we trying to create a utility that will do zipping and emailing of the given folder. This project should be extensible too.

You can download the library from url: http://dotnetzip.codeplex.com/

The Design

The project has been divided into the following parts:

  • The Application UI

clip_image002

  • The Core Library (containing folder searching, zipping, emailing classes)

clip_image004

TDD

The development of the project was gone through Test Driven Development. You can examine the unit tests generated.

Classes Explained

The core class involved here is the Zipper class:

clip_image006

The Zipper class consists of the properties:

Property

Description

Folder

The folder to zip for

ExcludeFolders

Exclude the folders from zipping

ExcludeFileExtensions

Exclude the following file extensions from zipping

AfterZipActions

Contains the action items to be performed after ipping

The Zipper class contains the Zip() method which is the core one:

The Zip() method performs the following actions:

  • Searches the Folder recursively and creates all files/folders list
  • Zip the file/folder list to a temporary file using DotNetZp
  • Performs the actions on zip file like Email, Copy etc.
  • Deletes the temporary zip file
  • Returns the size of the zip file

public bool Zip()

{

// Ensure valid folder is specified

if (!Directory.Exists(Folder))

throw new ArgumentException("Invalid Folder!");

// Get the files

var files = _searcher.GetFiles(Folder, ExcludeFolders, ExcludeFileExtensions);

// Perform zip

using (ZipFile zipFile = new ZipFile())

{

foreach (string file in files)

zipFile.AddFile(file);

// Create output zip file path

GenerateZipFilePath();

if (File.Exists(ZipFilePath))

File.Delete(ZipFilePath);

zipFile.Save(ZipFilePath);

}

// Perform actions assigned (email, copy to folder etc.)

foreach (IAction action in this.AfterZipActions)

action.Perform(this);

// Set file size for later reference

FileSize = new FileInfo(ZipFilePath).Length;

// Delete the original zip file

File.Delete(ZipFilePath);

// Return true

return true;

}

The DotNetZip’s ZipFile class is used to do the zipping operation. The associated dll can be found in the project’s Library folder.

The Extensibility Factor

The Zipper class is designed in such a way that in future the developer wanted to add a new action – it can be easily incorporated. For this purpose the IAction interface is used.

clip_image008

The interface contains the Perform() method. The IAction interface is implemented by the Emailer and Copier classes. So in future a new action is required, we can create the new class implementing the IAction interface and add to the AfterZipActions list property of the Zipper class.

Gmail for Sending Mails

As gmail is supporting smtp service, if the user is having a gmail account it would be really easier. Anyway other mail services are also supported as the mail parameters can be changed over the screen. The changes will be saved to config file back when the application exits.

Saving user preferences

The config file contains default values which can be overridden by the end user.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="LatestFolder" value=""/>

<add key="ExcludeFolders" value="obj,bin;TestResults"/>

<add key="ExcludeFileExtensions" value="*.suo;*.zip"/>

<!–Email Options–>

<add key="SenderEmail" value=""/>

<add key="SenderPassword" value=""/>

<add key="SMTPServer" value="smtp.gmail.com"/>

<add key="Recipients" value=""/>

<!–Copy Options–>

<add key="DestinationFolder" value=""/>

</appSettings>

</configuration>

The recommended usage of the utility is to add to the Visual Studio Tools>External Tools menu. This would be lot easier in zipping the project while after development.

Advertisements

2 Responses to “Zipping and Emailing Project – Using DotNetZip”

  1. kiquenet said

    Great! Where can I download ProjectZip project?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s