Index: src/main/java/org/gvsig/app/extension/ProjectExtension.java =================================================================== --- src/main/java/org/gvsig/app/extension/ProjectExtension.java (revisión: 43809) +++ src/main/java/org/gvsig/app/extension/ProjectExtension.java (copia de trabajo) @@ -37,6 +37,8 @@ import java.util.Set; import java.util.prefs.Preferences; import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import javax.imageio.ImageIO; @@ -446,39 +448,40 @@ return writeProject(file, p, true); } - /** - * Escribe el proyecto en XML. Pero permite decidir si se pide confirmación - * para sobreescribir - * - * @param file - * Fichero. - * @param p - * Proyecto. - * @param askConfirmation - * boolean - */ - public boolean writeProject(File file, Project p, boolean askConfirmation) { - if (askConfirmation && file.exists()) { - int resp = JOptionPane.showConfirmDialog((Component) PluginServices - .getMainFrame(), PluginServices.getText(this, - "fichero_ya_existe_seguro_desea_guardarlo"), PluginServices - .getText(this, "guardar"), JOptionPane.YES_NO_OPTION); - if (resp != JOptionPane.YES_OPTION) { - return false; - } - } - NotificationManager.addInfo(PluginServices.getText(this, - "writing_project") + ": " + file.getName()); + /** + * Escribe el proyecto en disco. + * Pero permite decidir si se pide confirmación para sobreescribir + * + * @param file Fichero. + * @param p Proyecto. + * @param askConfirmation boolean + * @return + */ +public boolean writeProject(File file, Project p, boolean askConfirmation) { + I18nManager i18n = ToolsLocator.getI18nManager(); + ApplicationManager application = ApplicationLocator.getManager(); + if (askConfirmation && file.exists()) { + int resp = application.confirmDialog( + i18n.getTranslation("fichero_ya_existe_seguro_desea_guardarlo"), + i18n.getTranslation("guardar"), + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + "Overwrite_project_file" + ); + if (resp != JOptionPane.YES_OPTION) { + return false; + } + } + FileOutputStream fout=null; + ZipOutputStream zout=null; + LOG.info("Writing project '"+ file.getAbsolutePath()+"'."); + try { + fireBeforeSavingFileEvent(new SaveEvent(this,SaveEvent.BEFORE_SAVING, file)); - // write it out as XML - try { - fireBeforeSavingFileEvent(new SaveEvent(this, - SaveEvent.BEFORE_SAVING, file)); - - FileOutputStream fout = new FileOutputStream(file); - ZipOutputStream zout = new ZipOutputStream(fout); + fout = new FileOutputStream(file); + zout = new ZipOutputStream(fout); p.saveState(zout); - + zout.putNextEntry(new ZipEntry("preview.jpg")); BufferedImage img = ApplicationLocator.getManager().getUIManager().getImagePreview(); img = scale(img, 0.40); @@ -485,34 +488,55 @@ try { ImageIO.write(img, "jpg", zout); } catch (IOException ex) { - LOG.warn("Can't save preview image'.",ex); + LOG.warn("Can't save preview image'.", ex); } - + fireAfterSavingFileEvent(new SaveEvent(this, SaveEvent.AFTER_SAVING, file)); IOUtils.closeQuietly(zout); IOUtils.closeQuietly(fout); + + if( !isValidZIP(file) ) { + throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'"); + } - fireAfterSavingFileEvent(new SaveEvent(this, - SaveEvent.AFTER_SAVING, file)); + PluginServices.getMainFrame().setTitle(file.getName()); + setPath(file.toString()); - PluginServices.getMainFrame().setTitle(file.getName()); - setPath(file.toString()); + } catch (Exception e) { + application.messageDialog( + i18n.getTranslation("_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again"), + null, + i18n.getTranslation("guardar"), + JOptionPane.ERROR_MESSAGE, + "Problems_saving_the_project" + ); + LOG.warn("Error writing project '"+file.getAbsolutePath()+"'.", e); + return false; + } finally { + IOUtils.closeQuietly(zout); + IOUtils.closeQuietly(fout); + } + + LOG.warn("Wrote project '"+file.getAbsolutePath()+"'."); + return true; + } - } catch (PersistenceException e) { - String messagestack = e.getLocalizedMessageStack(); - NotificationManager.addError( - PluginServices.getText(this, "error_writing_project") - + ": " + file.getName() + "\n" + messagestack, e); - return false; - } catch (Exception e) { - NotificationManager.addError( - PluginServices.getText(this, "error_writing_project") - + ": " + file.getName(), e); - return false; - } - NotificationManager.addInfo(PluginServices.getText(this, - "wrote_project") + ": " + file.getName()); - return true; - } + boolean isValidZIP(final File file) { + ZipFile zipfile = null; + try { + zipfile = new ZipFile(file); + return true; + } catch (IOException e) { + return false; + } finally { + try { + if (zipfile != null) { + zipfile.close(); + zipfile = null; + } + } catch (IOException e) { + } + } + } private BufferedImage scale(BufferedImage before, double factor) { int w = (int) (before.getWidth()*factor); Index: src/main/java/org/gvsig/app/project/DefaultProject.java =================================================================== --- src/main/java/org/gvsig/app/project/DefaultProject.java (revisión: 43809) +++ src/main/java/org/gvsig/app/project/DefaultProject.java (copia de trabajo) @@ -30,6 +30,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; @@ -44,6 +45,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; import org.cresques.cts.IProjection; @@ -69,7 +72,6 @@ import org.gvsig.fmap.mapcontext.layers.FLayers; import org.gvsig.tools.ToolsLocator; import org.gvsig.tools.dynobject.DynStruct; -import org.gvsig.tools.exception.ListBaseException; import org.gvsig.tools.observer.ObservableHelper; import org.gvsig.tools.observer.Observer; import org.gvsig.tools.persistence.PersistenceManager; @@ -542,10 +544,31 @@ } catch (FileNotFoundException e) { throw new PersistenceException(e); } + if( !isValidZIP(out) ) { + throw new PersistenceException(new ZipException()); + } this.fname = out; notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out); } + boolean isValidZIP(final File file) { + ZipFile zipfile = null; + try { + zipfile = new ZipFile(file); + return true; + } catch (IOException e) { + return false; + } finally { + try { + if (zipfile != null) { + zipfile.close(); + zipfile = null; + } + } catch (IOException e) { + } + } + } + public File getFile() { return this.fname; } Index: src/main/resources-plugin/i18n/text.properties =================================================================== --- src/main/resources-plugin/i18n/text.properties (revisión: 43809) +++ src/main/resources-plugin/i18n/text.properties (copia de trabajo) @@ -1287,4 +1287,5 @@ _Change_view_projection=CRS Vista _Change_view_projection_to_projection_of_the_selected_item_in_the_list=Cambia la proyecci\u00f3n de la Vista a la proyecci\u00f3n del item seleccionado en la lista. _precision_comparing_decimal_numbers=Precisi\u00f3n de comparaci\u00f3n de n\u00fameros decimales -_default_expression_evaluator=Evaluador de expresiones por defecto \ No newline at end of file +_default_expression_evaluator=Evaluador de expresiones por defecto +_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again=Problemas guardando el proyecto.\nEs posible que este no se haya guardado correctamente y no se pueda volver a cargar. \ No newline at end of file Index: src/main/resources-plugin/i18n/text_en.properties =================================================================== --- src/main/resources-plugin/i18n/text_en.properties (revisión: 43809) +++ src/main/resources-plugin/i18n/text_en.properties (copia de trabajo) @@ -1206,4 +1206,5 @@ _Change_view_projection=CRS View _Change_view_projection_to_projection_of_the_selected_item_in_the_list=Change View projection to the projection of the selected item in the list _precision_comparing_decimal_numbers=Precision comparing decimal numbers -_default_expression_evaluator=Default expression evaluator \ No newline at end of file +_default_expression_evaluator=Default expression evaluator +_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again=Problems saving the project.\nIt is possible that this was not saved properly and can not be loaded again. \ No newline at end of file