Exemplo n.º 1
0
        internal static void AddFilesToArchive(NewTuZipArchiveCommand cmdlet)
        {
            if (null == cmdlet.Filename || 0 == cmdlet.Filename.Length)
            {
                cmdlet.WriteError(
                    cmdlet,
                    "There are no files to archive",
                    "NoFiles",
                    ErrorCategory.InvalidArgument,
                    true);
            }

            if (string.IsNullOrEmpty(cmdlet.ArchiveName))
            {
                cmdlet.ArchiveName = "NewArchive";
            }

            try {
                using (var zipFile = new ZipFile()) {
                    foreach (string fileName in cmdlet.Filename)
                    {
                        cmdlet.WriteVerbose(
                            cmdlet,
                            "Adding file '" +
                            fileName +
                            "' to the archive");

                        if (System.IO.Directory.Exists(fileName))
                        {
                            if (null == cmdlet.PathInArchive)
                            {
                                zipFile.AddDirectory(fileName);
                            }
                            else
                            {
                                zipFile.AddDirectory(fileName, cmdlet.PathInArchive);
                            }
                        }
                        if (System.IO.File.Exists(fileName))
                        {
                            if (null == cmdlet.PathInArchive)
                            {
                                zipFile.AddFile(fileName);
                            }
                            else
                            {
                                zipFile.AddFile(fileName, cmdlet.PathInArchive);
                            }
                        }
//                        if (System.IO.Directory.Exists(fileName))
//                            string.IsNullOrEmpty(cmdlet.PathInArchive) ? zipFile.AddDirectory(fileName) : zipFile.AddDirectory(fileName, cmdlet.PathInArchive);
//                        if (System.IO.File.Exists(fileName))
//                            string.IsNullOrEmpty(cmdlet.PathInArchive) ? zipFile.AddFile(fileName) : zipFile.AddFile(file, cmdlet.PathInArchive);
                    }

                    zipFile.Comment = cmdlet.Comment;

                    zipFile.Save(cmdlet.ArchiveName + ".zip");

                    cmdlet.WriteObject(zipFile);
                }
            }
            catch (Exception eArchive) {
                cmdlet.WriteError(
                    cmdlet,
                    "Failed to create archive '" +
                    cmdlet.ArchiveName +
                    ".zip'." +
                    eArchive.Message,
                    "FailedToCreateArchive",
                    ErrorCategory.InvalidOperation,
                    true);
            }
        }
Exemplo n.º 2
0
        internal static void AddFilesToArchive(NewTuZipArchiveCommand cmdlet)
        {
            
            if (null == cmdlet.Filename || 0 == cmdlet.Filename.Length)
                cmdlet.WriteError(
                    cmdlet,
                    "There are no files to archive",
                    "NoFiles",
                    ErrorCategory.InvalidArgument,
                    true);
            
            if (string.IsNullOrEmpty(cmdlet.ArchiveName))
                cmdlet.ArchiveName = "NewArchive";
            
            try {
                using (var zipFile = new ZipFile()) {
                    
                    foreach (string fileName in cmdlet.Filename) {
                        
                        cmdlet.WriteVerbose(
                            cmdlet, 
                            "Adding file '" +
                            fileName +
                            "' to the archive");
                        
                        if (System.IO.Directory.Exists(fileName)) {
                            if (null == cmdlet.PathInArchive) {
                                zipFile.AddDirectory(fileName);
                            } else {
                                zipFile.AddDirectory(fileName, cmdlet.PathInArchive);
                            }
                        }
                        if (System.IO.File.Exists(fileName)) {
                            if (null == cmdlet.PathInArchive) {
                                zipFile.AddFile(fileName);
                            } else {
                                zipFile.AddFile(fileName, cmdlet.PathInArchive);
                            }
                        }
//                        if (System.IO.Directory.Exists(fileName))
//                            string.IsNullOrEmpty(cmdlet.PathInArchive) ? zipFile.AddDirectory(fileName) : zipFile.AddDirectory(fileName, cmdlet.PathInArchive);
//                        if (System.IO.File.Exists(fileName))
//                            string.IsNullOrEmpty(cmdlet.PathInArchive) ? zipFile.AddFile(fileName) : zipFile.AddFile(file, cmdlet.PathInArchive);
                    }
                    
                    zipFile.Comment = cmdlet.Comment;
                    
                    zipFile.Save(cmdlet.ArchiveName + ".zip");
                    
                    cmdlet.WriteObject(zipFile);
                }
            }
            catch (Exception eArchive) {
                
                cmdlet.WriteError(
                    cmdlet,
                    "Failed to create archive '" +
                    cmdlet.ArchiveName + 
                    ".zip'." +
                    eArchive.Message,
                    "FailedToCreateArchive",
                    ErrorCategory.InvalidOperation,
                    true);
            }
        }