Click here to Skip to main content
15,616,341 members
Articles / Programming Languages / C#
Posted 13 Feb 2012

Tagged as


4 bookmarked

Zip archiving files after a cutoff date, but preserving directory structure

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Feb 2012CPOL
A small archiving utility

A basic little console app I threw together. You can simply drag a folder onto it to get it going. Great recursion example...

Uses the Ionic zip library for zip functions. (You will want to save the zip less often if dealing with large files.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;

namespace MoveAndArchive
    class Program
        /// <summary>
        /// Application entry point.
        /// </summary>
        /// <param name="args">Arguments array.</param>
        static void Main(string[] args)
                DirectoryInfo root;
                DateTime cutoff;
                ZipFile destZIP = new ZipFile(Directory.GetParent(
                    System.Reflection.Assembly.GetExecutingAssembly().Location) + 
                    @"\" + DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".zip");

                if (args.Length > 0)
                    root = new DirectoryInfo(args[0]);
                    Console.WriteLine("Source Directory: " + root.FullName);
                    Console.Write("Enter Source Directory: ");
                    root = new DirectoryInfo(Console.ReadLine());
                Console.Write("Enter a cutoff (last) date to copy files from (Enter for all): ");
                string cutoffString = Console.ReadLine();
                cutoff = (cutoffString == string.Empty ? DateTime.MaxValue : DateTime.Parse(cutoffString));
                MoveAll(destZIP, cutoff, root, root);
            catch (Exception ex)
                Console.WriteLine("\nERROR: " + ex.Message);

        /// <summary>
        /// Recursively move all files to local ZIP file.
        /// </summary>
        /// <param name="Zip">The ZIP file to write to.</param>
        /// <param name="Cutoff">The last modify-date of files to transfer.</param>
        /// <param name="Root">The root directory being read from.</param>
        /// <param name="Dir">The current directory.</param>
        static void MoveAll(ZipFile Zip, DateTime Cutoff, DirectoryInfo Root, DirectoryInfo Dir)
            string zipPath = Dir.FullName.Replace(Root.FullName, string.Empty);
            if (zipPath != string.Empty)
                Console.WriteLine("Directory: " + zipPath);

            FileInfo[] files = Dir.GetFiles();
            for (int i = 0; i < files.Length; i++)
                if (files[i].LastWriteTime > Cutoff)  continue;
                Console.WriteLine(DateTime.Now.ToString() + " ZIP: " + files[i].FullName);
                Zip.AddFile(files[i].FullName, zipPath == string.Empty ? @"\" : zipPath);

            foreach (DirectoryInfo d in Dir.GetDirectories())  MoveAll(Zip, Cutoff, Root, d);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralReason for my vote of 5 Useful tip Pin
ProEnggSoft24-Feb-12 20:07
ProEnggSoft24-Feb-12 20:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.