views:

63

answers:

4

In my program i want the user to be able to take some images from a directory, and save them under a single file, that can be transferred to another computer possibly, and actually read and displayed(using the same program).

How would i go about doing this, especially if i want to save other data along with it, perhaps objects and such. I know you can use the ObjectOutputStream class, but im not sure how to integrate it with images.

So overall, i want the program to be able to read/write data, objects, and images to/from a single file.

Thanks in Advance.

[EDIT - From Responses + Comment regarding Zip Files]

A zip might be able to get the job done.

But i want it to be read only be the program. ( You think making it a zip, changing the file extension would work, then when reading it just chaing it back and reading as a zip?? ) I dont want users to be able to see the contents directly.

Ill elaborate a bit more saying its a game, and users can create their own content using xml files, images and such. But when a user creates something i dont want other users to be able to see exactly how they created it, or what they used, only the end result.

+2  A: 

In your case I would use a ZIP library to package all the images in a ZIP file. For the metadata you want to save along with these, use XML files. XML and ZIP are quite a de-facto standard today, simple to handle and though flexible if you want to add new files or metadata. There are also serializing tools to serialize your objects into XML. (I don't know them exactly in Java, but I'm sure there are.)

chiccodoro
Check the second comment ( Three similar responses sorta, so i just posted it there )
Larry
A: 

Yep, just pack/unpack them with java.util.zip.* which is pretty straightforward to go. Every Windows Version since XP has built in zip support, so your good to go. There are many good (and faster) free zip libraries for java/c#, too.

atamanroman
A: 

I know you can use the ObjectOutputStream class, but im not sure how to integrate it with images.

Images are binary data, so reading it into a byte[] and writing the byte[] to ObjectOutputStream should work. It's however only memory hogging since every byte eats at least one byte of JVM's memory. You'll need to take this into account.

BalusC
Yeah, that is what i was using when i asked the question, but it was the images that had me confused a bit.
Larry
A: 

You can programatically create a zip file, and read a zip file from Java, no need to expose it as a regular .zip file.

See: java.io.zip pacakge for more information, and these others for code samples on how to read/write zip using java.

Now if you want to prevent the users from unzipping this file, but you don't want to complicate your life by encrypting the content, or creating a complex format, you can emulate a simple internet message format, similar to the one used for e-mails to attach files.

You can read more about the internet message format here

This would be a custom file format only used by your application so you can do it as simple as you want. You just have to define your format.

It could be:

  • Header with the names ( and number ) of files in that bundle.
  • Followed by a list of separators ( for instance limit.a.txt=yadayada some identifier to know you have finished with that content )
  • Actual content

So, you create the bundle with something like the following:

public void createBundle() {
    ZipOutputStream out = .... 
    writeHeader( out );
    writeLimits( out yourFiles );
    for( File f : youFiles ) {
        writeFileTo( f, out );
     }
     out.close();
}

Sort of...

And the result would be a zipped file with something like:

 filenames =a.jpg, b.xml, c.ser, d.properties, e.txt
 limits.a.jpg =poiurqpoiurqpoeiruqeoiruqproi
 limits.b.xml =faklsdjfñaljsdfñalksjdfa
 limit.s.ser =sdf09asdf0as9dfasd09fasdfasdflkajsdfñlk
 limit.d.properties =adfa0sd98fasdf90asdfaposdifasdfklasdfkñm
 limit.e.txt =asdf9asdfaoisdfapsdfñlj
 attachments=
 <include binary data from a.jpg here>
 --poiurqpoiurqpoeiruqeoiruqproi
 <include binary data from b.xml here>
 --faklsdjfñaljsdfñalksjdfa

etc

Since is your file format you can keep it as simple as possible or complicate your life at infinitum.

If you manage to include a MIME library in your app, that could save you a lot of time.

Finally if you want to add extra security, you have to encrypt the file, which is not that hard after all, the problems is, if you ship the encrypting code too, your users could get curious about it and decompile them to find out. But a good encrypting mechanism would prevent this.

So, depending on your needs you can go from a simple zip, a zip with a custom format, a zip with a complicated customformat or a zip with a custom complicated encrypted format.

Since that's to broad you may ask about specific parts here: http://stackoverflow.com/questions/ask

OscarRyz
Thanks for the in-depth response, ill have to take some time looking over all this.
Larry
I hope it helps. BTW, Welcome to Stackoverflow, If you find an answer that is helpful, you may upvote it ( clicking the upper triangle by the number next to the question ) or downvote it if something is ostentatious wrong. Also, if you find an answer that exactly answer your doubt you can mark it as accepted ( by clicking the green arrow below the numbers ) B-)
OscarRyz