ZipNameTransform transforms names as per the Zip file naming convention.
The use of absolute names is supported although its use is not valid according to Zip naming conventions, and should not be used if maximum compatability is desired.
Inheritance: INameTransform
		public WebTemplateGeneratorHost(Hashtable globalContext)
		{
			_globalContext = globalContext;
			_outputStream = new MemoryStream();
			_zipOutput = new ZipOutputStream(_outputStream); 
			_zipOutput.SetLevel(9);
			_zipNameTransform = new ZipNameTransform();
		}
Esempio n. 2
0
        /// <summary>
        /// Create a zip file.
        /// </summary>
        /// <param name="zipFileName">The name of the zip file to create.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The file filter to apply.</param>
        /// <param name="directoryFilter">The directory filter to apply.</param>
        public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform        = new ZipNameTransform(true, sourceDirectory);
            this.sourceDirectory = sourceDirectory;

            outputStream = new ZipOutputStream(File.Create(zipFileName));
            try {
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
                }
                scanner.Scan(sourceDirectory, recurse);
            }
            finally {
                outputStream.Close();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="scanner">For performing the actual file system scan</param>
        /// <param name="leaveOpen">true to leave <paramref name="outputStream"/> open after the zip has been created, false to dispose it.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
        private void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, FileSystemScanner scanner, bool leaveOpen)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream))
            {
                outputStream_.SetLevel((int)CompressionLevel);
                outputStream_.IsStreamOwner = !leaveOpen;
                outputStream_.NameTransform = null;                 // all required transforms handled by us

                if (false == string.IsNullOrEmpty(password_) && EntryEncryptionMethod != ZipEncryptionMethod.None)
                {
                    outputStream_.Password = password_;
                }

                outputStream_.UseZip64 = UseZip64;
                scanner.ProcessFile   += ProcessFile;
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += ProcessDirectory;
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter,
                              string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream))
            {
#if !NETCF_1_0
                if (password_ != null)
                {
                    outputStream_.Password = password_;
                }
#endif

                outputStream_.UseZip64 = UseZip64;
                var scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += ProcessFile;
                if (CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += ProcessDirectory;
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Esempio n. 5
0
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;
            ZipOutputStream zipOutputStream = (outputStream_ = new ZipOutputStream(outputStream));

            try
            {
                if (password_ != null)
                {
                    outputStream_.Password = password_;
                }
                outputStream_.UseZip64 = UseZip64;
                FileSystemScanner fileSystemScanner = new FileSystemScanner(fileFilter, directoryFilter);
                fileSystemScanner.ProcessFile = (ProcessFileHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.ProcessFile, (global::System.Delegate) new ProcessFileHandler(ProcessFile));
                if (CreateEmptyDirectories)
                {
                    fileSystemScanner.ProcessDirectory = (ProcessDirectoryHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.ProcessDirectory, (global::System.Delegate) new ProcessDirectoryHandler(ProcessDirectory));
                }
                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        fileSystemScanner.FileFailure = (FileFailureHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.FileFailure, (global::System.Delegate)events_.FileFailure);
                    }
                    if (events_.DirectoryFailure != null)
                    {
                        fileSystemScanner.DirectoryFailure = (DirectoryFailureHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.DirectoryFailure, (global::System.Delegate)events_.DirectoryFailure);
                    }
                }
                fileSystemScanner.Scan(sourceDirectory, recurse);
            }
            finally
            {
                ((global::System.IDisposable)zipOutputStream)?.Dispose();
            }
        }
    public Stream Build (string archiveFileName)
    {
      ArgumentUtility.CheckNotNullOrEmpty ("archiveFileName", archiveFileName);

      using (var zipOutputStream = new ZipOutputStream (File.Create (archiveFileName)))
      {
        foreach (var fileInfo in _files)
        {
          var directoryName = fileInfo.Directory == null ? string.Empty : fileInfo.Directory.FullName;
          var nameTransform = new ZipNameTransform (directoryName);

          if (fileInfo is IFileInfo)
            AddFileToZipFile ((IFileInfo) fileInfo, zipOutputStream, nameTransform);
          else if (fileInfo is IDirectoryInfo)
            AddDirectoryToZipFile ((IDirectoryInfo) fileInfo, zipOutputStream, nameTransform);
        }
      }
      
      _files.Clear();
      _currentFileIndex = 0;
      _currentTotalValueExcludingCurrentFileValue = 0;

      return File.Open (archiveFileName, FileMode.Open, FileAccess.Read, FileShare.None);
    }
