我正在编写一个网络应用程序,它可以上传/下载文件,这是我关于下载文件的代码(仅供测试):
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!");
}
当我打开zip文件时,它说zip是空的,然后我将其拖到bandizip中,它说文件已损坏,但所有文件都在列表中并且可以解压缩。 如何修复它?
试试这个,
static void Main(string[] args)
{
var zipName = $"archive-{DateTime.Now:yyyy_MM_dd-HH_mm_ss}.zip";
var folder = "D:\\xxdd";
using (var fileStream = File.Create(zipName))
using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
{
var files = Directory.GetFiles(folder);
foreach (var file in files)
{
zipArchive.CreateEntryFromFile(file, Path.GetFileName(file));
}
}
Console.WriteLine("Hello, World!");
}