ImagePlus openDicomStack(ZipInputStream zis, ZipEntry entry) throws IOException { ImagePlus imp = null; int count = 0; ImageStack stack = null; while (true) { if (count > 0) entry = zis.getNextEntry(); if (entry == null) break; String name = entry.getName(); ImagePlus imp2 = null; if (name.endsWith(".dcm")) { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int len, byteCount = 0, progress = 0; while (true) { len = zis.read(buf); if (len < 0) break; out.write(buf, 0, len); byteCount += len; // IJ.showProgress((double)(byteCount%fileSize)/fileSize); } byte[] bytes = out.toByteArray(); out.close(); InputStream is = new ByteArrayInputStream(bytes); DICOM dcm = new DICOM(is); dcm.run(name); imp2 = dcm; is.close(); } zis.closeEntry(); if (imp2 == null) continue; count++; String label = imp2.getTitle(); String info = (String) imp2.getProperty("Info"); if (info != null) label += "\n" + info; if (count == 1) { imp = imp2; imp.getStack().setSliceLabel(label, 1); } else { stack = imp.getStack(); stack.addSlice(label, imp2.getProcessor()); imp.setStack(stack); } } zis.close(); IJ.showProgress(1.0); if (count == 0) throw new IOException("This ZIP archive does not appear to contain any .dcm files"); return imp; }
/** * Opens a single TIFF or DICOM contained in a ZIP archive, or a ZIPed collection of ".roi" files * created by the ROI manager. */ public ImagePlus openZip(String path) { ImagePlus imp = null; try { ZipInputStream zis = new ZipInputStream(new FileInputStream(path)); ZipEntry entry = zis.getNextEntry(); if (entry == null) { zis.close(); return null; } String name = entry.getName(); if (name.endsWith(".roi")) { zis.close(); if (!silentMode) if (IJ.isMacro() && Interpreter.isBatchMode() && RoiManager.getInstance() == null) IJ.log( "Use roiManager(\"Open\", path) instead of open(path)\nto open ROI sets in batch mode macros."); else IJ.runMacro("roiManager(\"Open\", getArgument());", path); return null; } if (name.endsWith(".tif")) { imp = openTiff(zis, name); } else if (name.endsWith(".dcm")) { DICOM dcm = new DICOM(zis); dcm.run(name); imp = dcm; } else { zis.close(); IJ.error( "Opener", "This ZIP archive does not appear to contain a \nTIFF (\".tif\") or DICOM (\".dcm\") file, or ROIs (\".roi\")."); return null; } zis.close(); } catch (Exception e) { IJ.error("Opener", "" + e); return null; } File f = new File(path); FileInfo fi = imp.getOriginalFileInfo(); if (fi != null) { fi.fileFormat = FileInfo.ZIP_ARCHIVE; fi.fileName = f.getName(); fi.directory = f.getParent() + File.separator; } return imp; }