我正在开发一款游戏,我需要加载多个图像文件(png、gif 等),我最终希望将其转换为 BufferedImage 对象。在我的设置中,我想从一个 zip 文件“Resources.zip”中加载所有这些图像。该资源文件将包含图像、地图文件和音频文件——所有这些都包含在各种整齐有序的子目录中。我想这样做是因为它(希望)使我的程序的小程序和应用程序版本中的资源加载变得容易。我也希望对于小程序版本,这种方法可以让我轻松显示游戏资源 zip 文件的加载进度(最终可能达到 10MB,具体取决于游戏的精细程度,尽管我希望将其保持在该大小以下,以使其对浏览器友好)。
我在下面包含了我的 zip 处理类。这个想法是,我有一个单独的资源处理类,它创建一个 ZipFileHandler 对象,用于从 Resources.zip 文件中提取特定资源。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipFileHandler
{
private ZipFile zipFile;
public ZipFileHandler(String zipFileLocation)
{
try
{
zipFile = new ZipFile(zipFileLocation);
}
catch (IOException e) {System.err.println("Unable to load zip file at location: " + zipFileLocation);}
}
public byte[] getEntry(String filePath)
{
ZipEntry entry = zipFile.getEntry(filePath);
int entrySize = (int)entry.getSize();
try
{
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
byte[] finalByteArray = new byte[entrySize];
int bufferSize = 2048;
byte[] buffer = new byte[2048];
int chunkSize = 0;
int bytesRead = 0;
while(true)
{
//Read chunk to buffer
chunkSize = bis.read(buffer, 0, bufferSize); //read() returns the number of bytes read
if(chunkSize == -1)
{
//read() returns -1 if the end of the stream has been reached
break;
}
//Write that chunk to the finalByteArray
//System.arraycopy(src, srcPos, dest, destPos, length)
System.arraycopy(buffer, 0, finalByteArray, bytesRead, chunkSize);
bytesRead += chunkSize;
}
bis.close(); //close BufferedInputStream
System.err.println("Entry size: " + finalByteArray.length);
return finalByteArray;
}
catch (IOException e)
{
System.err.println("No zip entry found at: " + filePath);
return null;
}
}
}
我使用这样的 ZipFileHandler 类:
ZipFileHandler zfh = new ZipFileHandler(the_resourceRootPath + "Resources.zip");
InputStream in = new ByteArrayInputStream(zfh.getEntry("Resources/images/bg_tiles.png"));
try
{
BufferedImage bgTileSprite = ImageIO.read(in);
}
catch (IOException e)
{
System.err.println("Could not convert zipped image bytearray to a BufferedImage.");
}
好消息是,它有效!
但我觉得可能有更好的方法来做我正在做的事情(而且我对使用 BufferedInputStreams 还很陌生)。
最后,我的问题是:
这甚至是个好主意吗?
有没有更好的方法以对小程序和应用程序友好的方式在单个下载/流中加载一大堆游戏资源文件?
我欢迎所有的想法和建议!
谢谢!