Prefer using statement

This commit is contained in:
ivandroly 2015-06-30 15:10:38 +00:00
parent 9397c1022b
commit 3b29926f37
2 changed files with 23 additions and 26 deletions

View File

@ -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<ZipExtractor.ZipFileEntry> 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<ZipExtractor.ZipFileEntry> 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<ZipExtractor.ZipFileEntry> 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<ZipExtractor.ZipFileEntry> innerDir = innerZip.ReadCentralDir();
ExtractDic(dictionaryFolder, innerZip, innerDir, ref found);
}
}
}
}
}
zip.Close();
Cursor = Cursors.Default;
labelPleaseWait.Text = string.Empty;
buttonOK.Enabled = true;

View File

@ -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))
{