private File generateFileName(File outDir, String fileName, String type, boolean isZip) { DocumentFormat fmt = type != null ? DocumentFormat.byMimeType(type) : null; // DocumentFormat fmtext = fileName!=null ? DocumentFormat.byExtension(fileName) : null; if (fileName == null) fileName = "noname"; String ext = null; if (fileName.lastIndexOf(".") > 0) { ext = fileName.substring(fileName.lastIndexOf(".") + 1); fileName = fileName.substring(0, fileName.lastIndexOf(".")); } fileName = transcribeFileName(fileName); if (fmt != null) { if (fmt == DocumentFormat.FB2 && isZip) ext = ".fb2.zip"; else ext = fmt.getExtensions()[0].substring(1); } for (int i = 0; i < 1000; i++) { String fn = fileName + (i == 0 ? "" : "(" + i + ")") + "." + ext; File f = new File(outDir, fn); if (!f.exists() && !f.isDirectory()) return f; } return null; }