I’m writing a web app ,it can upload/download files, here is my code about download files(just for test):
static void Main(string[] args)
{
var zipName = $"archive-{DateTime.Now:yyyy_MM_dd-HH_mm_ss}.zip";
var folder = "D:\xxdd";
using var memoryStream = new MemoryStream();
using var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
var files = Directory.GetFiles(folder);
foreach (var file in files)
{
zipArchive.CreateEntryFromFile(file, Path.GetFileName(file));
}
memoryStream.Position = 0;
File.WriteAllBytes(zipName,memoryStream.ToArray());
Console.WriteLine("Hello, World!");
}
when I opened the zip file, it said the zip is empty, then I dragged it into the bandizip,it said the file was broken,but all the files were in the list and it could be unzip.
how to fix it ?
2
Answers
Try this,
disposed before writing.
structure.
data.
You don’t call
Dispose
on theZipArchive
, so the data isn’t completely written. As the documentation says:"This method finishes writing the archive and releases all resources used by the ZipArchive object."