简体   繁体   English

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

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

I'm not a C# developer so my knowledge is limited. 我不是C#开发人员,所以我的知识有限。 However, I'm trying to write a program in C# which exports the Windows security log for certain date and export it as an evtx file. 但是,我试图用C#编写一个程序,该程序可以导出Windows安全日志在特定日期并将其导出为evtx文件。

But then what I have to do is add this evtx file to a zip file. 但是然后我要做的就是将此evtx文件添加到zip文件中。 The code below creates the zip file (I can see it) and adds evtx file to the zip (I think because in windows explore the size changes from 0kb to more). 下面的代码创建了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

But when I go try to open the zip file using windows explorer, it gives me an error. 但是当我尝试使用Windows资源管理器打开zip文件时,它给了我一个错误。

Windows cannot open the folder. Windows无法打开该文件夹。

The Compressed (zipped) Folder 'D:\\TEST\\VS\\FilesAndFolders\\TestDirectory\\Archives\\Zip-11-01-2015-04-18-58.zip' is invalid. 压缩(压缩)文件夹“ D:\\ TEST \\ VS \\ FilesAndFolders \\ TestDirectory \\ Archives \\ Zip-11-01-2015-04-18-58.zip”无效。

Perhaps, the code that I'm using is incomplete? 也许,我使用的代码不完整? or I missed something? 还是我错过了什么?

you have written a piece of code that opens the file and writes. 您编写了一段代码来打开文件并编写。 Once the process of creating the file and writing in it, you should tell the PC to close the file. 一旦完成了文件的创建和写入过程,您应该告诉PC关闭文件。

Im not exactly sure of the commands in this instance, try something like 我不确定在这种情况下的命令,尝试类似

zipFile.close;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM