Create and email ZIP files on the fly!

I was faced with a peculiar request tonight. One of my hosting clients wanted me to write a script that would automatically email him his Access database each night. Even though our servers are backed up on a daily basis, he wanted a copy sent through to him. After a bit of searching I came across a fantastic CFC created by Nate Nielson, http://www.webclarity.com, which creates ZIP files on the fly. As this guy's database is over 1MB in size I thought, perfect!

There are a couple of things that you'll need before implementing this tutorial though:

  1. The CFC component which is free and can be downloaded from Nate's site here: http://www.webclarity.com/developers/zip.cfm
  2. You need to have the function createObject enabled on your server
  3. You will also need to find about 5 minutes of spare time because that's how long it took me to get this working!


NB: by enabling createObject on a server a possible security implication raises its head in that users could access the Service Factory setting in the CF Adminstrator and view datasources, change passwords etc. For this reason some hosts may NOT allow you to use this function so check with them first!

OK. The CFC component itself is beautiful! Short, simple and quick. Everything a function should be so at this point it's only fair to offer congratulations to Nate for his creation. How you include the component in your page is totally up to you but I simply used <CFINCLUDE> to include it. I won't go into detail as to how the CFC component actually works as Nate covers this in detail on both his site and in the download itself.

Create a new file called email_zip.cfm or whatever and add the code below:

<!---include the zip cfc--->
<cfinclude template="zip.cfc" />

<!---set the path to the file that you want to zip up, obviously change the file path to suit--->
<cfparam name="dbFile" default="c:\domains\mydomain.com\db\myDatabase.mdb" />

<!---I wanted to give the zip file a unique name so all I'm doing here is creating a date stamp for the zip file. This is only one of many ways to do this --->
<cfset zipTS="#DateFormat(Now(), 'ddmmyy')#">

<!---set the path to the zip file you are going to create. Again the actual path will be up to you. The zip file is named and stamped with the date--->
<cfparam name="dbZip" default="c:\domains\mydomain.com\wwwroot\dbBackup-#zipTS#.zip" />

<!---the following script does four simple actions to actually create the zip file --->
<cfscript>
// create an instance of the zip component 
zipper = createObject("component", "zip");

// start a new zip file. This just uses the path we set above 
zipper.newZip("#dbZip#"); 

// add files to zip. Again this uses our file path above. 
zipper.addFile("#dbFile#");

// create the zip file 
zipper.createZip();
</cfscript>

<!---OK we now have our zip file sitting on the server so all we do is attach it to an email and off it goes! --->
<cfmail to="foo@bar.com" from="bar@foo.com" subject="Database Backup for #DateFormat(Now(), ' d mmmm yyyy')#">
    Attached to this email is the latest backup for your database
    <cfmailparam file="#dbZip#" />
</cfmail>

And that's it! All there is to it. The email gets sent out to the recipient with a compressed zip file attached. To add a bit of automation I simple created a scheduled task to run each night to send the email automatically.

Again thanks to Nate for a great CFC and I hope this helps others as much as it helped me. It certainly got a big thumbs up from my client!

Submitted by: Phil Williams
Email: support<at>openmindhosting.com
Web: http://www.openmindhosting.com



All ColdFusion Tutorials By Author: Phil Williams
  • Create and email ZIP files on the fly!
    This tutorial will allow you to zip up a file or files on your server and email them to you. The whole tutorial runs to less than 20 lines of code!
    Author: Phil Williams
    Views: 9,632
    Posted Date: Thursday, October 28, 2004
  • Currency Conversion using Web Services
    A very simple currency convertor that uses the latest exchange rates through the available Web Service
    Author: Phil Williams
    Views: 12,656
    Posted Date: Wednesday, April 21, 2004
  • Remote Reboots with ColdFusion MX
    This tutorial will tell you how to reboot your server without even logging in.
    Author: Phil Williams
    Views: 7,619
    Posted Date: Sunday, July 18, 2004
  • SE Friendly URL's
    Get the search engines to really dig deep into your site by replacing the ? and & in your dynamic URL's with /. Tricks the SE bot into thinking it's a regular folder and eats up your content!
    Author: Phil Williams
    Views: 21,774
    Posted Date: Wednesday, February 12, 2003