예제 #1
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));
        }
예제 #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 (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));
        }