从Jar中更有效地提取文件 [英] Extracting files from a Jar more efficently

查看:157
本文介绍了从Jar中更有效地提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展一个实用程序类,捆绑一组图像和.xml描述文件。目前,我将所有的文件保存在一个目录,并从那里加载它们。目录如下所示:

I'm extending a utility class that bundles a set of images and .xml description files. Currently I keep all the files in a directory and load them from there. The directory looks like this:

8.png
8.xml
9.png
9.xml
10.png
10.xml
...
...
50.png
50.xml
...




这是我当前的构造函数。它是闪电快,做我需要它做的。 (我已经删除了一些错误检查,使其更容易阅读):




Here's my current constructor. It is lightning fast and does what I need it to do. (I've stripped out some of the error checking to make it easier to read):

public DivineFont(String directory ) {

    File dir = new File(directory);

    //children is an array that looks like this: '10.fnt', '11.fnt', etc.
    String[] children = dir.list(fntFileFilter);

    fonts = new Hashtable<Integer, AngelCodeFont>(100);

    AngelCodeFont buffer;
    int number;
            String fntFile;
            String imgFile;

    for(int k = 0; k < children.length; k++ ) {
        number = Integer.parseInt( children[k].split("\\.")[0] );
        fntFile = directory + File.separator + number + ".xml";
        imgFile = directory + File.separator + number + ".png";
        buffer = new AngelCodeFont(fntFile, imgFile);

        fonts.put(number, buffer);
    }
}

为了webstart和清洁,尝试从Jar加载这些资源。我已经工作,但负载时间从瞬间到几秒钟,这是不能接受的。这里是我试过的代码(再次,错误检查剥离):

For the sake of webstart and cleanliness, I've been trying to load these resources from a Jar instead. I've got it working, but the load time went from instantaneous to a few seconds, and that's not acceptable. Here's the code I tried (again, error checking stripped):

(这不是最好的方式做我想做的,它是一个模拟,以查看是否的想法工作,它没有。两个for循环不是问题的根源;它是创建所有的InputStreams,减慢它的过程)

(This isn't the best way to do what I want to do, it's a mock-up to see if the idea worked. It didn't. The two for-loops is in no way the source of the problem; it's the process of creating all those InputStreams that slows it down)

public DivineFont(String jarFileName ) {

    JarFile jarfile = new JarFile(jarFileName);
    Enumeration<JarEntry> em = jarfile.entries();
    ArrayList<Integer> fontHeights = new ArrayList<Integer>(100);
    for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements(); ) {
        String fileName = em1.nextElement().toString();
        if( fileName.endsWith(".fnt") ) {
            fontHeights.add( Integer.parseInt(fileName.split("\\.")[0] ) );
        }
    }

    fonts = new Hashtable<Integer, AngelCodeFont>(100);

    AngelCodeFont buffer;
    int number;

    for(int k = 0; k < fontHeights.size(); k++ ) {
        number = fontHeights.get(k);
        InputStream fntFileStream = jarfile.getInputStream(jarfile.getEntry(number + ".xml"));
        InputStream pngFileStream = jarfile.getInputStream(jarfile.getEntry(number + ".png"));
        buffer = new AngelCodeFont(String.valueOf(number), fntFileStream, pngFileStream );

        fonts.put(number, buffer);


    }
}


任何人都知道更好的方式使用.jar文件除了我在这里尝试的方式吗?以下是 AngelCodeFont API 。如果绝对需要我可以提交补丁,但我宁愿不必。在我看来,可能有一种方法来做我想做的,我只是不熟悉它。

Anyone know of a better way to work with .jar files besides the way I've tried here? Here's the AngelCodeFont API. If it was absolutely necessary I could submit a patch for that, but I'd rather not have to. It seems to me that there's probably a way to do what I want to do, I'm just not familiar with it.

我不太可能快速将jar转储到临时目录,然后从那里读取文件,但如果有办法从jar直接读取很快,我宁愿这样做。

I'm not terribly against quickly dumping the jar to a temporary directory and then reading the files from there, but if there's a way to do it reading directly from the jar quickly, I'd much rather do that.

另外:压缩不是一个问题。我使用jar的唯一原因是打包问题。

Also: Compression isn't an issue at all. The only reason I'm using a jar is for the packing issue.

推荐答案

好吧,我找到了答案。我原来问题的答案是错误的问题。 Jar文件不是问题,它是我用来加载图像的库。

Well, I found the answer. The answer to my original question was "Wrong question." The Jar file wasn't the issue, it was the library I was using to load the images.

当我从文件系统加载时,图像被命名38.png等。当我从Jar装载,我只是命名为38。

When I was loading from the file system, the image was being named "38.png", etc. When I was loading from the Jar, I was simply naming it "38".

库中的图像加载器类使用名称的文件扩展名来标识要使用的图像加载器。如果没有文件扩展名,它使用一个较慢的基本映像加载器。当我改变这一行:

The Image loader class inside the library uses the file extension of the name to identify which image loader to use. If there is no file extension, it uses a slower, basic image loader. When I changed this line:

buffer = new AngelCodeFont(String.valueOf(number), fntFileStream, pngFileStream );

到此行:

buffer = new AngelCodeFont( number + ".png", fntFileStream, pngFileStream );

我们有自己赢家。

感谢帮助反正家伙。我花了几个小时才弄明白这一点,但是如果不是为了你的帖子,我可能会继续假设是Java的而不是我使用的图书馆。

Thanks for the help anyway guys. It took me a few hours to figure this out, but if not for your posts, I probably would have continued to assume the blame was Java's rather than the Library I'm using.

这篇关于从Jar中更有效地提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