Ejemplo n.º 1
0
        /// <summary>
        /// Reads the configuration and creates the zipJobs
        /// </summary>
        /// <returns>bool - valid zipJobs are found in config</returns>
        public bool init()
        {
            bool success = false;

            lstJobs = new List <ZipJob>();

            string[] lstPathes       = strPathes.Split(';');
            string[] lstDaysToZip    = strDaysToZip.Split(';');
            string[] lstDaysToDelete = strDaysToDelete.Split(';');

            // create the zip jobs
            if (lstPathes.Count() == lstDaysToZip.Count() && lstPathes.Count() == lstDaysToDelete.Count())
            {
                for (int i = 0; i < lstPathes.Count(); i++)
                {
                    ZipJob job = new ZipJob();
                    job.RootPath        = lstPathes[i];
                    job.AgeDaysToZip    = Convert.ToInt32(lstDaysToZip[i]);
                    job.AgeDaysToDelete = Convert.ToInt32(lstDaysToDelete[i]);

                    // job is only done, if age to zip > 0
                    if (job.AgeDaysToZip > 0)
                    {
                        lstJobs.Add(job);
                    }
                }
            }

            //valid jobs are found
            if (lstJobs.Count > 0)
            {
                success = true;
            }

            return(success);
        }
Ejemplo n.º 2
0
        private void zipDirectory(DirectoryInfo parent, ZipJob job)
        {
            DirectoryInfo[] lst   = parent.GetDirectories();
            FileInfo[]      lstFi = parent.GetFiles();

            // first try to delete old files in folder
            if (job.AgeDaysToDelete > 0)
            {
                foreach (FileInfo fi in lstFi)
                {
                    TimeSpan diff = DateTime.Now - fi.CreationTime;

                    if (diff.TotalDays > job.AgeDaysToDelete)
                    {
                        fi.Delete();
                    }
                }
            }

            try
            {
                // No subfolders in folder anymore, try to zip the
                // the current parent, only inf parent is not the global root folder
                if (lst.Count() == 0 && parent.FullName != job.RootPath)
                {
                    DateTime dt = parent.CreationTime;

                    TimeSpan diff = DateTime.Now - dt;

                    // Zip if the folder is old enough to zip
                    if (diff.TotalDays > job.AgeDaysToZip)
                    {
                        string zipFilePath = parent.FullName + ".zip";

                        ZipFile zip = new ZipFile();

                        lstFi = parent.GetFiles();

                        foreach (FileInfo fi in lstFi)
                        {
                            zip.AddFile(fi.FullName, @"\");
                        }

                        zip.Save(zipFilePath);

                        // file is zipped, delete the files
                        foreach (FileInfo fi in lstFi)
                        {
                            fi.Delete();
                        }

                        // finally delete the folder
                        parent.Delete();
                    }
                }

                foreach (DirectoryInfo di in lst)
                {
                    zipDirectory(di, job);
                }
            }
            catch (Exception)
            { }
        }