Xceed .NET Libraries Documentation
    Changing file properties during Zip

    Introduction

     

    Default behavior

     When zipping, the file properties like

    Change file metadata during Zip

     

        static void Example()
        {
          // Select a source folder
          AbstractFolder folder = new DiskFolder( @"DataFolderToZip" );
    
          // Select a zip file
          AbstractFile zipFile = new DiskFile( @"ZipChangeMetadataInEvent.zip" );
    
          // If the zip file already exists
          if( zipFile.Exists )
          {
            // Delete it
            zipFile.Delete();
          }
    
          // Wrap a logical zip archive around the zip file
          ZipArchive zip = new ZipArchive( zipFile );
    
          // Create events
          ZipEvents zipEvents = new ZipEvents();
    
          // Assign our method to handle the event where an item has completed processing
          zipEvents.ItemCompletion += new ItemProgressionEventHandler( OnItemCompletion );
    
          // Start a zip batch operation
          using( AutoBatchUpdate update = new AutoBatchUpdate( zip ) )
          {
            /* We get the current date/time once here to avoid getting it on each zipped item. It could hinder performance */
    
            // Get the current date/time
            DateTime now = DateTime.Now;
    
            // Zip the source files, hooking in events and a maximum allowable date/time as user data
            folder.CopyFilesTo( zipEvents, now, zip, true, true );
          }
        }
    
        static void OnItemCompletion( object sender, ItemProgressionEventArgs e )
        {
          /* At this point, the item has been zipped but it is still safe and efficient to update its metadata like
           * the last write date/time. Headers will simply be updated. The item's data will not need to be compressed
           * again */
    
          // Get the maximum allowable date/time for our zipped item
          DateTime maxAllowedDateTime = ( DateTime ) e.UserData;
    
          // If the zipped item has a last write date/time that is unacceptable to us
          if( e.TargetItem.LastWriteDateTime > maxAllowedDateTime )
          e{
            // Express the item as a ZippedFile
            ZippedFile zippedFile = ( ZippedFile ) e.TargetItem;
    
            /* The only situation where changing the last write date/time will not work if it the item was
             * zipped with "Zip Compatible encryption". This is a deprecated, not secure algorithm and should not be
             * used in new code anyway. Since zipping is under our control here, it will not happen. */
            System.Diagnostics.Debug.Assert( !( zippedFile.Encrypted && zippedFile.EncryptionMethod == EncryptionMethod.Compatible ) );
    
            // Update the zipped item last write date/time to our acceptable value
            zippedFile.LastWriteDateTime = maxAllowedDateTime;
    
            /* TODO: Other properties can be changed here too */
    //         zippedFile.CreationDateTime = myDate;
    //         zippedFile.LastAccessDateTime = myDate;
    //         zippedFile.Comment = "My comment";
    //         zippedFile.Name = "My name";
    //         zippedFile.ExtraHeaders = myExtraHeaders;
    //         zippedFile.EncryptionPassword = "My password";
    //         zippedFile.EncryptionMethod = myMethod;
    //         zippedFile.EncryptionStrength = myStrenngth;
    //         zippedFile.CompressionMethod = myCompressionMethod;
    //         zippedFile.CompressionLevel = myCompressionLevel;
          }
        }