boolean useTempFile = false;
File writeFile = null;
try {
Progress.start(progressTicket);
Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
FileObject fileObject = FileUtil.toFileObject(file);
writeFile = file;
if (writeFile.exists()) {
useTempFile = true;
String tempFileName = writeFile.getName() + "_temp";
writeFile = new File(writeFile.getParent(), tempFileName);
}
//Stream
int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
FileOutputStream outputStream = new FileOutputStream(writeFile);
zipOut = new ZipOutputStream(outputStream);
zipOut.setLevel(zipLevel);
zipOut.putNextEntry(new ZipEntry("Project"));
gephiWriter = new GephiWriter();
//Create Writer and write project
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.FALSE);
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(zipOut);
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(bufferedOutputStream, "UTF-8");
gephiWriter.writeAll(project, writer);
writer.close();
//Close
zipOut.closeEntry();
zipOut.finish();
bufferedOutputStream.close();
//Clean and copy
if (useTempFile && !cancel) {
String name = fileObject.getName();
String ext = fileObject.getExt();
//Delete original file
fileObject.delete();
//Rename temp file
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
FileLock lock = tempFileObject.lock();
tempFileObject.rename(lock, name, ext);
lock.releaseLock();
} else if (cancel) {
//Delete temp file
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
tempFileObject.delete();
}
Progress.finish(progressTicket);
} catch (Exception ex) {
ex.printStackTrace();
if (zipOut != null) {