Esempio n. 7
0
 public void LengthBoundaryOk()
 {
     ZipNameTransform zt = new ZipNameTransform();
     string veryLong = "c:\\" + new string('x', 65535);
     try {
         zt.TransformDirectory(veryLong);
     }
     catch {
         Assert.Fail("Expected no exception");
     }
 }
Esempio n. 8
0
 /// <summary>
 /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
 /// </summary>
 /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
 public ZipEntryFactory(TimeSetting timeSetting)
 {
     timeSetting_   = timeSetting;
     nameTransform_ = new ZipNameTransform();
 }
Esempio n. 9
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using ( outputStream_ = new ZipOutputStream(outputStream) ) {

            #if !NETCF_1_0
                if ( password_ != null ) {
                    outputStream_.Password = password_;
                }
            #endif

                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileHandler(ProcessFile);
                if ( this.CreateEmptyDirectories ) {
                    scanner.ProcessDirectory += new ProcessDirectoryHandler(ProcessDirectory);
                }

                if (events_ != null) {
                    if ( events_.FileFailure != null ) {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if ( events_.DirectoryFailure != null ) {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Esempio n. 10
0
		/// <summary>
		/// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
		/// </summary>
		/// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
		public ZipEntryFactory(DateTime time)
		{
			timeSetting_ = TimeSetting.Fixed;
			FixedDateTime = time;
			nameTransform_ = new ZipNameTransform();
		}
Esempio n. 11
0
 public void TooLong()
 {
     ZipNameTransform zt = new ZipNameTransform();
     string veryLong = new string('x', 65536);
     try {
         zt.TransformDirectory(veryLong);
         Assert.Fail("Expected an exception");
     }
     catch (PathTooLongException) {
     }
 }
    private void AddFileToZipFile (IFileInfo fileInfo, ZipOutputStream zipOutputStream, ZipNameTransform nameTransform)
    {
      var fileStream = GetFileStream (fileInfo);
      var fileLength = fileInfo.Length;

      if (fileStream == null)
        return;

      var zipEntry = CreateZipFileEntry (fileInfo, nameTransform);
      zipOutputStream.PutNextEntry (zipEntry);

      using (fileStream)
      {

        var streamCopier = new StreamCopier();
        streamCopier.TransferProgress += (sender, args) => OnZippingProgress (args, fileInfo.FullName);

        if (!streamCopier.CopyStream (fileStream, zipOutputStream))
        {
          zipEntry.Size = -1;
          throw new AbortException();
        }
      }

      _currentFileIndex++;
      _currentTotalValueExcludingCurrentFileValue += fileLength;
    }
 private void AddDirectoryToZipFile (IDirectoryInfo directoryInfo, ZipOutputStream zipOutputStream, ZipNameTransform nameTransform)
 {
   foreach (var file in directoryInfo.GetFiles ())
     AddFileToZipFile (file, zipOutputStream, nameTransform);
   foreach (var directory in directoryInfo.GetDirectories ())
     AddDirectoryToZipFile (directory, zipOutputStream, nameTransform);
 }
Esempio n. 14
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using ( outputStream_ = new ZipOutputStream(outputStream) ) {
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
                if ( this.CreateEmptyDirectories ) {
                    scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
                }

                if (events_ != null) {
                    if ( events_.FileFailure != null ) {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if ( events_.DirectoryFailure != null ) {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Esempio n. 15
0
        public void NameTransforms()
        {
            INameTransform t = new ZipNameTransform(@"C:\Slippery");
            Assert.AreEqual("Pongo/Directory/", t.TransformDirectory(@"C:\Slippery\Pongo\Directory"), "Value should be trimmed and converted");
            Assert.AreEqual("PoNgo/Directory/", t.TransformDirectory(@"c:\slipperY\PoNgo\Directory"), "Trimming should be case insensitive");
            Assert.AreEqual("slippery/Pongo/Directory/", t.TransformDirectory(@"d:\slippery\Pongo\Directory"), "Trimming should be case insensitive");

            Assert.AreEqual("Pongo/File", t.TransformFile(@"C:\Slippery\Pongo\File"), "Value should be trimmed and converted");
        }
Esempio n. 16
0
        public void PathalogicalNames()
        {
            string badName = ".*:\\zy3$";

            Assert.IsFalse(ZipNameTransform.IsValidName(badName));

            ZipNameTransform t = new ZipNameTransform();
            string result = t.TransformFile(badName);

            Assert.IsTrue(ZipNameTransform.IsValidName(result));
        }
Esempio n. 17
0
		/// <summary>
		/// Create a zip file.
		/// </summary>
		/// <param name="zipFileName">The name of the zip file to create.</param>
		/// <param name="sourceDirectory">The directory to source files from.</param>
		/// <param name="recurse">True to recurse directories, false for no recursion.</param>
		/// <param name="fileFilter">The file filter to apply.</param>
		/// <param name="directoryFilter">The directory filter to apply.</param>
		public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
		{
			NameTransform = new ZipNameTransform(true, sourceDirectory);
			this.sourceDirectory = sourceDirectory;
			
			outputStream = new ZipOutputStream(File.Create(zipFileName));
			try {
				FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
				scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
				if ( this.CreateEmptyDirectories ) {
					scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
				}
				scanner.Scan(sourceDirectory, recurse);
			}
			finally {
				outputStream.Close();
			}
		}
		/// <summary>
		/// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
		/// </summary>
		/// <param name="outputStream">The stream to write archive data to.</param>
		/// <param name="sourceDirectory">The directory to source files from.</param>
		/// <param name="recurse">True to recurse directories, false for no recursion.</param>
		/// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
		/// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
		public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
		{
			NameTransform = new ZipNameTransform(sourceDirectory);
			sourceDirectory_ = sourceDirectory;

			using ( outputStream_ = new ZipOutputStream(outputStream) ) {


				if ( password_ != null ) {
					outputStream_.Password = password_;
				}


				outputStream_.UseZip64 = UseZip64;
				var scanner = new IsolatedFileSystemScanner(fileFilter, directoryFilter);
				scanner.ProcessFile += ProcessFile;
				if ( CreateEmptyDirectories ) {
					scanner.ProcessDirectory += ProcessDirectory;
				}
				
				if (events_ != null) {
					if ( events_.FileFailure != null ) {
						scanner.FileFailure += events_.FileFailure;
					}

					if ( events_.DirectoryFailure != null ) {
						scanner.DirectoryFailure += events_.DirectoryFailure;
					}
				}

				scanner.Scan(sourceDirectory, recurse);
			}
		}
Esempio n. 19
0
 void TestFile(ZipNameTransform t, string original, string expected)
 {
     string transformed = t.TransformFile(original);
     Assert.AreEqual(expected, transformed, "Should be equal");
 }
Esempio n. 20
0
		/// <summary>
		/// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
		/// </summary>
		/// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
		public ZipEntryFactory(TimeSetting timeSetting)
		{
			timeSetting_ = timeSetting;
			nameTransform_ = new ZipNameTransform();
		}
 /// <summary>
 /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
 /// </summary>
 /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
 public IsolatedZipEntryFactory()
 {
     nameTransform_ = new ZipNameTransform();
 }
Esempio n. 22
0
		/// <summary>
		/// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
		/// </summary>
		/// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
		public ZipEntryFactory()
		{
			nameTransform_ = new ZipNameTransform();
		}
Esempio n. 23
0
 /// <summary>
 /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
 /// </summary>
 /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
 public ZipEntryFactory()
 {
     nameTransform_ = new ZipNameTransform();
     isUnicodeText_ = ZipStrings.UseUnicode;
 }
Esempio n. 24
0
 /// <summary>
 /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
 /// </summary>
 /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
 public ZipEntryFactory()
 {
     nameTransform_ = new ZipNameTransform();
 }
Esempio n. 25
0
        public void Basic()
        {
            ZipNameTransform t = new ZipNameTransform();

            TestFile(t, "abcdef", "abcdef");
            TestFile(t, @"\\uncpath\d1\file1", "file1");
            TestFile(t, @"C:\absolute\file2", "absolute/file2");

            // This is ignored but could be converted to 'file3'
            TestFile(t, @"./file3", "./file3");

            // The following relative paths cant be handled and are ignored
            TestFile(t, @"../file3", "../file3");
            TestFile(t, @".../file3", ".../file3");

            // Trick filenames.
            TestFile(t, @".....file3", ".....file3");
            TestFile(t, @"c::file", "_file");
        }
Esempio n. 26
0
 /// <summary>
 /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
 /// </summary>
 /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
 public ZipEntryFactory(DateTime time)
 {
     timeSetting_   = TimeSetting.Fixed;
     FixedDateTime  = time;
     nameTransform_ = new ZipNameTransform();
 }
Esempio n. 27
0
 /// <summary>
 /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
 /// </summary>
 /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
 public ZipEntryFactory()
 {
     nameTransform_ = new ZipNameTransform();
     isUnicodeText_ = true;
 }