Save an object and resources to the same file in java - java

im developing a program in which I need to make a save file that contains a serialized object with all the settings and some images that the user added to the program, i tried adding the images as imageicons to the object so they would get serialized with it but it turned out to be very inefficient regarding size, then, i tried serializing the object through a objectoutputstream and then in the same file serializing the image with imageoutputstream, but when i deserialize the image it is corrupt, the object does deserialize correctly tho, so how can i make a save file that contains a serialized object and some resourses in the same file?? (like a project save file) thanks in advance

You can implement some class which handles packaging system. Firstly serializing the Object with ObjectOutputStream and then save the resource next to it (can be in some folder when theres more resources) and pack it into ZIP archive (Java got API for this). The extension can be something else than ".zip"...Microsoft docx format or OpenOffice odt format is also a ZIP archive which contains many XML files... :)

Related

How to detect an HTML file that had extension changed to Excel .xls

I have a java app that processes excel files from emails in an automated fashion (.xls, xlsx etc). I've noticed that some files are not native files. Opening in Excel will give a warning that the file is corrupt/badly formated. Opening in notepad++ clearly shows HTML
Unfortunately I can't just manually handle these files so I need a way to automatically spot them.
I noticed that when I use java.io.fiile object then with org.apache.tika.Tika I can detect the the type. So with the file object I can find out the extension, and with tika.detect() i can find that the format is called "text/html". (Not sure if this is the best way, but it seems to work with my singular example)
So I can then find these kinds of files using:
File file = getTheFileObject();
if ( tika.detect(file).equalsIgnoreCase("text/html") && file.getName().contains(".xls") ) { ... do what I want with the corrupt file... }
My problem comes when doing something similar with email attachments. To get the file from emails I'm using the com.microsoft.ews-java-api 2.0 and from this I can get a FileAttachment object which represents the file.
But when I attempt to use tika.detect() on this (same corrupt file) i get a different format output "application/octet-stream" instead of "text/html". Or get "application/vnd.ms-excel" using the FileAttachments own methods
How can I spot these corrupt files if I can't spot the html formated xls files?
FileAttachment attachment = getFileAttachment();
attachment.getContentType() //application/vnd.ms-excel
tika.detect(attachment.getContentStream()) //application/octet-stream
How would I spot an html file that has .xls file extension from the emails ews FileAttachment object? Will tika still help?

EXIFTool JSON to EXIF batch processing

I have >400 JPG files and a JSON file for each which contains the image tags, description and title. I've found this command
exiftool -json=picture.json picture.jpg
But I don't want to run this for each and every file.
How can I run this command for the folder containing the JPGs and JSONs or is there another way I can batch process these?
Each JSON file has the same name as it's JPG counterpart so it's easy to identify which files match up to each other.
Assuming your JPGs and JSONs have the same filename, but different extesion(e.g. picture001.jpg has an associated picture001.json,etc.), a batch for loop might work.
Assuming you've already cd-ed into the folder and the files aren't nested in folders, something like this should work
( for jpg in *.jpg; do exiftool -json=${jpg/\.jpg/.json} $jpg; done )
Note that this isn't tested. I recommend making a copy of your folder and testing there beforehand to make sure you don't irreversibly damage them.
I've also noticed you're using the java tag. I had to work with EXIF data in Java a while back (on Android then) and I used the JHeader library. If you want to roll your own little java command line tool, you should be able to use Java's IO classes to traverse your directory and files and the JHeader library to modify the EXIF data.

Two applications need to export and import a single file which needs to include data and images, best file type?

I'm making two Java applications one to collect data, another to use it. The one collecting will be importing a file from the other which will include data and images and will be decrypted.
I'm unsure what filetype to use. So far all of the data is in XML and works great but I need the images and was hoping not to have to rely on giving all the images in a folder with a path reference.
Ideas?
well, I think that the best way is to create your own format (.myformat or .data). This file will be in fact a Zip file that contains your XML file and images.
There is no perfect example writen in java as far as I know. However, here are some examples :
Not in java
The best example is, as #Bolo said, the odt format. Indeed, OpenOffice writes the doc in an xml file, and the images too. All that is wrapped in an odt file.
The .exe file is an other example. The C files and the resources are put in a single file. try to open it with 7-zip, you'll see.
The Skyrim plugins are .esp file that contain the dds, the scripts, the niffs (textures)...
In java
The minecraft texture packs are a zip file that contains a .mcmeta file (the infos) and the textures (.png)
Jar files are like exe.
If both programs are in java you could also go with serialization, which is basically saving an object as a file (suffix will be .ser I think) and then being able to retrieve it. You should google it, even if it won't help right now it is quite good to know about it.
I'd suggest using JSON. Gson is a decent library.
You can embed images as byte arrays.
Save the serialized string in a file with a preferred extension, read it from the second application, de-serialize, and reconstruct images.
You can convert binary image data to text with Base64 encoding and this way you can embed your images in XML. [1]: http://en.wikipedia.org/wiki/Base64

FileInputStream without Creating an Actual File using Java

I was wondering if there was any way to create a FileInputStream object from just a file object without creating an actual file on the file system? What I am attempting to do is create a file object with some information, and then upload that file somewhere else. I have no need for it to be on the local file system. I know that I could just create a temp folder and then delete it afterwards, but was wondering if it was possible to not do it that way?
What I am attempting to do is create a file object with some
information, and then upload that file somewhere else
In that case you should not work with any file-related classes at all. Instead, crate a byte array, which you can tread as an InputStream via ByteArrayInputStream.
You are probably looking for a ByteArrayInputStream or something similar.
A file input stream reads from a file on disk, that is its purpose. By the way, a File object in Java does not really represent a file, but rather the path pointing to a (potential) file on disk.
Try creating a memory stream, your file is stored in the memory instead of the file system

Modifying an Existing PDF without creating an new pdf file

Using iText, I am wanting to open a PDF file, add some more pages with text to it, and then close it. I have found some questions like this on here, but all require creating a new PDF file. Is there any way to read in the pdf file and modify it and then overwrite the original?
Of course you can create a new pdf file, and afterwards overwriting the old file with the new one.
Commons Apache File Util
forceDelete(oldPdf)
moveFile(newPdf, oldPdf)
Of course, you can always overwrite a file (if it is not locked by the OS) by writing to the whole content to the FileOutputStream. You cannot partially write to part of a file unless it is to append data at the end of file. This is limited by the operating system itself so there is nothing you can do.

Categories

Resources