C#创建zip,添加文件但无法打开

C# Create zip, add file but can't open

我不是 C# 开发人员,所以我的知识有限。但是,我正在尝试用 C# 编写一个程序,该程序导出特定日期的 Windows 安全日志并将其导出为 evtx 文件。

但是我要做的就是将这个 evtx 文件添加到一个 zip 文件中。下面的代码创建了 zip 文件(我可以看到)并将 evtx 文件添加到 zip(我认为是因为在 windows 中探索大小从 0kb 到更多的变化)。

#region "COMPRESS THE EVTX FILE INTO A ZIP FILE: "
// First create a new ZIP archive in the "Achives" folder.
string zipFileName = "Zip-" + getDateTimeStamp(1) + ".zip";
string zipFilePath = System.IO.Path.Combine(zipEvtxDirectoryPath, zipFileName);
ZipArchive zipFile = ZipFile.Open(zipFilePath, ZipArchiveMode.Create);

// Add the evtx file created in program directory to this zip archive.
Console.WriteLine(evtxFilePath);
Console.WriteLine(evtxFileName);
zipFile.CreateEntryFromFile(evtxFilePath, evtxFileName);

//evtxFilePath = "D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Security-Log-Archive-11-01-2015-04-18-58.evtx"
//evtxFileName = "Security-Log-Archive-11-01-2015-04-18-58.evtx"
//zipFilePath = "D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Zip-11-01-2015-04-18-58"

#endregion

但是当我尝试使用 windows 资源管理器打开 zip 文件时,它给了我一个错误。

Windows cannot open the folder.

The Compressed (zipped) Folder 'D:\TEST\VS\FilesAndFolders\TestDirectory\Archives\Zip-11-01-2015-04-18-58.zip' is invalid.

也许,我使用的代码不完整?或者我错过了什么?

你写了一段打开文件并写入的代码。 创建文件并写入文件后,您应该告诉PC关闭文件。

我不太确定这种情况下的命令,请尝试

zipFile.close;