使用密码在 .net 中创建 zip 文件
create zip file in .net with password
我正在做一个项目,我需要用 c# 中的文件内容保护密码创建 zip。
在我使用 System.IO.Compression.GZipStream 创建 gzip 内容之前。
.net 是否具有创建 zip 或 rar 密码保护文件的功能?
很遗憾,框架中没有这样的功能。有一种制作 ZIP 文件的方法,但没有密码。如果您想在 C# 中创建受密码保护的 ZIP 文件,我推荐 SevenZipSharp。它基本上是 7-Zip 的托管包装器。
SevenZipBase.SetLibraryPath(Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Environment.CurrentDirectory,
"7za.dll"));
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.Compressing += Compressor_Compressing;
compressor.FileCompressionStarted += Compressor_FileCompressionStarted;
compressor.CompressionFinished += Compressor_CompressionFinished;
string password = @"whatever";
string destinationFile = @"C:\Temp\whatever.zip";
string[] sourceFiles = Directory.GetFiles(@"C:\Temp\YourFiles\");
if (String.IsNullOrWhiteSpace(password))
{
compressor.CompressFiles(destinationFile, sourceFiles);
}
else
{
//optional
compressor.EncryptHeaders = true;
compressor.CompressFilesEncrypted(destinationFile, password, sourceFiles);
}
看看 DotNetZip(@AFract 在评论中为 GitHub 提供了一个新的 link)
它有非常好的文档,它还允许您在运行时将 dll 作为嵌入文件加载。
我想添加更多选项。
对于 .NET,可以使用 SharpZipLib, for Xamarin use SharpZipLib.Portable.
.NET 示例:
using ICSharpCode.SharpZipLib.Zip;
// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) {
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
zipStream.Password = "Your password";
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
return outputMemStream;
// Alternative outputs:
// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
byte[] byteArrayOut = outputMemStream.ToArray();
// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
byte[] byteArrayOut = outputMemStream.GetBuffer();
long len = outputMemStream.Length;
}
可以找到更多示例 here。
如果你可以在没有密码功能的情况下生活,可以提到 ZipStorer or the built in .NET function in System.IO.Compression
。
DotNetZip 以干净的方式工作得很好。
DotNetZip is a FAST, FREE class library and toolset for manipulating zip files.
代码
static void Main(string[] args)
{
using (ZipFile zip = new ZipFile())
{
zip.Password = "mypassword";
zip.AddDirectory(@"C:\Test\Report_CCLF5\");
zip.Save(@"C:\Test\Report_CCLF5_PartB.zip");
}
}
我正在做一个项目,我需要用 c# 中的文件内容保护密码创建 zip。
在我使用 System.IO.Compression.GZipStream 创建 gzip 内容之前。 .net 是否具有创建 zip 或 rar 密码保护文件的功能?
很遗憾,框架中没有这样的功能。有一种制作 ZIP 文件的方法,但没有密码。如果您想在 C# 中创建受密码保护的 ZIP 文件,我推荐 SevenZipSharp。它基本上是 7-Zip 的托管包装器。
SevenZipBase.SetLibraryPath(Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Environment.CurrentDirectory,
"7za.dll"));
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.Compressing += Compressor_Compressing;
compressor.FileCompressionStarted += Compressor_FileCompressionStarted;
compressor.CompressionFinished += Compressor_CompressionFinished;
string password = @"whatever";
string destinationFile = @"C:\Temp\whatever.zip";
string[] sourceFiles = Directory.GetFiles(@"C:\Temp\YourFiles\");
if (String.IsNullOrWhiteSpace(password))
{
compressor.CompressFiles(destinationFile, sourceFiles);
}
else
{
//optional
compressor.EncryptHeaders = true;
compressor.CompressFilesEncrypted(destinationFile, password, sourceFiles);
}
看看 DotNetZip(@AFract 在评论中为 GitHub 提供了一个新的 link)
它有非常好的文档,它还允许您在运行时将 dll 作为嵌入文件加载。
我想添加更多选项。
对于 .NET,可以使用 SharpZipLib, for Xamarin use SharpZipLib.Portable.
.NET 示例:
using ICSharpCode.SharpZipLib.Zip;
// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) {
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
zipStream.Password = "Your password";
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
return outputMemStream;
// Alternative outputs:
// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
byte[] byteArrayOut = outputMemStream.ToArray();
// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
byte[] byteArrayOut = outputMemStream.GetBuffer();
long len = outputMemStream.Length;
}
可以找到更多示例 here。
如果你可以在没有密码功能的情况下生活,可以提到 ZipStorer or the built in .NET function in System.IO.Compression
。
DotNetZip 以干净的方式工作得很好。
DotNetZip is a FAST, FREE class library and toolset for manipulating zip files.
代码
static void Main(string[] args)
{
using (ZipFile zip = new ZipFile())
{
zip.Password = "mypassword";
zip.AddDirectory(@"C:\Test\Report_CCLF5\");
zip.Save(@"C:\Test\Report_CCLF5_PartB.zip");
}
}