Xceed .NET Libraries Documentation
In This Topic
    Changing file properties during Zip
    In This Topic

    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;
          }
        }
    Private Shared Sub Example()
        ' Select a source folder
        Dim folder As AbstractFolder = New DiskFolder("DataFolderToZip")
    
        ' Select a zip file
        Dim zipFile As AbstractFile = New DiskFile("ZipChangeMetadataInEvent.zip")
    
        ' If the zip file already exists
        If zipFile.Exists Then
            ' Delete it
            zipFile.Delete()
        End If
    
        ' Wrap a logical zip archive around the zip file
        Dim zip As New ZipArchive(zipFile)
    
        ' Create events
        Dim zipEvents As New ZipEvents()
    
        ' Assign our method to handle the event where an item has completed processing
        AddHandler zipEvents.ItemCompletion, AddressOf OnItemCompletion
    
        ' Start a zip batch operation
        Using update As 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
            Dim now As DateTime = 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)
        End Using
    End Sub
    
    Private Shared Sub OnItemCompletion(ByVal sender As Object, ByVal e As ItemProgressionEventArgs)
        '       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
        Dim maxAllowedDateTime As DateTime = CDate(e.UserData)
    
        ' If the zipped item has a last write date/time that is unacceptable to us
        If e.TargetItem.LastWriteDateTime > maxAllowedDateTime Then
            ' Express the item as a ZippedFile
            Dim zippedFile As ZippedFile = CType(e.TargetItem, ZippedFile)
    
            '         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(Not (zippedFile.Encrypted AndAlso zippedFile.EncryptionMethod Is EncryptionMethod.Compatible))
    
            ' Update the zipped item last write date/time to our acceptable value
            zippedFile.LastWriteDateTime = maxAllowedDateTime
        End If
    End Sub