在 .jar 中获取目录 [英] Getting a directory inside a .jar

查看:27
本文介绍了在 .jar 中获取目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问我的 jar 文件中的一个目录.我想浏览目录本身中的每个文件.例如,我尝试使用以下方法:

I am trying to access a directory inside my jar file. I want to go through every of the files inside the directory itself. I tried, for example, using the following:

URL imagesDirectoryURL=getClass().getClassLoader().getResource("Images");

if(imagesFolderURL!=null)
{
    File imagesDirectory= new File(imagesDirectoryURL.getFile());
}

如果我测试这个小程序,它运行良好.但是一旦我将内容物放入罐子中,由于多种原因,它不会.如果我使用此代码,则 URL 始终指向 jar 外部,因此我必须将 Images 目录放在那里.但是,如果我使用 new File(imagesDirectoryURL.toURI());,它在 jar 中不起作用,因为我收到错误 URI not hierarchy.我确定该目录存在于 jar 中.我应该如何获取 jar 中 Images 的内容?

If I test this applet, it works well. But once I put the contents into the jar, it doesn't because of several reasons. If I use this code, the URL always points outside the jar, so I have to put the Images directory there. But if I use new File(imagesDirectoryURL.toURI());, it doesn't work inside the jar because I get the error URI not hierarchical. I am sure the directory exists inside the jar. How am I supposed the get the contents of Images inside the jar?

推荐答案

Jars 中的路径是路径,而不是实际目录,因为您可以在文件系统上使用它们.获取 Jar 文件特定路径中的所有资源:

Paths within Jars are paths, not actual directories as you can use them on a file system. To get all resources within a particular path of a Jar file:

  • 获得一个指向 Jar 的 URL.
  • URL 获取一个 InputStream.
  • InputStream 构造一个 ZipInputStream.
  • 迭代每个 ZipEntry,寻找与所需路径的匹配项.
  • Gain an URL pointing to the Jar.
  • Get an InputStream from the URL.
  • Construct a ZipInputStream from the InputStream.
  • Iterate each ZipEntry, looking for matches to the desired path.

..如果我的 Applet 不在那个罐子里,我还能测试它吗?或者我必须编程两种方式来获取我的图像?

..will I still be able to test my Applet when it's not inside that jar? Or will I have to program two ways to get my Images?

ZipInputStream 不适用于文件系统目录中的松散资源.但是,我强烈建议使用诸如 Ant 之类的构建工具来构建(编译/jar/签名等)小程序.编写构建脚本可能需要一个小时左右的时间.检查它,但此后您可以通过几次按键和几秒钟来构建项目.

The ZipInputStream will not work with loose resources in directories on the file system. But then, I would strongly recommend using a build tool such as Ant to build (compile/jar/sign etc.) the applet. It might take an hour or so to write the build script & check it, but thereafter you can build the project by a few keystrokes and a couple of seconds.

如果我想测试我的Aplet,如果我总是必须提取并签署我的罐子,那会很烦人

It would be quite annoying if I always have to extract and sign my jar if I want to test my Aplet

我不确定你的意思.提取物"从何而来?如果我不清楚,沙盒小程序可以通过这种方式从 archive 属性中提到的任何 Jar 加载资源.您可能要做的另一件事是将资源 Jar 与小程序 Jar 分开.资源的变化通常少于代码,因此您的构建可能会走一些捷径.

I'm not sure what you mean there. Where does the 'extract' come into it? In case I was not clear, a sand-boxed applet can load resources this way, from any Jar that is mentioned in the archive attribute. Another thing you might do, is to separate the resource Jar(s) from the applet Jar. Resources typically change less than code, so your build might be able to take some shortcuts.

我想我真的必须考虑将我的图像放到 jar 之外的单独目录中.

I think I really have to consider putting my Images into a seperate directory outside the jar.

如果您的意思是在服务器上,如果没有服务器的帮助,将没有实用的方法来获取图像文件的列表.例如.某些服务器设置不安全,无法为任何没有默认文件的目录(例如 index.html)生成基于 HTML 的文件列表".

If you mean on the server, there will be no practical way to get a listing of the image files short of help from the server. E.G. Some servers are insecurely set up to produce an HTML based 'file list' for any directory with no default file (such as an index.html).

我只有一个罐子,里面有我的课程、图像和声音.

I have only got one jar, in which my classes, images and sounds are.

好的 - 考虑移动声音 &图像放入单独的 Jar 中.或者至少,将它们放入无压缩"的 Jar 中.虽然 Zip 压缩技术与类配合得很好,但它们在压缩(否则已经压缩)媒体格式方面效率较低.

OK - consider moving the sounds & images into a separate Jar. Or at the very least, put them in the Jar with 'no compression'. While Zip comression techniques work well with classes, they are less efficient at compressing (otherwise already compressed) media formats.

我必须签署它,因为我使用首选项"类来保存用户设置."

I have to sign it because I use the "Preferences" class to save user settings."

Preferences 可以替代 Applet,例如 cookie.在插件 2 架构小程序的情况下,您可以使用 Java Web 启动小程序(仍然嵌入在浏览器中)开始.JWS 提供了 PersistenceService.这是我的小型 演示.PersistenceService.

There are alternatives to the Preferences for applets, such as cookies. In the case of plug-in 2 architecture applet, you can launch the applet (still embedded in the browser) using Java Web Start. JWS offers the PersistenceService. Here is my small demo. of the PersistenceService.

说到 JWS,这让我想到:你绝对确定这款游戏作为一个小程序会更好,而不是一个应用程序(例如使用 JFrame)启动使用 JWS?

Speaking of JWS, that brings me to: Are you absolutely certain this game would be better as an applet, rather than an app (e.g. using a JFrame) launched using JWS?

Applet 会给您带来无尽的压力,而 JWS 自 Java 1.2 中引入以来就提供了 PersistenceService.

Applets will give you no end of stress, and JWS has offered the PersistenceService since it was introduced in Java 1.2.

这篇关于在 .jar 中获取目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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