Dotnetzip,保存后未触发事件处理程序
我有一个小问题,这可能是我的一个愚蠢的错误。
这是我在需要时创建 zip 文件的代码以及将文件添加到存档的方法。
添加文件可以正常工作,但由于某种原因,保存后不会触发事件。
我在 zipFile_SaveProgress 设置了断点,该事件没有被触发。
class CoverArchive
{
private ZipFile zipFile;
private String coverArchivePath;
public CoverArchive()
{
coverArchivePath = "Archive\\Covers";
if (!File.Exists("Archive\\Covers"))
{
CreateZipFile();
}
using (zipFile = ZipFile.Read(coverArchivePath))
{
//zipFile.AddProgress += zipFile_AddProgress;
//zipFile.ExtractProgress += zipFile_ExtractProgress;
//zipFile.ZipError += zipFile_ZipError;
zipFile.SaveProgress += zipFile_SaveProgress;
}
}
private void CreateZipFile()
{
zipFile = new ZipFile();
zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zipFile.UseUnicodeAsNecessary = true;
if (!Directory.Exists("Archive"))
Directory.CreateDirectory("Archive");
zipFile.Save(coverArchivePath);
}
public void AddCover(List<String> directories, String coverName, Stream fileStream)
{
try
{
using (zipFile = ZipFile.Read(coverArchivePath))
{
String filePath = createPath(directories, coverName);
ZipEntry entry = zipFile.AddEntry(filePath, fileStream);
zipFile.Save();
}
}
catch (Exception ex)
{
Console.WriteLine("Error adding File" + ex);
}
}
private void zipFile_SaveProgress(Object sender, SaveProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Saving_Completed)
Console.WriteLine("Save Done");
}
}
预先非常感谢:)
I have a small problem, which might me a stupid mistake on my side.
Here is my code to create a zipfile when needed and the method to add a file to the archive.
Adding a file works without problem but for some reason the Event is not fired after saving.
I set a breakpoint at zipFile_SaveProgress, the event is not fired.
class CoverArchive
{
private ZipFile zipFile;
private String coverArchivePath;
public CoverArchive()
{
coverArchivePath = "Archive\\Covers";
if (!File.Exists("Archive\\Covers"))
{
CreateZipFile();
}
using (zipFile = ZipFile.Read(coverArchivePath))
{
//zipFile.AddProgress += zipFile_AddProgress;
//zipFile.ExtractProgress += zipFile_ExtractProgress;
//zipFile.ZipError += zipFile_ZipError;
zipFile.SaveProgress += zipFile_SaveProgress;
}
}
private void CreateZipFile()
{
zipFile = new ZipFile();
zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zipFile.UseUnicodeAsNecessary = true;
if (!Directory.Exists("Archive"))
Directory.CreateDirectory("Archive");
zipFile.Save(coverArchivePath);
}
public void AddCover(List<String> directories, String coverName, Stream fileStream)
{
try
{
using (zipFile = ZipFile.Read(coverArchivePath))
{
String filePath = createPath(directories, coverName);
ZipEntry entry = zipFile.AddEntry(filePath, fileStream);
zipFile.Save();
}
}
catch (Exception ex)
{
Console.WriteLine("Error adding File" + ex);
}
}
private void zipFile_SaveProgress(Object sender, SaveProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Saving_Completed)
Console.WriteLine("Save Done");
}
}
Thanks a lot in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将在调用
Save()
之后添加事件处理。因此,当它引发
SaveProgress
事件时,没有任何处理程序。You're adding the event handling after calling
Save()
.Therefore, when it raised the
SaveProgress
event, there weren't any handlers.