projectestac / jclic

JClic is a set of cross-platform Java applications useful for creating and carrying out different types of educational activities like puzzles, associations, text exercises or crosswords.
GNU General Public License v2.0
27 stars 19 forks source link

Corrupted jclic.zip file if image not found when saving #26

Closed ferrarimartin closed 6 years ago

ferrarimartin commented 6 years ago
 When saving a file to jclic.zip format by using JClic author, if there's a resource (for example, an image) that's missing, the target jclic.zip file starts being written, but when the resource would be saved, an error appears, writing stops at that point, and the output  zip file gets corrupted and can't be opened.
 This could happen if for example an image from a usb drive was included, and the drive removed before saving.
  It would be good if the behavior was less drastic, because if the user closes Jclic at that point, their work is lost.
frncesc commented 6 years ago

Class edu.xtec.jclic.FileSystem#SecureFileOutputStream has been modified to avoid this kind of errors. The original workflow when saving projects was:

1 - Create a temporary ZIP file 2 - Write the project definition (.jclic file) and needed ingredients (images, sounds...) into this temporary file. 3 - If something goes wrong, catch the exception and notify the user about the error found. The project state remains as "unsaved", and the original file has been not modified. 4 - From here, we assume that everything went well. The temporary ZIP file contains the saved project and is in good condition. 5 - Replace the original project file with the temporary ZIP file. This is done deleting the original file and, then, renaming the temporary.

In some cases, and for unknown reasons, the assertion made in step 4 is not met. Actually, the temporary file is corrupted and no error or exception is thrown by the Java engine. This results in the loss of the original project.

Now, since JClic v0.3.2.0, we perform an additional step:

2b - Open the temporary file, read its the ZIP directory and check for empty or inaccessible files inside it. If something goes wrong, throw an IOException.

Hope this will reduce the risk of producing corrupted files when saving projects. Please notify us if the problem appears again.

Thanks for reporting!

ferrarimartin commented 6 years ago

Thank you so much Francesc. I've added some missing translations for portuguese in launchpad, in case they are useful.