FINAL UPDATE: I managed to work it out. Am still interested in case anyone has a nice way of putting together a large number of objects into a single file in a way that is suitable for saving game map data
I am working on a Tile-based labyrinth game. I want to be able to save an entire Map (or level, you might also call it) into a single file.
Currently I can save all the information about the Map structure as text and use that to recreate the Map later, but I have to save/load all the images I use to individual files which are loaded separately. If possible, I would like to be able to save all the images I use into the same file as the map itself. Maybe in a structure similar to how a folder looks like, only as a single file. This is to guarantee that if the Map exists, all of the resources are there as well.
So, my question; is there a way to make a single file that can contain multiple files, similar to how a folder would be like? The file would start with a set of text (a number of lines) and it’d be fine if I have to put the information about the images that are stored there as text as well if I need to do that in order to load the images that I want to save after the text in the file
UPDATE: I am trying to use Zip Files for this. This is my code so far (first part seems to work, see bottom of the post for my current problem; reading the saved image back)
public static void saveAllToFile(String path, String textData, String[] imageNames, BufferedImage[] images) throws Exception{
File saveFile = new File(path);
if(!saveFile.exists()) saveFile.createNewFile();
FileOutputStream fos = new FileOutputStream(saveFile);
ZipOutputStream zos = new ZipOutputStream(fos);
addToZipFile("TEXT_DATA", textData.getBytes(), zos); //First store the general Text Data that I want to access later as a single String
int limit = Math.min(imageNames.length, images.length);
ByteArrayOutputStream baos;
for(int i=0; i<limit; i++){
baos = new ByteArrayOutputStream(); //Using this method to convert the BufferedImage to a byte[]
ImageIO.write( images[i], "jpg", baos );
baos.flush();
addToZipFile(imageNames[i], baos.toByteArray(), zos); //Then I add that image data to the Zip File
baos.close();
}
zos.close();
fos.close();
}
private static void addToZipFile(String entryName, byte[] data, ZipOutputStream zos) throws Exception {
if(DEBUG) System.out.println("Writing '" + entryName + "' to zip file");
ZipEntry zipEntry = new ZipEntry(entryName); //Create an Entry to store the data in
zos.putNextEntry(zipEntry);
zos.write(data, 0, data.length); //Write the Entry to the Zip File
zos.closeEntry();
}
The issue I am facing now is how to properly read it back later.
SECOND UPDATE: I managed to figure it out. All is well!
public static BufferedImage[] loadImagesFromZipFile(String path) throws Exception{
File file = new File(path);
if(!file.exists()) return null;
ZipFile zFile = new ZipFile(file);
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
FileInputStream fis = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry;
BufferedImage img;
while((entry = zis.getNextEntry()) != null){
if(entry.isDirectory()) continue;
img = ImageIO.read(zFile.getInputStream(entry));
if(img != null) images.add(img);
}
return images.toArray(new String[images.size()]);
}