Zipping C #

I am trying to use GZipStream to create a gz file using C #. my problem is that I have a list containing strings. and I need to create a password protected zip file and paste a text file containing strings into it.
I do not want to create a text file, then zip it and then delete the text file. I want to create a password protected zip file that contains a text file.
any help?

EDITOR: I do this with a zipper. Now I need to set the pass for the created zip file. any help?

+4
source share
3 answers

You should consider using SharpZipLib . This is the open source .net library. It includes examples of creating a .gz or .zip file. Please note that you can directly write to a .zip file. You do not need to create an intermediate file on disk first.

Edit: (in response to your edit) SharpZipLib also supports mail passwords.

+3
source

Just create a StreamWriter by wrapping your GZipStream and write the text to it.

+3
source

GZipStream can be used to create a .gz file, but it is not the same as a .zip file.

To create password protected zip files, I think you need to go to a third-party library.

Here's how to do it using DotNetZip ...

 var sb = new System.Text.StringBuilder(); sb.Append("This is the text file..."); foreach (var item in listOfStrings) sb.Append(item); // sb now contains all the content that will be placed into // the text file entry inside the zip. using (var zip = new Ionic.Zip.ZipFile()) { // set the password on the zip (implicitly enables encryption) zip.Password = "Whatever.You.Like!!"; // optional: select strong encryption zip.Encryption = Ionic.Zip.EncryptionAlgorithm.WinZipAes256; // add an entry to the zip, specify a name, specify string content zip.AddEntry("NameOfFile.txt", sb.ToString()); // save the file zip.Save("MyFile.zip"); } 
0
source

Source: https://habr.com/ru/post/1305029/


All Articles