using System; using System.Globalization; using System.Text; using System.Threading; namespace ICSharpCode.SharpZipLib.Zip { #region Enumerations /// /// Determines how entries are tested to see if they should use Zip64 extensions or not. /// public enum UseZip64 { /// /// Zip64 will not be forced on entries during processing. /// /// An entry can have this overridden if required Off, /// /// Zip64 should always be used. /// On, /// /// #ZipLib will determine use based on entry values when added to archive. /// Dynamic, } /// /// The kind of compression used for an entry in an archive /// public enum CompressionMethod { /// /// A direct copy of the file contents is held in the archive /// Stored = 0, /// /// Common Zip compression method using a sliding dictionary /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees /// Deflated = 8, /// /// An extension to deflate with a 64KB window. Not supported by #Zip currently /// Deflate64 = 9, /// /// BZip2 compression. Not supported by #Zip. /// BZip2 = 11, /// /// WinZip special for AES encryption, Now supported by #Zip. /// WinZipAES = 99, } /// /// Identifies the encryption algorithm used for an entry /// public enum EncryptionAlgorithm { /// /// No encryption has been used. /// None = 0, /// /// Encrypted using PKZIP 2.0 or 'classic' encryption. /// PkzipClassic = 1, /// /// DES encryption has been used. /// Des = 0x6601, /// /// RC2 encryption has been used for encryption. /// RC2 = 0x6602, /// /// Triple DES encryption with 168 bit keys has been used for this entry. /// TripleDes168 = 0x6603, /// /// Triple DES with 112 bit keys has been used for this entry. /// TripleDes112 = 0x6609, /// /// AES 128 has been used for encryption. /// Aes128 = 0x660e, /// /// AES 192 has been used for encryption. /// Aes192 = 0x660f, /// /// AES 256 has been used for encryption. /// Aes256 = 0x6610, /// /// RC2 corrected has been used for encryption. /// RC2Corrected = 0x6702, /// /// Blowfish has been used for encryption. /// Blowfish = 0x6720, /// /// Twofish has been used for encryption. /// Twofish = 0x6721, /// /// RC4 has been used for encryption. /// RC4 = 0x6801, /// /// An unknown algorithm has been used for encryption. /// Unknown = 0xffff } /// /// Defines the contents of the general bit flags field for an archive entry. /// [Flags] public enum GeneralBitFlags { /// /// Bit 0 if set indicates that the file is encrypted /// Encrypted = 0x0001, /// /// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating) /// Method = 0x0006, /// /// Bit 3 if set indicates a trailing data desciptor is appended to the entry data /// Descriptor = 0x0008, /// /// Bit 4 is reserved for use with method 8 for enhanced deflation /// ReservedPKware4 = 0x0010, /// /// Bit 5 if set indicates the file contains Pkzip compressed patched data. /// Requires version 2.7 or greater. /// Patched = 0x0020, /// /// Bit 6 if set indicates strong encryption has been used for this entry. /// StrongEncryption = 0x0040, /// /// Bit 7 is currently unused /// Unused7 = 0x0080, /// /// Bit 8 is currently unused /// Unused8 = 0x0100, /// /// Bit 9 is currently unused /// Unused9 = 0x0200, /// /// Bit 10 is currently unused /// Unused10 = 0x0400, /// /// Bit 11 if set indicates the filename and /// comment fields for this file must be encoded using UTF-8. /// UnicodeText = 0x0800, /// /// Bit 12 is documented as being reserved by PKware for enhanced compression. /// EnhancedCompress = 0x1000, /// /// Bit 13 if set indicates that values in the local header are masked to hide /// their actual values, and the central directory is encrypted. /// /// /// Used when encrypting the central directory contents. /// HeaderMasked = 0x2000, /// /// Bit 14 is documented as being reserved for use by PKware /// ReservedPkware14 = 0x4000, /// /// Bit 15 is documented as being reserved for use by PKware /// ReservedPkware15 = 0x8000 } #endregion /// /// This class contains constants used for Zip format files /// public sealed class ZipConstants { #region Versions /// /// The version made by field for entries in the central header when created by this library /// /// /// This is also the Zip version for the library when comparing against the version required to extract /// for an entry. See . /// public const int VersionMadeBy = 51; // was 45 before AES /// /// The version made by field for entries in the central header when created by this library /// /// /// This is also the Zip version for the library when comparing against the version required to extract /// for an entry. See ZipInputStream.CanDecompressEntry. /// [Obsolete("Use VersionMadeBy instead")] public const int VERSION_MADE_BY = 51; /// /// The minimum version required to support strong encryption /// public const int VersionStrongEncryption = 50; /// /// The minimum version required to support strong encryption /// [Obsolete("Use VersionStrongEncryption instead")] public const int VERSION_STRONG_ENCRYPTION = 50; /// /// Version indicating AES encryption /// public const int VERSION_AES = 51; /// /// The version required for Zip64 extensions (4.5 or higher) /// public const int VersionZip64 = 45; #endregion #region Header Sizes /// /// Size of local entry header (excluding variable length fields at end) /// public const int LocalHeaderBaseSize = 30; /// /// Size of local entry header (excluding variable length fields at end) /// [Obsolete("Use LocalHeaderBaseSize instead")] public const int LOCHDR = 30; /// /// Size of Zip64 data descriptor /// public const int Zip64DataDescriptorSize = 24; /// /// Size of data descriptor /// public const int DataDescriptorSize = 16; /// /// Size of data descriptor /// [Obsolete("Use DataDescriptorSize instead")] public const int EXTHDR = 16; /// /// Size of central header entry (excluding variable fields) /// public const int CentralHeaderBaseSize = 46; /// /// Size of central header entry /// [Obsolete("Use CentralHeaderBaseSize instead")] public const int CENHDR = 46; /// /// Size of end of central record (excluding variable fields) /// public const int EndOfCentralRecordBaseSize = 22; /// /// Size of end of central record (excluding variable fields) /// [Obsolete("Use EndOfCentralRecordBaseSize instead")] public const int ENDHDR = 22; /// /// Size of 'classic' cryptographic header stored before any entry data /// public const int CryptoHeaderSize = 12; /// /// Size of cryptographic header stored before entry data /// [Obsolete("Use CryptoHeaderSize instead")] public const int CRYPTO_HEADER_SIZE = 12; #endregion #region Header Signatures /// /// Signature for local entry header /// public const int LocalHeaderSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); /// /// Signature for local entry header /// [Obsolete("Use LocalHeaderSignature instead")] public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); /// /// Signature for spanning entry /// public const int SpanningSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for spanning entry /// [Obsolete("Use SpanningSignature instead")] public const int SPANNINGSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for temporary spanning entry /// public const int SpanningTempSignature = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); /// /// Signature for temporary spanning entry /// [Obsolete("Use SpanningTempSignature instead")] public const int SPANTEMPSIG = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); /// /// Signature for data descriptor /// /// /// This is only used where the length, Crc, or compressed size isnt known when the /// entry is created and the output stream doesnt support seeking. /// The local entry cannot be 'patched' with the correct values in this case /// so the values are recorded after the data prefixed by this header, as well as in the central directory. /// public const int DataDescriptorSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for data descriptor /// /// /// This is only used where the length, Crc, or compressed size isnt known when the /// entry is created and the output stream doesnt support seeking. /// The local entry cannot be 'patched' with the correct values in this case /// so the values are recorded after the data prefixed by this header, as well as in the central directory. /// [Obsolete("Use DataDescriptorSignature instead")] public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); /// /// Signature for central header /// [Obsolete("Use CentralHeaderSignature instead")] public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); /// /// Signature for central header /// public const int CentralHeaderSignature = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); /// /// Signature for Zip64 central file header /// public const int Zip64CentralFileHeaderSignature = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); /// /// Signature for Zip64 central file header /// [Obsolete("Use Zip64CentralFileHeaderSignature instead")] public const int CENSIG64 = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); /// /// Signature for Zip64 central directory locator /// public const int Zip64CentralDirLocatorSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); /// /// Signature for archive extra data signature (were headers are encrypted). /// public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); /// /// Central header digitial signature /// public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); /// /// Central header digitial signature /// [Obsolete("Use CentralHeaderDigitalSignaure instead")] public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); /// /// End of central directory record signature /// public const int EndOfCentralDirectorySignature = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); /// /// End of central directory record signature /// [Obsolete("Use EndOfCentralDirectorySignature instead")] public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); #endregion /// /// The original Zip specification (https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT) states /// that file names should only be encoded with IBM Code Page 437 or UTF-8. /// In practice, most zip apps use OEM or system encoding (typically cp437 on Windows). /// Let's be good citizens and default to UTF-8 http://utf8everywhere.org/ /// static int defaultCodePage = Encoding.UTF8.CodePage; /// /// Default encoding used for string conversion. 0 gives the default system OEM code page. /// Using the default code page isnt the full solution neccessarily /// there are many variable factors, codepage 850 is often a good choice for /// European users, however be careful about compatability. /// public static int DefaultCodePage { get { return defaultCodePage; } set { if ((value < 0) || (value > 65535) || (value == 1) || (value == 2) || (value == 3) || (value == 42)) { throw new ArgumentOutOfRangeException("nameof(value)"); } defaultCodePage = value; } } /// /// Convert a portion of a byte array to a string. /// /// /// Data to convert to string /// /// /// Number of bytes to convert starting from index 0 /// /// /// data[0]..data[count - 1] converted to a string /// public static string ConvertToString(byte[] data, int count) { if (data == null) { return string.Empty; } return Encoding.GetEncoding(DefaultCodePage).GetString(data, 0, count); } /// /// Convert a byte array to string /// /// /// Byte array to convert /// /// /// dataconverted to a string /// public static string ConvertToString(byte[] data) { if (data == null) { return string.Empty; } return ConvertToString(data, data.Length); } /// /// Convert a byte array to string /// /// The applicable general purpose bits flags /// /// Byte array to convert /// /// The number of bytes to convert. /// /// dataconverted to a string /// public static string ConvertToStringExt(int flags, byte[] data, int count) { if (data == null) { return string.Empty; } if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { return Encoding.UTF8.GetString(data, 0, count); } else { return ConvertToString(data, count); } } /// /// Convert a byte array to string /// /// /// Byte array to convert /// /// The applicable general purpose bits flags /// /// dataconverted to a string /// public static string ConvertToStringExt(int flags, byte[] data) { if (data == null) { return string.Empty; } if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { return Encoding.UTF8.GetString(data, 0, data.Length); } else { return ConvertToString(data, data.Length); } } /// /// Convert a string to a byte array /// /// /// String to convert to an array /// /// Converted array public static byte[] ConvertToArray(string str) { if (str == null) { return new byte[0]; } return Encoding.GetEncoding(DefaultCodePage).GetBytes(str); } /// /// Convert a string to a byte array /// /// The applicable general purpose bits flags /// /// String to convert to an array /// /// Converted array public static byte[] ConvertToArray(int flags, string str) { if (str == null) { return new byte[0]; } if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { return Encoding.UTF8.GetBytes(str); } else { return ConvertToArray(str); } } /// /// Initialise default instance of ZipConstants /// /// /// Private to prevent instances being created. /// ZipConstants() { // Do nothing } } }