From 3b29926f37cfb3c32742bdf6e7dbe0044b5cd59e Mon Sep 17 00:00:00 2001 From: ivandroly Date: Tue, 30 Jun 2015 15:10:38 +0000 Subject: [PATCH] Prefer using statement --- src/Forms/GetDictionaries.cs | 41 +++++++++++++-------------- src/Forms/GetTesseractDictionaries.cs | 8 ++---- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Forms/GetDictionaries.cs b/src/Forms/GetDictionaries.cs index c4451b4b7..168bdb969 100644 --- a/src/Forms/GetDictionaries.cs +++ b/src/Forms/GetDictionaries.cs @@ -43,7 +43,7 @@ namespace Nikse.SubtitleEdit.Forms { comboBoxDictionaries.Items.Clear(); XmlDocument doc = new XmlDocument(); - var rdr = new StreamReader(strm); + using (var rdr = new StreamReader(strm)) using (var zip = new GZipStream(rdr.BaseStream, CompressionMode.Decompress)) { byte[] data = new byte[275000]; @@ -53,7 +53,6 @@ namespace Nikse.SubtitleEdit.Forms string s = System.Text.Encoding.UTF8.GetString(data2).Trim(); doc.LoadXml(s); } - rdr.Close(); foreach (XmlNode node in doc.DocumentElement.SelectNodes("Dictionary")) { @@ -167,32 +166,32 @@ namespace Nikse.SubtitleEdit.Forms int index = comboBoxDictionaries.SelectedIndex; - var ms = new MemoryStream(e.Result); - - ZipExtractor zip = ZipExtractor.Open(ms); - List dir = zip.ReadCentralDir(); - - // Extract dic/aff files in dictionary folder - bool found = false; - ExtractDic(dictionaryFolder, zip, dir, ref found); - - if (!found) // check zip inside zip + using (var ms = new MemoryStream(e.Result)) + using (ZipExtractor zip = ZipExtractor.Open(ms)) { - foreach (ZipExtractor.ZipFileEntry entry in dir) + List dir = zip.ReadCentralDir(); + // Extract dic/aff files in dictionary folder + bool found = false; + ExtractDic(dictionaryFolder, zip, dir, ref found); + + if (!found) // check zip inside zip { - if (entry.FilenameInZip.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) + foreach (ZipExtractor.ZipFileEntry entry in dir) { - var innerMs = new MemoryStream(); - zip.ExtractFile(entry, innerMs); - ZipExtractor innerZip = ZipExtractor.Open(innerMs); - List innerDir = innerZip.ReadCentralDir(); - ExtractDic(dictionaryFolder, innerZip, innerDir, ref found); - innerZip.Close(); + if (entry.FilenameInZip.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) + { + using (var innerMs = new MemoryStream()) + { + zip.ExtractFile(entry, innerMs); + ZipExtractor innerZip = ZipExtractor.Open(innerMs); + List innerDir = innerZip.ReadCentralDir(); + ExtractDic(dictionaryFolder, innerZip, innerDir, ref found); + } + } } } } - zip.Close(); Cursor = Cursors.Default; labelPleaseWait.Text = string.Empty; buttonOK.Enabled = true; diff --git a/src/Forms/GetTesseractDictionaries.cs b/src/Forms/GetTesseractDictionaries.cs index 883cce1ca..73e89db56 100644 --- a/src/Forms/GetTesseractDictionaries.cs +++ b/src/Forms/GetTesseractDictionaries.cs @@ -41,14 +41,13 @@ namespace Nikse.SubtitleEdit.Forms { comboBoxDictionaries.Items.Clear(); XmlDocument doc = new XmlDocument(); - var rdr = new StreamReader(strm); + using (var rdr = new StreamReader(strm)) using (var zip = new GZipStream(rdr.BaseStream, CompressionMode.Decompress)) { byte[] data = new byte[175000]; zip.Read(data, 0, 175000); doc.LoadXml(System.Text.Encoding.UTF8.GetString(data)); } - rdr.Close(); foreach (XmlNode node in doc.DocumentElement.SelectNodes("Dictionary")) { @@ -124,9 +123,9 @@ namespace Nikse.SubtitleEdit.Forms int index = comboBoxDictionaries.SelectedIndex; - var ms = new MemoryStream(e.Result); var tempFileName = Path.GetTempFileName() + ".tar"; - var fs = new FileStream(tempFileName, FileMode.Create); + using (var ms = new MemoryStream(e.Result)) + using (var fs = new FileStream(tempFileName, FileMode.Create)) using (var zip = new GZipStream(ms, CompressionMode.Decompress)) { byte[] buffer = new byte[1024]; @@ -136,7 +135,6 @@ namespace Nikse.SubtitleEdit.Forms fs.Write(buffer, 0, nRead); } } - fs.Close(); using (var tr = new TarReader(tempFileName)) {