Java - Reading multiple images from a single zip file and eventually turning them into BufferedImage objects. Good idea? -
I am working on a game, and I want to load more than one image file (PNG, GIF, etc.) I need to convert to BufferedImage objects at the end. In my setup, I want to load all these images with a zip file "Resource.zip". That resource file will include images, map files, and audio files - all contained in various streamlined sub-directories. I want to do this because it has to load easy resources in both the (expected) applet and the application version of my program. I also hope that for the applet version, this method makes it easy for me to show the loading progress of the game resources of zip file (which can eventually be 10 MB amounting to how wide this game can be, Although I'm expecting
I have included my zip handling class below. I think, I have a separate processing handling class, and it creates a zipfile handler object Which uses resources to pull out specific resources.
Import java.io.BufferedInputStream; Import java.io.File; import java.io.IOException; import java Use.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public square zipfilehandler {private zipfile zipfile; public zipfilehandler (string zip file location) {{zipFile = new zip file (zip file location);} Grip (IOException e) {System.err.println ("Locate zip file in Assam to load Th: "+ zipfile location);}} public byte [] getEntry (string Failpath) {ZipEntry entry = zipFile.getEntry (filePath); Int entry size = (int) entry.getSize (); Try {BufferedInputStream bis = New BufferedInputStream (zipFile.getInputStream (Entry)); Byte [] Last Bytere = New Byte [Entry]; Int buffer size = 2048; Byte [] buffer = new byte [2048]; Int chuck size = 0; Int BitesRad = 0; While (true) {// buffer chuck readjust the piece = BIS Read (buffer, 0, buffer size); // read () gives the number of bytes read (chunkSize == -1) {// read (1) returns if the brake has reached the end of the stream; } // Write Final Battery / System Arrhypopy (SRC, srcPos, dest, destPos, length) system. ArrayCopy (buffer, 0, final batterier, bytes read, Chuck size); Bytes Reid + = Beam size; } Bis.close (); // Close BufferedInputStream System.err.println ("Entry Size:" + finalByteArray.length); Return final bouterere; } Hold (IOException e) {System.err.println ("no zip entry found:" + filepath); Return tap; }}} And I use the ZipFileHandler class like this:
ZIPfilehandler zfh = new ZIPFileHandler (the_resourceRootPath + "resources.zip"); InputStream = new bytereinputstream (zfh.getEntry ("Resource / Images / BG_Tiles.png")); Try {BufferedImage bgTileSprite = ImageIO.read (in); } Grip (IOE EXPRESS E) {System.err.println ("Zip image could not be converted for a buffeted image."); } And the good news is, it works!
But I think that what I am doing can be a better way (and I
Finally, my question is:
This is also a good idea?
Is there a better way
Thanks!
Thanks Take several resources and put them in a compressed file is how many web applications work (i.e., GWT). It is less costly to load a larger file compared to very small ones. Cheli believes that you are going to use all those resources in your app, even if lazy loading is not a viable alternative. It is said that the application is usually best to work. And then to figure out where the obstacles are from. If you do not end up with a very complex code, and it will take a lot of time for your app to work. 80% to execute the code -20% -90% of the time Deficit is. You do not know that completed 10-20% of the entire project.
if your goal is to learn the techniques and Tinker, has been so good running - looks good.
Comments
Post a Comment