Esempio n. 1
0
        public List<string> Zip()
        {
            int nbZip = 0;
            IEnumerable<XElement> zips = gConfig.GetElements("Backup/Zip");
            foreach (XElement xeZip in zips)
            {
                nbZip += xeZip.zXPathValues("Source/SourceDirectory").Count();
                nbZip += xeZip.zXPathValues("Source/SourceFile").Count();
            }


            List<string> zipFiles = new List<string>();
            int iZip = 0;
            foreach (XElement xeZip in zips)
            {
                if (gAbortTask) break;
                CompressionLevel compressionLevel = GetCompressionLevel(xeZip.zXPathValue("CompressionLevel"));
                string sZipDir = xeZip.zXPathValue("ZipDirectory");
                foreach (string sourceDir in xeZip.zXPathValues("Source/SourceDirectory"))
                {
                    if (gAbortTask) break;
                    gTaskProgress.SetProgressText("Zip " + sourceDir);
                    gTaskProgress.SetProgress(++iZip, nbZip);

                    if (!zDirectory.Exists(sourceDir))
                    {
                        //cTrace.Trace("Zip          : directory does'nt exist {0}", sourceDir);
                        gTaskTrace.WriteLine("Zip          : directory does'nt exist {0}", sourceDir);
                        continue;
                    }
                    DateTime t3 = DateTime.Now;
                    using (ZipFile zip = new ZipFile())
                    {
                        zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(ZipSaveProgress);
                        //zip.CompressionLevel = CompressionLevel.BestSpeed;
                        zip.CompressionLevel = compressionLevel;
                        zip.AddDirectory(sourceDir, "");
                        string sPath = zpath.PathSetDirectory(zPath.GetFileNameWithoutExtension(sourceDir) + ".zip", sZipDir);
                        string sDir = zPath.GetDirectoryName(sPath);
                        if (!zDirectory.Exists(sDir))
                            zDirectory.CreateDirectory(sDir);
                        zip.Save(sPath);
                        if (gAbortTask) break;
                        zipFiles.Add(sPath);
                        int nbFiles = zip.zNbFile();
                        //int nbDir = zip.zNbDirectory();
                        long compressedSize = zip.zCompressedSize();
                        long uncompressedSize = zip.zUncompressedSize();
                        double ratio = zip.zCompressionRatio();
                        TimeSpan t4 = DateTime.Now.Subtract(t3);
                        gTaskTrace.WriteLine("Zip          : {0,-40} - ratio {1,4:0.0} - nb files {2,5} - size {3,8} / {4,8} - {5:00}:{6:00}:{7:00}", sourceDir, ratio, nbFiles, GetSizeString(compressedSize), GetSizeString(uncompressedSize), t4.Hours, t4.Minutes, t4.Seconds);
                    }
                }

                foreach (string sourceFile in xeZip.zXPathValues("Source/SourceFile"))
                {
                    if (gAbortTask) break;
                    gTaskProgress.SetProgressText("Zip " + sourceFile);
                    gTaskProgress.SetProgress(++iZip, nbZip);

                    if (!zFile.Exists(sourceFile))
                    {
                        //cTrace.Trace("Zip          : file does'nt exist {0}", sourceFile);
                        gTaskTrace.WriteLine("Zip          : file does'nt exist {0}", sourceFile);
                        continue;
                    }
                    DateTime t3 = DateTime.Now;
                    using (ZipFile zip = new ZipFile())
                    {
                        zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(ZipSaveProgress);
                        //zip.CompressionLevel = CompressionLevel.BestSpeed;
                        zip.CompressionLevel = compressionLevel;
                        zip.AddFile(sourceFile, "");
                        string sPath = zpath.PathSetDirectory(zPath.GetFileNameWithoutExtension(sourceFile) + ".zip", sZipDir);
                        string sDir = zPath.GetDirectoryName(sPath);
                        if (!zDirectory.Exists(sDir))
                            zDirectory.CreateDirectory(sDir);
                        zip.Save(sPath);
                        if (gAbortTask) break;
                        zipFiles.Add(sPath);
                        int nbFiles = zip.zNbFile();
                        //int nbDir = zip.zNbDirectory();
                        long compressedSize = zip.zCompressedSize();
                        long uncompressedSize = zip.zUncompressedSize();
                        double ratio = zip.zCompressionRatio();
                        TimeSpan t4 = DateTime.Now.Subtract(t3);
                        gTaskTrace.WriteLine("Zip          : {0,-40} - ratio {1,4:0.0} - nb files {2,5} - size {3,8} / {4,8} - {5:00}:{6:00}:{7:00}", sourceFile, ratio, nbFiles, GetSizeString(compressedSize), GetSizeString(uncompressedSize), t4.Hours, t4.Minutes, t4.Seconds);
                    }
                }
            }
            return zipFiles;
        }