| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Threading; |
| | | 4 | | |
| | | 5 | | namespace ICSharpCode.SharpZipLib.Zip |
| | | 6 | | { |
| | | 7 | | #region Enumerations |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Determines how entries are tested to see if they should use Zip64 extensions or not. |
| | | 11 | | /// </summary> |
| | | 12 | | public enum UseZip64 |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Zip64 will not be forced on entries during processing. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks>An entry can have this overridden if required <see cref="ZipEntry.ForceZip64"></see></remarks> |
| | | 18 | | Off, |
| | | 19 | | /// <summary> |
| | | 20 | | /// Zip64 should always be used. |
| | | 21 | | /// </summary> |
| | | 22 | | On, |
| | | 23 | | /// <summary> |
| | | 24 | | /// #ZipLib will determine use based on entry values when added to archive. |
| | | 25 | | /// </summary> |
| | | 26 | | Dynamic, |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The kind of compression used for an entry in an archive |
| | | 31 | | /// </summary> |
| | | 32 | | public enum CompressionMethod |
| | | 33 | | { |
| | | 34 | | /// <summary> |
| | | 35 | | /// A direct copy of the file contents is held in the archive |
| | | 36 | | /// </summary> |
| | | 37 | | Stored = 0, |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Common Zip compression method using a sliding dictionary |
| | | 41 | | /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees |
| | | 42 | | /// </summary> |
| | | 43 | | Deflated = 8, |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// An extension to deflate with a 64KB window. Not supported by #Zip currently |
| | | 47 | | /// </summary> |
| | | 48 | | Deflate64 = 9, |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// BZip2 compression. Not supported by #Zip. |
| | | 52 | | /// </summary> |
| | | 53 | | BZip2 = 11, |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// WinZip special for AES encryption, Now supported by #Zip. |
| | | 57 | | /// </summary> |
| | | 58 | | WinZipAES = 99, |
| | | 59 | | |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Identifies the encryption algorithm used for an entry |
| | | 64 | | /// </summary> |
| | | 65 | | public enum EncryptionAlgorithm |
| | | 66 | | { |
| | | 67 | | /// <summary> |
| | | 68 | | /// No encryption has been used. |
| | | 69 | | /// </summary> |
| | | 70 | | None = 0, |
| | | 71 | | /// <summary> |
| | | 72 | | /// Encrypted using PKZIP 2.0 or 'classic' encryption. |
| | | 73 | | /// </summary> |
| | | 74 | | PkzipClassic = 1, |
| | | 75 | | /// <summary> |
| | | 76 | | /// DES encryption has been used. |
| | | 77 | | /// </summary> |
| | | 78 | | Des = 0x6601, |
| | | 79 | | /// <summary> |
| | | 80 | | /// RC2 encryption has been used for encryption. |
| | | 81 | | /// </summary> |
| | | 82 | | RC2 = 0x6602, |
| | | 83 | | /// <summary> |
| | | 84 | | /// Triple DES encryption with 168 bit keys has been used for this entry. |
| | | 85 | | /// </summary> |
| | | 86 | | TripleDes168 = 0x6603, |
| | | 87 | | /// <summary> |
| | | 88 | | /// Triple DES with 112 bit keys has been used for this entry. |
| | | 89 | | /// </summary> |
| | | 90 | | TripleDes112 = 0x6609, |
| | | 91 | | /// <summary> |
| | | 92 | | /// AES 128 has been used for encryption. |
| | | 93 | | /// </summary> |
| | | 94 | | Aes128 = 0x660e, |
| | | 95 | | /// <summary> |
| | | 96 | | /// AES 192 has been used for encryption. |
| | | 97 | | /// </summary> |
| | | 98 | | Aes192 = 0x660f, |
| | | 99 | | /// <summary> |
| | | 100 | | /// AES 256 has been used for encryption. |
| | | 101 | | /// </summary> |
| | | 102 | | Aes256 = 0x6610, |
| | | 103 | | /// <summary> |
| | | 104 | | /// RC2 corrected has been used for encryption. |
| | | 105 | | /// </summary> |
| | | 106 | | RC2Corrected = 0x6702, |
| | | 107 | | /// <summary> |
| | | 108 | | /// Blowfish has been used for encryption. |
| | | 109 | | /// </summary> |
| | | 110 | | Blowfish = 0x6720, |
| | | 111 | | /// <summary> |
| | | 112 | | /// Twofish has been used for encryption. |
| | | 113 | | /// </summary> |
| | | 114 | | Twofish = 0x6721, |
| | | 115 | | /// <summary> |
| | | 116 | | /// RC4 has been used for encryption. |
| | | 117 | | /// </summary> |
| | | 118 | | RC4 = 0x6801, |
| | | 119 | | /// <summary> |
| | | 120 | | /// An unknown algorithm has been used for encryption. |
| | | 121 | | /// </summary> |
| | | 122 | | Unknown = 0xffff |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Defines the contents of the general bit flags field for an archive entry. |
| | | 127 | | /// </summary> |
| | | 128 | | [Flags] |
| | | 129 | | public enum GeneralBitFlags |
| | | 130 | | { |
| | | 131 | | /// <summary> |
| | | 132 | | /// Bit 0 if set indicates that the file is encrypted |
| | | 133 | | /// </summary> |
| | | 134 | | Encrypted = 0x0001, |
| | | 135 | | /// <summary> |
| | | 136 | | /// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating) |
| | | 137 | | /// </summary> |
| | | 138 | | Method = 0x0006, |
| | | 139 | | /// <summary> |
| | | 140 | | /// Bit 3 if set indicates a trailing data desciptor is appended to the entry data |
| | | 141 | | /// </summary> |
| | | 142 | | Descriptor = 0x0008, |
| | | 143 | | /// <summary> |
| | | 144 | | /// Bit 4 is reserved for use with method 8 for enhanced deflation |
| | | 145 | | /// </summary> |
| | | 146 | | ReservedPKware4 = 0x0010, |
| | | 147 | | /// <summary> |
| | | 148 | | /// Bit 5 if set indicates the file contains Pkzip compressed patched data. |
| | | 149 | | /// Requires version 2.7 or greater. |
| | | 150 | | /// </summary> |
| | | 151 | | Patched = 0x0020, |
| | | 152 | | /// <summary> |
| | | 153 | | /// Bit 6 if set indicates strong encryption has been used for this entry. |
| | | 154 | | /// </summary> |
| | | 155 | | StrongEncryption = 0x0040, |
| | | 156 | | /// <summary> |
| | | 157 | | /// Bit 7 is currently unused |
| | | 158 | | /// </summary> |
| | | 159 | | Unused7 = 0x0080, |
| | | 160 | | /// <summary> |
| | | 161 | | /// Bit 8 is currently unused |
| | | 162 | | /// </summary> |
| | | 163 | | Unused8 = 0x0100, |
| | | 164 | | /// <summary> |
| | | 165 | | /// Bit 9 is currently unused |
| | | 166 | | /// </summary> |
| | | 167 | | Unused9 = 0x0200, |
| | | 168 | | /// <summary> |
| | | 169 | | /// Bit 10 is currently unused |
| | | 170 | | /// </summary> |
| | | 171 | | Unused10 = 0x0400, |
| | | 172 | | /// <summary> |
| | | 173 | | /// Bit 11 if set indicates the filename and |
| | | 174 | | /// comment fields for this file must be encoded using UTF-8. |
| | | 175 | | /// </summary> |
| | | 176 | | UnicodeText = 0x0800, |
| | | 177 | | /// <summary> |
| | | 178 | | /// Bit 12 is documented as being reserved by PKware for enhanced compression. |
| | | 179 | | /// </summary> |
| | | 180 | | EnhancedCompress = 0x1000, |
| | | 181 | | /// <summary> |
| | | 182 | | /// Bit 13 if set indicates that values in the local header are masked to hide |
| | | 183 | | /// their actual values, and the central directory is encrypted. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <remarks> |
| | | 186 | | /// Used when encrypting the central directory contents. |
| | | 187 | | /// </remarks> |
| | | 188 | | HeaderMasked = 0x2000, |
| | | 189 | | /// <summary> |
| | | 190 | | /// Bit 14 is documented as being reserved for use by PKware |
| | | 191 | | /// </summary> |
| | | 192 | | ReservedPkware14 = 0x4000, |
| | | 193 | | /// <summary> |
| | | 194 | | /// Bit 15 is documented as being reserved for use by PKware |
| | | 195 | | /// </summary> |
| | | 196 | | ReservedPkware15 = 0x8000 |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | #endregion |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// This class contains constants used for Zip format files |
| | | 203 | | /// </summary> |
| | | 204 | | public sealed class ZipConstants |
| | | 205 | | { |
| | | 206 | | #region Versions |
| | | 207 | | /// <summary> |
| | | 208 | | /// The version made by field for entries in the central header when created by this library |
| | | 209 | | /// </summary> |
| | | 210 | | /// <remarks> |
| | | 211 | | /// This is also the Zip version for the library when comparing against the version required to extract |
| | | 212 | | /// for an entry. See <see cref="ZipEntry.CanDecompress"/>. |
| | | 213 | | /// </remarks> |
| | | 214 | | public const int VersionMadeBy = 51; // was 45 before AES |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// The version made by field for entries in the central header when created by this library |
| | | 218 | | /// </summary> |
| | | 219 | | /// <remarks> |
| | | 220 | | /// This is also the Zip version for the library when comparing against the version required to extract |
| | | 221 | | /// for an entry. See <see cref="ZipInputStream.CanDecompressEntry">ZipInputStream.CanDecompressEntry</see>. |
| | | 222 | | /// </remarks> |
| | | 223 | | [Obsolete("Use VersionMadeBy instead")] |
| | | 224 | | public const int VERSION_MADE_BY = 51; |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// The minimum version required to support strong encryption |
| | | 228 | | /// </summary> |
| | | 229 | | public const int VersionStrongEncryption = 50; |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// The minimum version required to support strong encryption |
| | | 233 | | /// </summary> |
| | | 234 | | [Obsolete("Use VersionStrongEncryption instead")] |
| | | 235 | | public const int VERSION_STRONG_ENCRYPTION = 50; |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Version indicating AES encryption |
| | | 239 | | /// </summary> |
| | | 240 | | public const int VERSION_AES = 51; |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// The version required for Zip64 extensions (4.5 or higher) |
| | | 244 | | /// </summary> |
| | | 245 | | public const int VersionZip64 = 45; |
| | | 246 | | #endregion |
| | | 247 | | |
| | | 248 | | #region Header Sizes |
| | | 249 | | /// <summary> |
| | | 250 | | /// Size of local entry header (excluding variable length fields at end) |
| | | 251 | | /// </summary> |
| | | 252 | | public const int LocalHeaderBaseSize = 30; |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Size of local entry header (excluding variable length fields at end) |
| | | 256 | | /// </summary> |
| | | 257 | | [Obsolete("Use LocalHeaderBaseSize instead")] |
| | | 258 | | public const int LOCHDR = 30; |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Size of Zip64 data descriptor |
| | | 262 | | /// </summary> |
| | | 263 | | public const int Zip64DataDescriptorSize = 24; |
| | | 264 | | |
| | | 265 | | /// <summary> |
| | | 266 | | /// Size of data descriptor |
| | | 267 | | /// </summary> |
| | | 268 | | public const int DataDescriptorSize = 16; |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Size of data descriptor |
| | | 272 | | /// </summary> |
| | | 273 | | [Obsolete("Use DataDescriptorSize instead")] |
| | | 274 | | public const int EXTHDR = 16; |
| | | 275 | | |
| | | 276 | | /// <summary> |
| | | 277 | | /// Size of central header entry (excluding variable fields) |
| | | 278 | | /// </summary> |
| | | 279 | | public const int CentralHeaderBaseSize = 46; |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Size of central header entry |
| | | 283 | | /// </summary> |
| | | 284 | | [Obsolete("Use CentralHeaderBaseSize instead")] |
| | | 285 | | public const int CENHDR = 46; |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Size of end of central record (excluding variable fields) |
| | | 289 | | /// </summary> |
| | | 290 | | public const int EndOfCentralRecordBaseSize = 22; |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Size of end of central record (excluding variable fields) |
| | | 294 | | /// </summary> |
| | | 295 | | [Obsolete("Use EndOfCentralRecordBaseSize instead")] |
| | | 296 | | public const int ENDHDR = 22; |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Size of 'classic' cryptographic header stored before any entry data |
| | | 300 | | /// </summary> |
| | | 301 | | public const int CryptoHeaderSize = 12; |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Size of cryptographic header stored before entry data |
| | | 305 | | /// </summary> |
| | | 306 | | [Obsolete("Use CryptoHeaderSize instead")] |
| | | 307 | | public const int CRYPTO_HEADER_SIZE = 12; |
| | | 308 | | #endregion |
| | | 309 | | |
| | | 310 | | #region Header Signatures |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Signature for local entry header |
| | | 314 | | /// </summary> |
| | | 315 | | public const int LocalHeaderSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Signature for local entry header |
| | | 319 | | /// </summary> |
| | | 320 | | [Obsolete("Use LocalHeaderSignature instead")] |
| | | 321 | | public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24); |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Signature for spanning entry |
| | | 325 | | /// </summary> |
| | | 326 | | public const int SpanningSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Signature for spanning entry |
| | | 330 | | /// </summary> |
| | | 331 | | [Obsolete("Use SpanningSignature instead")] |
| | | 332 | | public const int SPANNINGSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Signature for temporary spanning entry |
| | | 336 | | /// </summary> |
| | | 337 | | public const int SpanningTempSignature = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// Signature for temporary spanning entry |
| | | 341 | | /// </summary> |
| | | 342 | | [Obsolete("Use SpanningTempSignature instead")] |
| | | 343 | | public const int SPANTEMPSIG = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24); |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Signature for data descriptor |
| | | 347 | | /// </summary> |
| | | 348 | | /// <remarks> |
| | | 349 | | /// This is only used where the length, Crc, or compressed size isnt known when the |
| | | 350 | | /// entry is created and the output stream doesnt support seeking. |
| | | 351 | | /// The local entry cannot be 'patched' with the correct values in this case |
| | | 352 | | /// so the values are recorded after the data prefixed by this header, as well as in the central directory. |
| | | 353 | | /// </remarks> |
| | | 354 | | public const int DataDescriptorSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Signature for data descriptor |
| | | 358 | | /// </summary> |
| | | 359 | | /// <remarks> |
| | | 360 | | /// This is only used where the length, Crc, or compressed size isnt known when the |
| | | 361 | | /// entry is created and the output stream doesnt support seeking. |
| | | 362 | | /// The local entry cannot be 'patched' with the correct values in this case |
| | | 363 | | /// so the values are recorded after the data prefixed by this header, as well as in the central directory. |
| | | 364 | | /// </remarks> |
| | | 365 | | [Obsolete("Use DataDescriptorSignature instead")] |
| | | 366 | | public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24); |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// Signature for central header |
| | | 370 | | /// </summary> |
| | | 371 | | [Obsolete("Use CentralHeaderSignature instead")] |
| | | 372 | | public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Signature for central header |
| | | 376 | | /// </summary> |
| | | 377 | | public const int CentralHeaderSignature = 'P' | ('K' << 8) | (1 << 16) | (2 << 24); |
| | | 378 | | |
| | | 379 | | /// <summary> |
| | | 380 | | /// Signature for Zip64 central file header |
| | | 381 | | /// </summary> |
| | | 382 | | public const int Zip64CentralFileHeaderSignature = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Signature for Zip64 central file header |
| | | 386 | | /// </summary> |
| | | 387 | | [Obsolete("Use Zip64CentralFileHeaderSignature instead")] |
| | | 388 | | public const int CENSIG64 = 'P' | ('K' << 8) | (6 << 16) | (6 << 24); |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Signature for Zip64 central directory locator |
| | | 392 | | /// </summary> |
| | | 393 | | public const int Zip64CentralDirLocatorSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Signature for archive extra data signature (were headers are encrypted). |
| | | 397 | | /// </summary> |
| | | 398 | | public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Central header digitial signature |
| | | 402 | | /// </summary> |
| | | 403 | | public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); |
| | | 404 | | |
| | | 405 | | /// <summary> |
| | | 406 | | /// Central header digitial signature |
| | | 407 | | /// </summary> |
| | | 408 | | [Obsolete("Use CentralHeaderDigitalSignaure instead")] |
| | | 409 | | public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// End of central directory record signature |
| | | 413 | | /// </summary> |
| | | 414 | | public const int EndOfCentralDirectorySignature = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// End of central directory record signature |
| | | 418 | | /// </summary> |
| | | 419 | | [Obsolete("Use EndOfCentralDirectorySignature instead")] |
| | | 420 | | public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); |
| | | 421 | | #endregion |
| | | 422 | | |
| | | 423 | | /// <remarks> |
| | | 424 | | /// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc. |
| | | 425 | | /// But sometimes it yields the special value of 1 which is nicknamed <c>CodePageNoOEM</c> in <see cref="Encoding"/> |
| | | 426 | | /// This was observed on Ukranian and Hindu systems. |
| | | 427 | | /// Given this value, <see cref="Encoding.GetEncoding(int)"/> throws an <see cref="ArgumentException"/>. |
| | | 428 | | /// So replace it with some fallback, e.g. 437 which is the default cpcp in a console in a default Windows installat |
| | | 429 | | /// </remarks> |
| | 1 | 430 | | static int defaultCodePage = |
| | 1 | 431 | | // these values cause ArgumentException in subsequent calls to Encoding::GetEncoding() |
| | 1 | 432 | | ((Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 1) || (Thread.CurrentThread.CurrentCulture.TextInfo. |
| | 1 | 433 | | ? 437 // The default OEM encoding in a console in a default Windows installation, as a fallback. |
| | 1 | 434 | | : Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage; |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Default encoding used for string conversion. 0 gives the default system OEM code page. |
| | | 438 | | /// Dont use unicode encodings if you want to be Zip compatible! |
| | | 439 | | /// Using the default code page isnt the full solution neccessarily |
| | | 440 | | /// there are many variable factors, codepage 850 is often a good choice for |
| | | 441 | | /// European users, however be careful about compatability. |
| | | 442 | | /// </summary> |
| | | 443 | | public static int DefaultCodePage { |
| | | 444 | | get { |
| | 263830 | 445 | | return defaultCodePage; |
| | | 446 | | } |
| | | 447 | | set { |
| | 1 | 448 | | if ((value < 0) || (value > 65535) || |
| | 1 | 449 | | (value == 1) || (value == 2) || (value == 3) || (value == 42)) { |
| | 0 | 450 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 451 | | } |
| | | 452 | | |
| | 1 | 453 | | defaultCodePage = value; |
| | 1 | 454 | | } |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Convert a portion of a byte array to a string. |
| | | 459 | | /// </summary> |
| | | 460 | | /// <param name="data"> |
| | | 461 | | /// Data to convert to string |
| | | 462 | | /// </param> |
| | | 463 | | /// <param name="count"> |
| | | 464 | | /// Number of bytes to convert starting from index 0 |
| | | 465 | | /// </param> |
| | | 466 | | /// <returns> |
| | | 467 | | /// data[0]..data[count - 1] converted to a string |
| | | 468 | | /// </returns> |
| | | 469 | | public static string ConvertToString(byte[] data, int count) |
| | | 470 | | { |
| | 131956 | 471 | | if (data == null) { |
| | 0 | 472 | | return string.Empty; |
| | | 473 | | } |
| | | 474 | | |
| | 131956 | 475 | | return Encoding.GetEncoding(DefaultCodePage).GetString(data, 0, count); |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Convert a byte array to string |
| | | 480 | | /// </summary> |
| | | 481 | | /// <param name="data"> |
| | | 482 | | /// Byte array to convert |
| | | 483 | | /// </param> |
| | | 484 | | /// <returns> |
| | | 485 | | /// <paramref name="data">data</paramref>converted to a string |
| | | 486 | | /// </returns> |
| | | 487 | | public static string ConvertToString(byte[] data) |
| | | 488 | | { |
| | 12 | 489 | | if (data == null) { |
| | 0 | 490 | | return string.Empty; |
| | | 491 | | } |
| | 12 | 492 | | return ConvertToString(data, data.Length); |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | /// <summary> |
| | | 496 | | /// Convert a byte array to string |
| | | 497 | | /// </summary> |
| | | 498 | | /// <param name="flags">The applicable general purpose bits flags</param> |
| | | 499 | | /// <param name="data"> |
| | | 500 | | /// Byte array to convert |
| | | 501 | | /// </param> |
| | | 502 | | /// <param name="count">The number of bytes to convert.</param> |
| | | 503 | | /// <returns> |
| | | 504 | | /// <paramref name="data">data</paramref>converted to a string |
| | | 505 | | /// </returns> |
| | | 506 | | public static string ConvertToStringExt(int flags, byte[] data, int count) |
| | | 507 | | { |
| | 65956 | 508 | | if (data == null) { |
| | 0 | 509 | | return string.Empty; |
| | | 510 | | } |
| | | 511 | | |
| | 65956 | 512 | | if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { |
| | 5 | 513 | | return Encoding.UTF8.GetString(data, 0, count); |
| | | 514 | | } else { |
| | 65951 | 515 | | return ConvertToString(data, count); |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | /// <summary> |
| | | 520 | | /// Convert a byte array to string |
| | | 521 | | /// </summary> |
| | | 522 | | /// <param name="data"> |
| | | 523 | | /// Byte array to convert |
| | | 524 | | /// </param> |
| | | 525 | | /// <param name="flags">The applicable general purpose bits flags</param> |
| | | 526 | | /// <returns> |
| | | 527 | | /// <paramref name="data">data</paramref>converted to a string |
| | | 528 | | /// </returns> |
| | | 529 | | public static string ConvertToStringExt(int flags, byte[] data) |
| | | 530 | | { |
| | 66000 | 531 | | if (data == null) { |
| | 0 | 532 | | return string.Empty; |
| | | 533 | | } |
| | | 534 | | |
| | 66000 | 535 | | if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { |
| | 7 | 536 | | return Encoding.UTF8.GetString(data, 0, data.Length); |
| | | 537 | | } else { |
| | 65993 | 538 | | return ConvertToString(data, data.Length); |
| | | 539 | | } |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | /// <summary> |
| | | 543 | | /// Convert a string to a byte array |
| | | 544 | | /// </summary> |
| | | 545 | | /// <param name="str"> |
| | | 546 | | /// String to convert to an array |
| | | 547 | | /// </param> |
| | | 548 | | /// <returns>Converted array</returns> |
| | | 549 | | public static byte[] ConvertToArray(string str) |
| | | 550 | | { |
| | 131891 | 551 | | if (str == null) { |
| | 17 | 552 | | return new byte[0]; |
| | | 553 | | } |
| | | 554 | | |
| | 131874 | 555 | | return Encoding.GetEncoding(DefaultCodePage).GetBytes(str); |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | /// <summary> |
| | | 559 | | /// Convert a string to a byte array |
| | | 560 | | /// </summary> |
| | | 561 | | /// <param name="flags">The applicable <see cref="GeneralBitFlags">general purpose bits flags</see></param> |
| | | 562 | | /// <param name="str"> |
| | | 563 | | /// String to convert to an array |
| | | 564 | | /// </param> |
| | | 565 | | /// <returns>Converted array</returns> |
| | | 566 | | public static byte[] ConvertToArray(int flags, string str) |
| | | 567 | | { |
| | 131777 | 568 | | if (str == null) { |
| | 0 | 569 | | return new byte[0]; |
| | | 570 | | } |
| | | 571 | | |
| | 131777 | 572 | | if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { |
| | 12 | 573 | | return Encoding.UTF8.GetBytes(str); |
| | | 574 | | } else { |
| | 131765 | 575 | | return ConvertToArray(str); |
| | | 576 | | } |
| | | 577 | | } |
| | | 578 | | |
| | | 579 | | |
| | | 580 | | /// <summary> |
| | | 581 | | /// Initialise default instance of <see cref="ZipConstants">ZipConstants</see> |
| | | 582 | | /// </summary> |
| | | 583 | | /// <remarks> |
| | | 584 | | /// Private to prevent instances being created. |
| | | 585 | | /// </remarks> |
| | 0 | 586 | | ZipConstants() |
| | | 587 | | { |
| | | 588 | | // Do nothing |
| | 0 | 589 | | } |
| | | 590 | | } |
| | | 591 | | } |