Пример #1
0
        private static void ExtractFiles(string zipToExtractPath, DirectoryInfo extractionDestinationPath, ZipExtraction zipExtractionMode)
        {
            var zipFiles = ZipManager.Extract(new MemoryStream(File.ReadAllBytes(zipToExtractPath)));

            foreach (var zipItem in zipFiles.Where(zipItem => zipItem.Data.Length > 0))
            {
                var names = zipItem.Filename.Split('/');
                // Carico l'effettivo nome file
                var fileName        = names.Length > 0 ? names.Last() : names[0];
                var destinationName = Path.Combine(extractionDestinationPath.FullName, fileName);

                // Definisco come gestire il nesting
                switch (zipExtractionMode)
                {
                // Estrazione piatta di tutti gli elementi
                case ZipExtraction.FlatExtraction:
                {
                    // Niente da fare: in fileName ho già il nome e non serve caricare le cartelle
                }
                break;

                case ZipExtraction.FullDirectoryExtraction:
                {
                    // Devo caricare tutte le directory (se presenti)
                    if (names.Length > 1)
                    {
                        var fileInfo = new FileInfo(Path.Combine(extractionDestinationPath.FullName, zipItem.Filename.Replace('/', '\\')));
                        if (fileInfo.Directory != null)
                        {
                            // Creo la directory se non esiste
                            fileInfo.Directory.Create();
                            // Aggiorno il path
                            destinationName = Path.Combine(destinationName, fileInfo.Directory.FullName, fileName);
                        }
                    }
                }
                break;

                case ZipExtraction.FullInternalDirectionExtraction:
                {
                    // Carico tutte le directory meno che la prima
                    // Devo caricare tutte le directory (se presenti)
                    if (names.Length > 2)
                    {
                        var fileInfo = new FileInfo(Path.Combine(extractionDestinationPath.FullName, zipItem.Filename.Substring(zipItem.Filename.IndexOf('/') + 1).Replace('/', '\\')));
                        if (fileInfo.Directory != null)
                        {
                            // Creo la directory se non esiste
                            fileInfo.Directory.Create();
                            // Aggiorno il path
                            destinationName = Path.Combine(destinationName, fileInfo.Directory.FullName, fileName);
                        }
                    }
                }
                break;
                }
                File.WriteAllBytes(destinationName, zipItem.Data);
            }

            // Elimino lo zip di aggiornamento
            File.Delete(zipToExtractPath);
        }
 public ActionMessage Update(string updatingObjectName, string zipUpdatePackagePath, ZipExtraction zipExtractionMode, string zipExtractTempPath, string extractionDestinationPath, bool doBackup, string backupPath, string executeCommandFullPath)
 {
     return(Channel.Update(updatingObjectName, zipUpdatePackagePath, zipExtractionMode, zipExtractTempPath,
                           extractionDestinationPath, doBackup, backupPath, executeCommandFullPath));
 }
Пример #3
0
        public ActionMessage Update(string updatingObjectName, string zipUpdatePackagePath, ZipExtraction zipExtractionMode, string zipExtractTempPath, string extractionDestinationPath, bool doBackup, string backupPath, string executeCommandFullPath)
        {
            var retvalMessage = new ActionMessage();

            //Gestione Backup
            if (doBackup)
            {
                retvalMessage.Message = BackupFiles(new DirectoryInfo(extractionDestinationPath), new DirectoryInfo(Path.Combine(extractionDestinationPath, backupPath)),
                                                    String.Format("{0}_{1}.zip", DateTime.Now.ToString("yyyyMMdd_HHmmss"), updatingObjectName)).FullName;
            }

            var zipExtractTemp = new DirectoryInfo(Path.Combine(extractionDestinationPath, zipExtractTempPath));

            //Estraggo i dati caricati
            ExtractFiles(zipUpdatePackagePath, zipExtractTemp, zipExtractionMode);

            //Sposto i file dalla directory temporanea alla directory di destinazione
            MoveFiles(zipExtractTemp, new DirectoryInfo(extractionDestinationPath));

            return(retvalMessage);
        }