예제 #1
0
        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();
        }
예제 #2
0
        public DicomFile GetDataset(ZipPool pool = null)
        {
            if (IsZipReference(FullPath))
            {
                var bits = FullPath.Split('!');

                var zip = pool != null?pool.OpenRead(bits[0]) : ZipFile.Open(bits[0], ZipArchiveMode.Read);

                try
                {
                    var entry = zip.GetEntry(bits[1]);

                    if (!IsDicomReference(bits[1]))
                    {
                        throw new AmbiguousFilePathResolutionException("Path provided '" + FullPath + "' was to a zip file but not to a dicom file entry");
                    }

                    var buffer = ByteStreamHelper.ReadFully(entry.Open());

                    //todo: when GH-627 goes live we can use FileReadOption  https://github.com/fo-dicom/fo-dicom/blob/GH-627/DICOM/DicomFile.cs
                    //using (var memoryStream = new MemoryStream(buffer))
                    var memoryStream = new MemoryStream(buffer);

                    return(DicomFile.Open(memoryStream));
                }
                finally
                {
                    if (pool == null)
                    {
                        zip.Dispose();
                    }
                }
            }

            if (!IsDicomReference(FullPath))
            {
                throw new AmbiguousFilePathResolutionException("Path provided '" + FullPath + "' was not to either an entry in a zip file or to a dicom file");
            }

            return(DicomFile.Open(FullPath));
        }
예제 #3
0
        public DicomFile GetDataset(ZipPool pool = null)
        {
            if (IsZipReference(FullPath))
            {
                var bits = FullPath.Split('!');

                var zip = pool != null?pool.OpenRead(bits[0]) : ZipFile.Open(bits[0], ZipArchiveMode.Read);

                try
                {
                    var entry = zip.GetEntry(bits[1]);

                    if (entry == null)
                    {
                        //Maybe user has formatted it dodgy
                        //e.g. \2015\3\18\2.25.177481563701402448825228719253578992342.dcm
                        string adjusted = bits[1].TrimStart('\\', '/');

                        //if that doesn't work
                        if ((entry = zip.GetEntry(adjusted)) == null)
                        {
                            //try normalizing the slashes
                            adjusted = adjusted.Replace('\\', '/');

                            //nope we just cannot get a legit path in this zip
                            if ((entry = zip.GetEntry(adjusted)) == null)
                            {
                                throw new AmbiguousFilePathResolutionException($"Could not find path '{bits[1]}' within zip archive '{bits[0]}'");
                            }
                        }

                        //we fixed it to something that actually exists so update our state that we don't make the same mistake again
                        FullPath = bits[0] + '!' + adjusted;
                    }

                    if (!IsDicomReference(bits[1]))
                    {
                        throw new AmbiguousFilePathResolutionException("Path provided '" + FullPath + "' was to a zip file but not to a dicom file entry");
                    }

                    var buffer = ByteStreamHelper.ReadFully(entry.Open());

                    //todo: when GH-627 goes live we can use FileReadOption  https://github.com/fo-dicom/fo-dicom/blob/GH-627/DICOM/DicomFile.cs
                    //using (var memoryStream = new MemoryStream(buffer))
                    var memoryStream = new MemoryStream(buffer);

                    return(DicomFile.Open(memoryStream));
                }
                finally
                {
                    if (pool == null)
                    {
                        zip.Dispose();
                    }
                }
            }

            if (!IsDicomReference(FullPath))
            {
                throw new AmbiguousFilePathResolutionException("Path provided '" + FullPath + "' was not to either an entry in a zip file or to a dicom file");
            }

            return(DicomFile.Open(FullPath));
        }