public void Compress(string sourceFilename, string targetFilename, FileMode fileMode, OutArchiveFormat archiveFormat, CompressionMethod compressionMethod, CompressionLevel compressionLevel, ZipEncryptionMethod zipEncryptionMethod, string password, int bufferSize, int preallocationPercent, bool check, Dictionary<string, string> customParameters) { bufferSize *= this._sectorSize; SevenZipCompressor compressor = new SevenZipCompressor(); compressor.FastCompression = true; compressor.ArchiveFormat = archiveFormat; compressor.CompressionMethod = compressionMethod; compressor.CompressionLevel = compressionLevel; compressor.DefaultItemName = Path.GetFileName(sourceFilename); compressor.DirectoryStructure = false; compressor.ZipEncryptionMethod = zipEncryptionMethod; foreach (var pair in customParameters) { compressor.CustomParameters[pair.Key] = pair.Value; } using (FileStream sourceFileStream = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, Win32.FileFlagNoBuffering | FileOptions.SequentialScan)) { using (FileStream targetFileStream = new FileStream(targetFilename, fileMode, FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.WriteThrough | Win32.FileFlagNoBuffering)) { this.Compress(compressor, sourceFileStream, targetFileStream, password, preallocationPercent, check, bufferSize); } } }
public void Encryption(ZipEncryptionMethod encryptionMethod) { const string tempName1 = "a.dat"; var target = new MemoryStream(); string tempFilePath = GetTempFilePath(); Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, tempName1); MakeTempFile(addFile, 1); try { var fastZip = new FastZip { Password = "******", EntryEncryptionMethod = encryptionMethod }; fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); var archive = new MemoryStream(target.ToArray()); using (ZipFile zf = new ZipFile(archive)) { zf.Password = "******"; Assert.AreEqual(1, zf.Count); ZipEntry entry = zf[0]; Assert.AreEqual(tempName1, entry.Name); Assert.AreEqual(1, entry.Size); Assert.IsTrue(zf.TestArchive(true)); Assert.IsTrue(entry.IsCrypted); switch (encryptionMethod) { case ZipEncryptionMethod.ZipCrypto: Assert.That(entry.AESKeySize, Is.Zero, "AES key size should be 0 for ZipCrypto encrypted entries"); break; case ZipEncryptionMethod.AES128: Assert.That(entry.AESKeySize, Is.EqualTo(128), "AES key size should be 128 for AES128 encrypted entries"); break; case ZipEncryptionMethod.AES256: Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES key size should be 256 for AES256 encrypted entries"); break; } } } finally { File.Delete(tempName1); } }
public static EncryptionAlgorithm GetZipEnriptionAlgorithm(ZipEncryptionMethod encryptionMethod) { switch (encryptionMethod) { case ZipEncryptionMethod.None: return(EncryptionAlgorithm.None); case ZipEncryptionMethod.PkZipWeak: return(EncryptionAlgorithm.PkzipWeak); case ZipEncryptionMethod.WinZipAes128: return(EncryptionAlgorithm.WinZipAes128); case ZipEncryptionMethod.WinZipAes256: return(EncryptionAlgorithm.WinZipAes256); case ZipEncryptionMethod.PkUnsupported: return(EncryptionAlgorithm.Unsupported); default: return(EncryptionAlgorithm.None); } }