public void Write(Stream stream) { FlushSheets(); //Save the template var tmplFile = TempFile.CreateTempFile("poi-sxssf-template", ".xlsx"); try { var os = new FileStream(tmplFile.FullName, FileMode.Open, FileAccess.ReadWrite); try { XssfWorkbook.Write(os); } finally { os.Close(); } //Substitute the template entries with the generated sheet data files ZipEntrySource source = new ZipFileZipEntrySource(new ZipFile(tmplFile.FullName)); InjectData(source, stream); } finally { tmplFile.Delete(); if (File.Exists(tmplFile.FullName)) { throw new IOException("Could not delete temporary file after processing: " + tmplFile); } } }
/** * Constructor. Opens a Zip based Open XML document. * * @param file * The file to open or create. * @param access * The package access mode. */ public ZipPackage(FileInfo file, PackageAccess access) : base(access) { ZipEntrySource ze; try { ZipFile zipFile = ZipHelper.OpenZipFile(file); ze = new ZipFileZipEntrySource(zipFile); } catch (IOException e) { // probably not happening with write access - not sure how to handle the default read-write access ... if (access == PackageAccess.WRITE) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e); } logger.Log(POILogger.ERROR, "Error in zip file " + file + " - falling back to stream processing (i.e. ignoring zip central directory)"); // some zips can't be opened via ZipFile in JDK6, as the central directory // contains either non-latin entries or the compression type can't be handled // the workaround is to iterate over the stream and not the directory FileStream fis = null; //ThresholdInputStream zis = null; ZipInputStream zis = null; try { fis = file.Create(); // TODO: ZipSecureFile // zis = ZipHelper.OpenZipStream(fis); zis = ZipHelper.OpenZipStream(fis); ze = new ZipInputStreamZipEntrySource(zis); } catch (IOException e2) { if (zis != null) { try { zis.Close(); } catch (IOException) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'" + " and couldn't close the file input stream", e); } } else if (fis != null) { try { fis.Close(); } catch (IOException) { throw new InvalidOperationException("Can't open the specified file: '" + file + "'" + " and couldn't close the file input stream", e); } } throw new InvalidOperationException("Can't open the specified file: '" + file + "'", e2); } } this.zipArchive = ze; }