CSharp · Console
using System;
using System.IO;
using System.IO.Compression;
using System.Timers;
namespace TrimZipperExample
{
public class Archive
{
public void CreateZip()
{
// root folder path
string basePath = @"D:\Documents\output";
try
{
// return subdirectories of specific directory
string[] directories = Directory.GetDirectories(basePath);
foreach (var dir in directories)
{
var subdir = new DirectoryInfo(dir);
var zipPath = $@"{basePath}\{subdir.Name}.zip";
var isZipFileExist = File.Exists(zipPath);
if (isZipFileExist)
{
Console.WriteLine($"Existing folder: {Path.GetFileName(zipPath)} already existed.");
}
else
{
// zip the incoming folder in the root folder
ZipFile.CreateFromDirectory(dir, zipPath, CompressionLevel.Fastest, true);
}
}
}
catch (Exception e)
{
Console.WriteLine($@"Error: {e.Message}");
}
}
}
public class FileWatcher
{
public void StartWatching(string path)
{
var rootWatcher = new FileSystemWatcher();
rootWatcher.Path = path;
rootWatcher.Created += new FileSystemEventHandler(OnRootFolderWatched);
rootWatcher.EnableRaisingEvents = true;
}
private void OnRootFolderWatched(object sender, FileSystemEventArgs e)
{
string fullPath = e.FullPath;
//string dirInfo = DirectoryInfo(fullPath);
// prevent new folder naming
if (e.FullPath.Contains("New folder"))
{
Console.WriteLine("naming not accepted.");
return;
}
FileAttributes attr = File.GetAttributes(fullPath);
//prevent file extension in root folder
if ((attr & FileAttributes.Directory) != FileAttributes.Directory &
((attr & FileAttributes.Archive) != FileAttributes.Archive))
{
Console.WriteLine("File not accept in root folder.");
return;
}
Console.WriteLine($"Folder/File: {e.FullPath} {e.ChangeType}");
// create another watcher for file creation and send event to timer
FileSystemWatcher subFolderWatcher = new FileSystemWatcher();
subFolderWatcher.Path = fullPath;
subFolderWatcher.EnableRaisingEvents = true;
subFolderWatcher.Filter = "*.*";
subFolderWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;
subFolderWatcher.IncludeSubdirectories = false;
// Timer setting
var aTimer = new System.Timers.Timer();
aTimer.Interval = 10000;
// Lambda == args => expression
// send event to subFolderWatcher
aTimer.Elapsed += new ElapsedEventHandler((s, evt) => OnTimedEvent(s, evt, subFolderWatcher));
aTimer.AutoReset = false;
aTimer.Enabled = true;
aTimer.Start();
// Send event to timer (sender, event of the subfolder bind to timer )
subFolderWatcher.Created += new FileSystemEventHandler((s, evt) => subFolderWatcher_Created(s, evt, aTimer));
}
private void OnTimedEvent(object sender, ElapsedEventArgs evt, FileSystemWatcher subFolderWatcher)
{
// kill it
subFolderWatcher.Dispose();
subFolderWatcher.EnableRaisingEvents = false;
// Explicit Casting
Timer timer = sender as Timer;
;
if (timer != null)
{
// Once time elapsed, zip the folder
Console.WriteLine($"time up. zip process at {evt.SignalTime}");
//Archive zip = new Archive();
//zip.CreateZip();
}
}
private void subFolderWatcher_Created(object sender, FileSystemEventArgs evt, Timer aTimer)
{
// if new file created, stop the timer
// then restart the timer
if (evt.FullPath.Contains(evt.Name))
{
aTimer.Stop();
Console.WriteLine($"restart the timer as {evt.Name} created on {DateTime.Now.ToString()}");
}
aTimer.Start();
aTimer.AutoReset = false;
}
}
internal class Program
{
private static void Main(string[] args)
{
//var directory = new Directory() { Path = @"D:\Documents\output"};
// Wait for the user to quit the program.
string path = @"D:\Documents\output";
var fileWatcher = new FileWatcher();
fileWatcher.StartWatching(path);
Console.WriteLine("Start watching... \n");
Console.ReadKey();
}
}
}
// end namespace;
// get current file content in the subdir
//var fileInfo = new FileInfo(subdir);
// get dir info of the current file
//var subdirInfo = fileInfo.Directory;