I'm trying to figure it out how to create new zip file from a given folder path, and sending it back to the sender.
The need is that the file will be downloaded at the sender that requested it. I've seen alot of answers but none helped me with the exact answer.
My code:
Guid folderGuid = Guid.NewGuid(); string folderToZip = ConfigurationManager.AppSettings["folderToZip"] + folderGuid.ToString();
Directory.CreateDirectory(folderToZip);
string directoryPath = ConfigurationManager.AppSettings["DirectoryPath"]; string combinedPath = Path.Combine(directoryPath, id);
DirectoryInfo di = new DirectoryInfo(combinedPath); if (di.Exists) { //provide the path and name for the zip file to create string 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 = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "file.zip"
};
log.Debug("END ExportFiles()");
return ResponseMessage(result);