Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 14 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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)
        {
            try
            {
                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);
                }
                else
                {
                    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);
            }
            Console.WriteLine("\nCompleted.\n");
            Console.ReadKey();
        }
 
        /// <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);
                Zip.AddDirectoryByName(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);
                Zip.Save();
                File.Delete(files[i].FullName);
            }
 
            foreach (DirectoryInfo d in Dir.GetDirectories())  MoveAll(Zip, Cutoff, Root, d);
        }
    }
}

License

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

Share

About the Author

Paladin2000

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 Useful tip PinmemberProEnggSoft24-Feb-12 21:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 14 Feb 2012
Article Copyright 2012 by Paladin2000
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid