Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/5ce31cfc8674f57debca328ab120e6ca to your computer and use it in GitHub Desktop.
Save conholdate-gists/5ce31cfc8674f57debca328ab120e6ca to your computer and use it in GitHub Desktop.
Create ZIP Archives and Add Files and Folders using C#

Learn how to create a ZIP archive and add files and folders using C#: https://blog.conholdate.com/2021/09/18/create-encrypted-zip-files-using-csharp/

The following topics are covered here:

  1. Create Encrypted ZIP Archives with AES Encryption
  2. Create Password-Protected ZIP Archives
  3. Encrypt Specific Files in ZIP Archives
  4. Encrypt Folders in ZIP Archives
  5. Create Encrypted ZIP Archives with Mixed Encryption
// create an archive
Archive archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES128)));
// Add files to the archive
archive.CreateEntry("abc.txt", "C:\\Files\\sample1.txt");
// save the archive
archive.Save("C:\\Files\\aes.zip");
// create an archive
Archive archive = new Archive();
// Add files to the archive
archive.CreateEntry("sample1.txt", "C:\\Files\\sample1.txt");
archive.CreateEntry("sample2.txt", "C:\\Files\\sample2.txt", false, new ArchiveEntrySettings(encryptionSettings: new TraditionalEncryptionSettings("123@abc")));
// save the archive
archive.Save("C:\\Files\\password_protcted.zip");
// create an archive
Archive archive = new Archive();
// Add file with AES encryption to the archive
archive.CreateEntry("sample1.txt", "C:\\Files\\sample1.txt", false, new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES128)));
// Add file with traditional encryption to the archive
archive.CreateEntry("sample2.txt", "C:\\Files\\sample2.txt", false, new ArchiveEntrySettings(encryptionSettings: new TraditionalEncryptionSettings("321")));
// Add file without encryption to the archive
archive.CreateEntry("sample3.txt", "C:\\Files\\sample2.txt");
// Add folder without encryption to the archive
archive.CreateEntries("C:\\Files\\MyFolder");
// save the archive
archive.Save("C:\\Files\\Mixed.zip");
// create an archive
Archive archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("123@45")));
// Add folder to the archive
archive.CreateEntries("C:\\Files\\MyFolder");
// save the archive
archive.Save("C:\\Files\\password_protcted_folder.zip");
// create an archive
Archive archive = new Archive(new ArchiveEntrySettings(encryptionSettings: new TraditionalEncryptionSettings("12345")));
// Add files to the archive
archive.CreateEntry("sample1.txt", "C:\\Files\\sample1.txt");
archive.CreateEntry("sample2.txt", "C:\\Files\\sample2.txt");
// save the archive
archive.Save("C:\\Files\\password_protcted.zip");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment