如何使用 C# 锁定 Zip 存档

Aspose.Zip for .NET 是一款功能强大的应用程序,它使开发人员能够轻松锁定 zip 存档并为其文件添加额外的安全层。通过使用 Aspose.Zip 库提供的 Lock 方法,开发人员可以毫不费力地为他们的 zip 存档设置密码。此功能确保只有授权的个人才能访问档案的内容,从而保护敏感数据免遭未经授权的访问。支持各种压缩格式,如 ZIP、GZIP、BZIP2、TAR、CPIO、LZIP、7Z、LZMA、XZ 和 Z,Aspose.Zip 为处理不同的存档类型提供了全面的解决方案。直观且用户友好的 API 允许开发人员将密码保护无缝集成到他们的 .NET 应用程序中,无论它们是基于 Web 还是 Zip 在线应用程序 在 Windows、Linux 或 macOS 上运行。通过使用 Aspose.Zip,开发人员可以增强其 zip 存档的安全性并确保其文件的机密性。

使用文件压缩和存档 .NET API 管理 NuGet 包

Aspose.ZIP for .NET 是一个功能强大的类库,专为.NET 开发人员设计,用于处理各种存档处理任务。它支持 ZIP、GZIP、BZIP2、TAR、CPIO、LZIP、7Z、LZMA、XZ 和 Z 等多种格式,提供了广泛的功能。开发人员可以利用此库来加密和解密文件、创建自解压存档以及提取 RAR、CAB 和 WIM 格式。

API 用户友好,简单可靠.它使用托管 C# 实现,可以与任何 .NET 语言无缝集成,包括 C#、VB.NET 和 F#。无论您是在处理 ASP.NET Web 应用程序还是表单应用程序,无论是 Windows、Linux 还是 MacOS,它都可以轻松集成。通过使用 Aspose.ZIP for .NET,开发人员获得了一个通用的解决方案来处理其 .NET 应用程序中的存档处理任务。


在 C# 中保护 Zip 存档的步骤

Protecting archive using password with

Aspose.ZIP for .NET

APIs can be done with just few lines of code.

  • Create FileStream object with the output ZIP archive file path as parameter with mod FileMode.Create.
  • Open and read the source file into a FileStream object with mod FileMode.Open and FileAccess.Read.
  • Create Archive class object with ArchiveEntrySettings object as parameter.
  • Use TraditionalEncryptionSettings as parameters to ArchiveEntrySettings.
  • Use Archive.Save(FileStream) method for creating the archive.

系统要求

Before running the conversion example code, make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, Mono and COM Interop.
  • Development environment like Microsoft Visual Studio.
  • Aspose.Tasks for .NET DLL referenced in your project.

锁定 ZIP 存档 - C# 代码示例

using (FileStream zipFile = File.Open(dataDir + "CompressWithTraditionalEncryption_out.zip", FileMode.Create))
{
    using (FileStream source1 = File.Open(dataDir + "alice29.txt", FileMode.Open, FileAccess.Read))
    {
        var archive = new Archive(new ArchiveEntrySettings(null, new TraditionalEncryptionSettings("p@s$")));
        archive.CreateEntry("alice29.txt", source1);
        archive.Save(zipFile);
    }
}



常见问题解答部分

People have been asking

1. How can I unlock a password-protected zip archive?

To unlock a password-protected zip archive, you need to provide the correct password. You can use the appropriate method or property of the zip library you are using to set the password, and then extract the files from the archive using that password.

2. Can I remove or change the password of a locked zip archive?

Yes, it is possible to remove or change the password of a locked zip archive. You can use the library’s methods or properties to modify the password. Typically, you would need to provide the current password to remove it or provide the current password along with the new password to change it.

3. Is it possible to recover a forgotten password for a locked zip archive?

Recovering a forgotten password for a locked zip archive is extremely difficult, if not impossible, without specialized tools. Zip archives are designed to provide strong encryption, and forgetting the password means losing access to the contents of the archive. It is recommended to keep your passwords safe and secure to avoid such situations.

受保护的 ZIP 归档操作

Aspose.Zip 提供了多种加密选项以进一步增强 ZIP 档案的安全性。开发人员可以利用 AES 加密算法(例如 AES128、AES192 和 AES256)来加密存档中的文件。这允许使用不同的密钥长度进行更强大的加密,以满足特定的安全要求。此外,Aspose.Zip 支持对单个存档中的多个文件进行加密,使开发人员能够根据他们的特定需求使用混合加密技术。凭借这些高级加密功能,Aspose.Zip 使开发人员能够实施强大的安全措施来保护 ZIP 存档中的敏感数据。

其他支持的 Aspose.Zip for .NET API 功能

使用 Aspose.Zip C# 库进行转换、压缩、锁定、解锁、解压缩档案等!

Compression (Archive compressions)
Conversions (Rar to 7Zip)
Extract (Extracting Archives)
Lock (Archive lock)
Lock (Archive unlock)
ZIP Viewer (ZIP File Viewer)
Creating Archive (Add files or folders to ZIP)