我试图弄清楚如何从给定的文件夹路径创建新的 zip 文件,并将其发送回发件人。
需要的是文件将在请求它的发件人处下载。我看过很多答案,但没有一个能帮我找到确切的答案。
我的代码:
Guid folderGuid = Guid.NewGuid(); string folderToZip = ConfigurationManager.AppSettings["folderToZip"] + folderGuid.ToString();
Directory.CreateDirectory(folderToZip);
string directoryPath = ConfigurationManager.AppSettings["DirectoryPath"]; 字符串组合路径 = Path.Combine(directoryPath, id);
DirectoryInfo di = new DirectoryInfo(combinedPath); if (di.Exists) { // 提供 zip 文件的路径和名称以创建字符串 zipFile = folderToZip + "\" + folderGuid + ".zip";
//call the ZipFile.CreateFromDirectory() method
ZipFile.CreateFromDirectory(combinedPath, zipFile, CompressionLevel.Fastest, true);
var result = new HttpResponseMessage(HttpStatusCode.OK);
using (ZipArchive zip = ZipFile.Open(zipFile, ZipArchiveMode.Read))
{
zip.CreateEntryFromFile(folderFiles, "file.zip");
}
var stream = new FileStream(zipFile, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = …Run Code Online (Sandbox Code Playgroud)