Xceed .NET Libraries Documentation
DefaultPasswordTextEncoding Property (ZipArchive)


Xceed.Zip Assembly > Xceed.Zip Namespace > ZipArchive Class : DefaultPasswordTextEncoding Property

Gets or sets a value that indicates the text encoding to use when processing passwords.

Syntax
'Declaration
 
Public Property DefaultPasswordTextEncoding As Encoding
'Usage
 
Dim instance As ZipArchive
Dim value As Encoding
 
instance.DefaultPasswordTextEncoding = value
 
value = instance.DefaultPasswordTextEncoding
public Encoding DefaultPasswordTextEncoding {get; set;}

Property Value

A Encoding object that represents the text encoding to use when processing passwords. By default, the value is a null reference (Nothing in Visual Basic).
Remarks
There is no accepted standard on how to encode non-ASCII characters in passwords. That means every zip tool/library can end up doing it differently. This is why it is strongly recommended to use ASCII characters only in passwords if compatibility is important as it's the only "standard" that all zip tools will honor.

If Xceed Zip components are the only tools used, non-ASCII passwords, there won't be an issue. But if the resulting zip file will be shared with another zip tool/library or vice-versa, it won't work because Xceed might encode passwords differently than other tools.
Example
using System;

using Xceed.FileSystem;
using Xceed.Zip;

namespace DocumentationExamples.Zip
{
  class DefaultPasswordTextEncodingProperty
  {
    static void DefaultPasswordTextEncodingExample()
    {
      string password = "kf/E@´d!i_p7/ow`TgK!SV1w65X<y#";

      // Setup a text encoding object that mimics the password encoding performed by 7-Zip
      System.Text.Encoding encoding = System.Text.Encoding.GetEncoding( 1252, new System.Text.EncoderExceptionFallback(), new System.Text.DecoderReplacementFallback( "_" ) );

      AbstractFolder baseFolder = new DiskFolder( @"D:\DocumentationExamples.Zip" );
      AbstractFile zipFile = baseFolder.GetFile( "DefaultPasswordTextEncodingExample.zip" );
      if( zipFile.Exists ) zipFile.Delete();

      ZipArchive zip = new ZipArchive( zipFile );

      zip.DefaultEncryptionMethod = EncryptionMethod.WinZipAes;
      zip.DefaultEncryptionPassword = password;
      zip.DefaultDecryptionPassword = password;

      // Make the archive use our own text encoding for the password
      zip.DefaultPasswordTextEncoding = encoding;

      using( new AutoBatchUpdate( zip ) )
      {
        AbstractFile file = new DiskFile( @"D:\SomeFile.dat" );

        file.CopyTo( zip, true );
      }

      AbstractFolder folder = baseFolder.GetFolder( "Output" );
      zip.CopyFilesTo( folder, true, true );
    }

  }
}
Imports Microsoft.VisualBasic
Imports System

Imports Xceed.FileSystem
Imports Xceed.Zip

Namespace DocumentationExamples.Zip
  Friend Class DefaultPasswordTextEncodingProperty
    Private Shared Sub DefaultPasswordTextEncodingExample()
      Dim password As String = "kf/E@�d!i_p7/ow`TgK!SV1w65X<y#"

      ' Setup a text encoding object that mimics the password encoding performed by 7-Zip
      Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(1252, New System.Text.EncoderExceptionFallback(), New System.Text.DecoderReplacementFallback("_"))

      Dim baseFolder As AbstractFolder = New DiskFolder("D:\DocumentationExamples.Zip")
      Dim zipFile As AbstractFile = baseFolder.GetFile("DefaultPasswordTextEncodingExample.zip")
      If zipFile.Exists Then
        zipFile.Delete()
      End If

      Dim zip As New ZipArchive(zipFile)

      zip.DefaultEncryptionMethod = EncryptionMethod.WinZipAes
      zip.DefaultEncryptionPassword = password
      zip.DefaultDecryptionPassword = password

      ' Make the archive use our own text encoding for the password
      zip.DefaultPasswordTextEncoding = encoding

      Using TempAutoBatchUpdate As AutoBatchUpdate = New AutoBatchUpdate(zip)
        Dim file As AbstractFile = New DiskFile("D:\SomeFile.dat")

        file.CopyTo(zip, True)
      End Using

      Dim folder As AbstractFolder = baseFolder.GetFolder("Output")
      zip.CopyFilesTo(folder, True, True)
    End Sub

  End Class
End Namespace
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

ZipArchive Class
ZipArchive Members