Skip to content

Commit e29d5af

Browse files
author
兰小方
committed
修改异常捕获范围。
1 parent bcdd176 commit e29d5af

1 file changed

Lines changed: 38 additions & 36 deletions

File tree

src/dotnetCampus.OfficeDocumentZipper/MainWindow.xaml.cs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,10 @@ private void UnzipFile()
9797
}
9898

9999
Directory.CreateDirectory(directory);
100-
ZipFile.ExtractToDirectory(file, directory, true);
101-
102-
// 涉及IO操作,最好捕获一下异常。
103-
try
104-
{
105-
// 这个方法对嵌入excel表格的PPT文件进行处理。
106-
UnZipOleObjectFile();
107-
}
108-
catch (Exception exception)
109-
{
110-
Warn(exception.ToString());
111-
}
100+
System.IO.Compression.ZipFile.ExtractToDirectory(file, directory, true);
112101

102+
// 这个方法对嵌入excel表格的PPT文件进行处理。
103+
UnZipOleObjectFile();
113104

114105
Warn("");
115106
}
@@ -118,7 +109,7 @@ private void UnzipFile()
118109
/// 这个方法对嵌入excel表格的PPT文件进行处理。
119110
/// 如果PPT文件中嵌入了Excel表格,则调用该方法对OleObj映射的xlsx文件进行解压缩。
120111
/// </summary>
121-
void UnZipOleObjectFile()
112+
private void UnZipOleObjectFile()
122113
{
123114
// 获取OleObj的映射的xlsx文件。
124115
var oleObjDirectory = Path.Combine(OfficeFolder.Text, "ppt\\embeddings\\");
@@ -131,7 +122,7 @@ void UnZipOleObjectFile()
131122
{
132123
var directoryName = Path.Combine(fileInfo.DirectoryName, Path.GetFileNameWithoutExtension(fileInfo.Name));
133124
Directory.CreateDirectory(directoryName);
134-
ZipFile.ExtractToDirectory(fileInfo.FullName, directoryName, true);
125+
System.IO.Compression.ZipFile.ExtractToDirectory(fileInfo.FullName, directoryName, true);
135126
}
136127
}
137128
}
@@ -225,7 +216,10 @@ private void Explorer_OnClick(object sender, RoutedEventArgs e)
225216
Warn("");
226217
}
227218

228-
private void Zip_OnClick(object sender, RoutedEventArgs e)
219+
/// <summary>
220+
/// 压缩PPT文件。
221+
/// </summary>
222+
private void ZipFile()
229223
{
230224
if (!CheckFolderExists())
231225
{
@@ -247,35 +241,31 @@ private void Zip_OnClick(object sender, RoutedEventArgs e)
247241

248242
var directory = OfficeFolder.Text;
249243

250-
// 涉及IO操作,最好捕获一下异常
251-
try
252-
{
253-
// 这个步骤是查找文件夹里是否存在oleObj元素映射的xlsx文件,并进行压缩处理。
254-
ZipOleObjectFile();
255-
}
256-
catch (Exception exception)
257-
{
258-
Warn(exception.ToString());
259-
}
244+
// 这个步骤是查找文件夹里是否存在oleObj元素映射的xlsx文件,并进行压缩处理
245+
ZipOleObjectFile();
246+
247+
System.IO.Compression.ZipFile.CreateFromDirectory(directory, file, CompressionLevel.NoCompression, false);
248+
249+
// 还原ZipOleObjectFile方法删除的Excel文件解压出来的问价夹。
250+
UnZipOleObjectFile();
251+
252+
OfficeFile.Text = file;
253+
OfficeFolder.Text = directory;
260254

261-
ZipFile.CreateFromDirectory(directory, file, CompressionLevel.NoCompression, false);
255+
Warn("");
256+
}
262257

258+
private void Zip_OnClick(object sender, RoutedEventArgs e)
259+
{
263260
// 涉及IO操作,最好捕获一下异常。
264261
try
265262
{
266-
// 还原ZipOleObjectFile方法删除的Excel文件解压出来的问价夹。
267-
UnZipOleObjectFile();
263+
ZipFile();
268264
}
269265
catch (Exception exception)
270266
{
271267
Warn(exception.ToString());
272268
}
273-
274-
275-
OfficeFile.Text = file;
276-
OfficeFolder.Text = directory;
277-
278-
Warn("");
279269
}
280270

281271
/// <summary>
@@ -302,7 +292,7 @@ private void ZipOleObjectFile()
302292
{
303293
var fileName = Path.ChangeExtension(directoryInfo.FullName, ".xlsx");
304294
File.Delete(fileName);
305-
ZipFile.CreateFromDirectory(directoryInfo.FullName, fileName, CompressionLevel.NoCompression, false);
295+
System.IO.Compression.ZipFile.CreateFromDirectory(directoryInfo.FullName, fileName, CompressionLevel.NoCompression, false);
306296

307297
// 必须要删除原目录,否则PPT打开错误。
308298
directoryInfo.Delete(true);
@@ -404,7 +394,19 @@ private string GetFile()
404394

405395
private void ZipAndOpen_OnClick(object sender, RoutedEventArgs e)
406396
{
407-
Zip_OnClick(sender, e);
397+
// 涉及IO操作,最好捕获一下异常。
398+
try
399+
{
400+
ZipFile();
401+
}
402+
catch (Exception exception)
403+
{
404+
Warn(exception.ToString());
405+
406+
// 异常了就没必要打开PPT文件了。
407+
return;
408+
}
409+
408410
OpenOfficeFile_OnClick(sender, e);
409411
}
410412

0 commit comments

Comments
 (0)