Small fixes:
[fanfix.git] / src / be / nikiroo / fanfix / output / Epub.java
index 64797d395f1442d0d49220f4072bb7df832e9634..b56cc339285e4f64c83401b5ba79cfe50bb068fc 100644 (file)
@@ -36,7 +36,7 @@ class Epub extends BasicOutput {
                String targetNameOrig = targetName;
                targetName += getDefaultExtension(false);
 
-               tmpDir = File.createTempFile("fanfic-reader-epub_", ".wip");
+               tmpDir = Instance.getTempFiles().createTempDir("fanfic-reader-epub");
                tmpDir.delete();
 
                if (!tmpDir.mkdir()) {
@@ -46,51 +46,54 @@ class Epub extends BasicOutput {
 
                super.process(story, targetDir, targetNameOrig);
 
-               // "Originals"
-               File data = new File(tmpDir, "DATA");
-               data.mkdir();
-               BasicOutput.getOutput(OutputType.TEXT, isWriteInfo(), isWriteCover())
-                               .process(story, data, targetNameOrig);
-               InfoCover.writeInfo(data, targetNameOrig, story.getMeta());
-               IOUtils.writeSmallFile(data, "version", "3.0");
-
-               // zip/epub
-               File epub = new File(targetDir, targetName);
-               IOUtils.zip(tmpDir, epub, true);
-
-               OutputStream out = new FileOutputStream(epub);
+               File epub = null;
                try {
-                       ZipOutputStream zip = new ZipOutputStream(out);
+                       // "Originals"
+                       File data = new File(tmpDir, "DATA");
+                       data.mkdir();
+                       BasicOutput.getOutput(OutputType.TEXT, isWriteInfo(),
+                                       isWriteCover()).process(story, data, targetNameOrig);
+                       InfoCover.writeInfo(data, targetNameOrig, story.getMeta());
+                       IOUtils.writeSmallFile(data, "version", "3.0");
+
+                       // zip/epub
+                       epub = new File(targetDir, targetName);
+                       IOUtils.zip(tmpDir, epub, true);
+
+                       OutputStream out = new FileOutputStream(epub);
                        try {
-                               // "mimetype" MUST be the first element and not compressed
-                               zip.setLevel(ZipOutputStream.STORED);
-                               File mimetype = new File(tmpDir, "mimetype");
-                               IOUtils.writeSmallFile(tmpDir, "mimetype",
-                                               "application/epub+zip");
-                               ZipEntry entry = new ZipEntry("mimetype");
-                               entry.setExtra(new byte[] {});
-                               zip.putNextEntry(entry);
-                               FileInputStream in = new FileInputStream(mimetype);
+                               ZipOutputStream zip = new ZipOutputStream(out);
                                try {
-                                       IOUtils.write(in, zip);
+                                       // "mimetype" MUST be the first element and not compressed
+                                       zip.setLevel(ZipOutputStream.STORED);
+                                       File mimetype = new File(tmpDir, "mimetype");
+                                       IOUtils.writeSmallFile(tmpDir, "mimetype",
+                                                       "application/epub+zip");
+                                       ZipEntry entry = new ZipEntry("mimetype");
+                                       entry.setExtra(new byte[] {});
+                                       zip.putNextEntry(entry);
+                                       FileInputStream in = new FileInputStream(mimetype);
+                                       try {
+                                               IOUtils.write(in, zip);
+                                       } finally {
+                                               in.close();
+                                       }
+                                       IOUtils.deltree(mimetype);
+                                       zip.setLevel(ZipOutputStream.DEFLATED);
+                                       //
+
+                                       IOUtils.zip(zip, "", tmpDir, true);
                                } finally {
-                                       in.close();
+                                       zip.close();
                                }
-                               IOUtils.deltree(mimetype);
-                               zip.setLevel(ZipOutputStream.DEFLATED);
-                               //
-
-                               IOUtils.zip(zip, "", tmpDir, true);
                        } finally {
-                               zip.close();
+                               out.close();
                        }
                } finally {
-                       out.close();
+                       IOUtils.deltree(tmpDir);
+                       tmpDir = null;
                }
 
-               IOUtils.deltree(tmpDir);
-               tmpDir = null;
-
                return epub;
        }