有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

支持GZip的java编写游戏文件存储?

    public static void loadModels() {
    try {
        DataInputStream indexFile = new DataInputStream(new FileInputStream("./cache/models.idx"));
        DataInputStream dataFile = new DataInputStream(new FileInputStream("./cache/models.dat"));
        int length = indexFile.readInt();
        for(int i = 0; i < length; i++) {
            int id = indexFile.readInt();
            int invlength = indexFile.readInt();
            byte[] data = new byte[invlength];
            dataFile.readFully(data);
            //System.out.println("ID: "+ id +" Length: "+ invlength +" Data: "+ data);
            Class30_Sub2_Sub4_Sub6.method460(data, id);
        }
        indexFile.close();
        dataFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我加载我的模型(顺便说一下,有57056个)方法460(数据,id)的空白;指从存储器调用模型

package com.rs.modelcompressor.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.util.List;
import java.util.ArrayList;

import com.rs.modelcompressor.Main;

public class FileController {

private List<Model> modelList = new ArrayList<Model>();

public FileController() {   
}

private byte[] build() {
    byte[] returnValue;
    int i = 0;
    for(Model m : modelList) {
        i += m.getData().length;
    }
    returnValue = new byte[i];
    int offset = 0;
    for(Model model : modelList) {
        Main.getUserInterface().getTextArea().append("Copying Model Data: "+ model.getId()+"\n");
        System.arraycopy(model.getData(), 0, returnValue , offset, model.getData().length);
        offset += model.getData().length;
    }
    return returnValue;
}

public void compress() {
    int offset = 0;
    if(modelList.size() == 0) {
        Main.getUserInterface().getTextArea().append("No models loaded\n");
        return;
    }

    try {
        byte[] data = build();
        Main.getUserInterface().getTextArea().append("Creating data file\n");
        DataOutputStream dataFile = new DataOutputStream(new FileOutputStream("models.dat"));
        dataFile.write(data, 0, data.length);
        Main.getUserInterface().getTextArea().append("Length of written data: "+ data.length +"\n");
        dataFile.flush();
        dataFile.close();
    } catch (Exception e) {
        Main.getUserInterface().getTextArea().append("An error occured while writing data file\n");
    }
    try {
        Main.getUserInterface().getTextArea().append("Creating index file\n");
        DataOutputStream indexFile = new DataOutputStream(new FileOutputStream("models.idx"));
        indexFile.writeInt(modelList.size());
        for(Model m : modelList) {
            indexFile.writeInt(m.getId());
            indexFile.writeInt(m.getData().length);
        }
        indexFile.flush();
        indexFile.close();
    } catch (Exception e2) {
        Main.getUserInterface().getTextArea().append("An error occured while writing index file\n");
    }
    Main.getUserInterface().getTextArea().append("model.dat and model.idx created\n");
}           

public void loadModels() {
    modelList.clear();
    File[] file = new File("models/").listFiles();
    Main.getUserInterface().getTextArea().append("Found "+ file.length +" model files\n");
    for(File f : file) {
        byte[] data = new byte[(int)f.length()];
        try {
            FileInputStream in = new FileInputStream(f);
            in.read(data);
            in.close();
        } catch (Exception e) {
            Main.getUserInterface().getTextArea().append(e.getMessage()+"\n");
        }
        String s = f.getName();
        int id = Integer.parseInt(s.substring(0, s.indexOf(".")));
        Main.getUserInterface().getTextArea().append("ID: "+ id +" Data: "+ data +" Length: "+ (int)f.length()+"\n");   
        Model m = new Model();
        m.setData(data);
        m.setId(id);
        modelList.add(m);
    }       
}

那是压缩机课,但我不知道该怎么做

看,我想用GZip压缩模型文件以减小文件大小。。。但是,如果我压缩gzip文件,它将使用。idx将导致使用错误的尺寸=>;错误。。。我该怎么计算呢?所以,文件被压缩到了。dat,但是。idx具有未压缩文件的大小/长度/w.e以及如何使加载程序支持gzip


共 (1) 个答案

  1. # 1 楼答案

    1. 分别压缩每个单独的模型,并将结果连接到models.dat文件
    2. 将压缩长度和偏移量添加到models.idx
    3. 读取模型时,使用models.idx的压缩长度和偏移量从models.dat读取一个字节块并解压缩该块,这将导致未压缩的模型

    或者你可以用类似的东西 ZipFile类。在这里,您可以将模型文件打包为zip文件,并为每个模型指定唯一的文件名,只需使用这种代码即可获取模型数据。这可能比在GZIP中使用上述方法稍微慢一点,但代码更简单

    zipFile = new ZipFile("models.zip");
    ZipEntry zipEntry = zipFile.getEntry(modelFilename);
    InputStream input = getInputStream(zipEntry);