我正在尝试使用GZipStream来使用c#创建一个gz文件。 我的问题是我有一个包含字符串的列表。我需要创建一个受密码保护的zip文件,并在其中放入一个包含字符串的文本文件 我不想创建文本文件,然后压缩它,然后删除文本文件。我想直接创建一个包含文本文件的密码保护的zip文件 任何帮助?
编辑:我完成了拉链的东西。现在我需要为创建的zip文件设置一个pass。有什么帮助吗?答案 0 :(得分:3)
只需创建StreamWriter
包裹GZipStream
,然后将文字写入其中。
答案 1 :(得分:3)
您应该考虑使用SharpZipLib。它是一个开源的.net压缩库。它包含有关如何创建.gz
或.zip
文件的示例。请注意,您可以直接写入.zip文件。您不需要首先在磁盘上创建中间文件。
编辑:(响应您的编辑)SharpZipLib也支持zip密码。
答案 2 :(得分:0)
GZipStream可用于创建.gz文件,但这与.zip文件不同。
要创建受密码保护的zip文件,我认为您需要转到第三方库。
以下是使用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");
}