[英]C# - DotNetZip - Create a brand new folder or file in archive
I am working on a project which creates a compressed zip file and then adds a few new files and folders into it. 我正在开发一个项目,该项目创建一个压缩的zip文件,然后向其中添加一些新文件和文件夹。 I have searched the internet for some while now and can't find a solution.
我已经在互联网上搜索了一段时间,找不到解决方案。 I am using .NET Framework 4.7.1.
我正在使用.NET Framework 4.7.1。 in Visual Studio 2017 .
在Visual Studio 2017中 。
Here is what I have tried: 这是我尝试过的:
private void CreateFile(string saveDirectory)
{
using (ZipFile zip = new ZipFile())
{
zip.Password = "Some Password";
zip.Encryption = EncryptionAlgorithm.WinZipAes256;
zip.Save(saveDirectory);
}
}
private void ConfigureFile(string loadDirectory)
{
using (ZipFile zip = ZipFile.Read(loadDirectory))
{
zip.AddDirectory("Folder 1");
zip.AddDirectory("Folder 2");
zip.AddDirectory("Folder 3");
zip.AddFile("File 1");
zip.AddFile("File 2");
}
}
But it searches the directory of the project for that file, and throws a Not Found
error. 但是它将在项目的目录中搜索该文件,并引发“
Not Found
错误。 I also tried creating the folder or file in some place on the hard drive, and then placing it into the zip file, but is there a way to create a file or folder straight into the zip file without? 我也尝试过在硬盘驱动器上的某个位置创建文件夹或文件,然后将其放入zip文件中,但是有没有办法直接在zip文件中创建文件或文件夹呢?
Yes, you can create a ZipEntry
which doesn't rely on the corresponding path or file name existing, you can provide the content yourself. 是的,您可以创建一个不依赖于现有路径或文件名的
ZipEntry
,您可以自己提供内容。 For example: 例如:
public void ConfigureFile(string loadDirectory)
{
using (ZipFile zip = ZipFile.Read(loadDirectory))
{
zip.AddEntry(@"Folder1\Test.txt", new byte[] { 1, 2, 3 });
zip.Save();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.