private void ProcessZipArchive(DataTable dt, IDataLoadEventListener listener, string zipFileName) { var skippedEntries = 0; var corruptedEntries = 0; try { using (var archive = ZipFile.Open(zipFileName, ZipArchiveMode.Read)) foreach (var f in archive.Entries) { //it's not a dicom file! if (!AmbiguousFilePath.IsDicomReference(f.FullName)) { skippedEntries++; continue; } try { var buffer = ByteStreamHelper.ReadFully(f.Open()); using (var memoryStream = new MemoryStream(buffer)) ProcessFile(memoryStream, dt, zipFileName + "!" + f.FullName, listener); } catch (Exception e) { corruptedEntries++; RecordError("Zip entry '" + f.FullName + "'", e); if (corruptedEntries <= 3) { continue; } listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Skipping the rest of '" + f.FullName + "'", e)); break; } } } catch (InvalidDataException e) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Error processing zip file '" + zipFileName + "'", e)); } if (skippedEntries > 0) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Skipped '" + skippedEntries + "' in zip archive '" + zipFileName + "' because they did not have .dcm extensions")); } UpdateProgressListeners(); }
private void ProcessDirectory(DataTable dt, DirectoryInfo directoryInfo, IDataLoadEventListener listener) { listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, "Started Directory '" + directoryInfo.FullName + "' on Thread " + Thread.CurrentThread.ManagedThreadId)); FileInfo[] dicomFiles; FileInfo[] zipFiles; try { dicomFiles = directoryInfo.EnumerateFiles().Where(f => AmbiguousFilePath.IsDicomReference(f.FullName)).ToArray(); zipFiles = directoryInfo.EnumerateFiles("*.zip").ToArray(); } catch (Exception e) { RecordError(directoryInfo.FullName, e); return; } //process all dcm files in current directory foreach (var dcmFile in dicomFiles) { try { using (var fs = dcmFile.OpenRead()) ProcessFile(fs, dt, dcmFile.FullName, listener); } catch (Exception e) { RecordError(dcmFile.FullName, e); } } foreach (var zipFile in zipFiles) { ProcessZipArchive(dt, listener, zipFile.FullName); } UpdateProgressListeners(); }
public void TestIsDicomReference(string input, bool expected) { Assert.AreEqual(expected, AmbiguousFilePath.IsDicomReference(input)); }