.net core 如何将文件添加到.NET内核中zip的文件夹中?

.net core 如何将文件添加到.NET内核中zip的文件夹中?,.net-core,zipfile,ziparchive,.net Core,Zipfile,Ziparchive,我需要从一个文件夹中读取一个文件,并将其放置在.NET内核中zip的文件夹中 我搜索了一下,发现了这个,但这个对我不起作用 我的zip文件代码是: using (var fs = new FileStream("Test.zip", FileMode.Create)) using (var zip = new ZipArchive(fs, ZipArchiveMode.Create)) { zip.CreateEntry("Folder1 /");

我需要从一个文件夹中读取一个文件,并将其放置在.NET内核中zip的文件夹中

我搜索了一下,发现了这个,但这个对我不起作用

我的zip文件代码是:

using (var fs = new FileStream("Test.zip", FileMode.Create))
using (var zip = new ZipArchive(fs, ZipArchiveMode.Create))
{
    zip.CreateEntry("Folder1 /"); 
}// working till here, Creates a zip and then creates Folder 1 inside Test.zip`

但我需要从“C:\Files”中读取一个文件,并将其放在“Folder1”中。文件被添加到zip中,但不在“Folder1”中。

此代码似乎起到了作用:

using System.IO;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string sourceFileName = "MyFile.txt";
        string sourceFolder = @"C:\Files";
        string zipFilePath = Path.Combine(@"C:\Files", "Test.zip");

        using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
        {
            archive.CreateEntryFromFile(Path.Combine(sourceFolder, sourceFileName), $"Folder1\\{sourceFileName}");
        }
    }
}

您面临什么错误?我没有遇到任何错误。我尝试使用
zip.CreateEntryFromFile(SourceFilePath,“FileName”,CompressionLevel.Optimal)
但这是在test.zip中创建的条目。但是我需要将此条目添加到“test.zip/Folder1”中。您能否在Folder1
var zipPath=configuration.GetValue(Constant.AppSettings.FileDownloadLocation)中添加条目的位置共享您的代码;var fileToReadPath=configuration.GetValue(Constant.AppSettings.TestFileLocation);使用(ZipArchive archive=ZipFile.Open(zipPath,ZipArchiveMode.Update)){archive.CreateEntryFromFile(fileToReadPath,“NewEntry”,CompressionLevel.faster);}