下面是我在另一条评论中承诺的代码......这不是我记得的,但它可能会让你开始。
本质上你调用:String fileName = FileUtils.getFileName(Main.class,“foo.txt”);
它会发现该文件在磁盘或JAR文件中。如果它在JAR文件中,则将其提取到临时目录。然后,您可以使用“新文件(文件名)”来打开该文件,无论以前它在哪里,都将在磁盘上。
我会做的是看看getFile方法,看看你可以用JAR文件做什么来迭代它的内容,并找到给定目录中的文件。
就像我说的,不完全是你想要的,但确实为你做了很多最初的工作。
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class FileUtils
{
public static String getFileName(final Class<?> owner,
final String name)
throws URISyntaxException,
ZipException,
IOException
{
String fileName;
final URI uri;
try
{
final String external;
final String decoded;
final int pos;
uri = getResourceAsURI(owner.getPackage().getName().replaceAll("\\.", "/") + "/" + name, owner);
external = uri.toURL().toExternalForm();
decoded = external; // URLDecoder.decode(external, "UTF-8");
pos = decoded.indexOf(":/");
fileName = decoded.substring(pos + 1);
}
catch(final FileNotFoundException ex)
{
fileName = null;
}
if(fileName == null || !(new File(fileName).exists()))
{
fileName = getFileNameX(owner, name);
}
return (fileName);
}
private static String getFileNameX(final Class<?> clazz, final String name)
throws UnsupportedEncodingException
{
final URL url;
final String fileName;
url = clazz.getResource(name);
if(url == null)
{
fileName = name;
}
else
{
final String decoded;
final int pos;
decoded = URLDecoder.decode(url.toExternalForm(), "UTF-8");
pos = decoded.indexOf(":/");
fileName = decoded.substring(pos + 1);
}
return (fileName);
}
private static URI getResourceAsURI(final String resourceName,
final Class<?> clazz)
throws URISyntaxException,
ZipException,
IOException
{
final URI uri;
final URI resourceURI;
uri = getJarURI(clazz);
resourceURI = getFile(uri, resourceName);
return (resourceURI);
}
private static URI getJarURI(final Class<?> clazz)
throws URISyntaxException
{
final ProtectionDomain domain;
final CodeSource source;
final URL url;
final URI uri;
domain = clazz.getProtectionDomain();
source = domain.getCodeSource();
url = source.getLocation();
uri = url.toURI();
return (uri);
}
private static URI getFile(final URI where,
final String fileName)
throws ZipException,
IOException
{
final File location;
final URI fileURI;
location = new File(where);
// not in a JAR, just return the path on disk
if(location.isDirectory())
{
fileURI = URI.create(where.toString() + fileName);
}
else
{
final ZipFile zipFile;
zipFile = new ZipFile(location);
try
{
fileURI = extract(zipFile, fileName);
}
finally
{
zipFile.close();
}
}
return (fileURI);
}
private static URI extract(final ZipFile zipFile,
final String fileName)
throws IOException
{
final File tempFile;
final ZipEntry entry;
final InputStream zipStream;
OutputStream fileStream;
tempFile = File.createTempFile(fileName.replace("/", ""), Long.toString(System.currentTimeMillis()));
tempFile.deleteOnExit();
entry = zipFile.getEntry(fileName);
if(entry == null)
{
throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
}
zipStream = zipFile.getInputStream(entry);
fileStream = null;
try
{
final byte[] buf;
int i;
fileStream = new FileOutputStream(tempFile);
buf = new byte[1024];
i = 0;
while((i = zipStream.read(buf)) != -1)
{
fileStream.write(buf, 0, i);
}
}
finally
{
close(zipStream);
close(fileStream);
}
return (tempFile.toURI());
}
private static void close(final Closeable stream)
{
if(stream != null)
{
try
{
stream.close();
}
catch(final IOException ex)
{
ex.printStackTrace();
}
}
}
}
编辑:
对不起,我没有时间来对这一权利的工作,现在(如果我得到一些我会做到这一点并张贴在这里的代码)。这就是我会做,虽然:
- 看那ZipFile类,并使用entries()方法来发现所有的文件/目录中的zip文件。
- ZipEntry有一个isDirectory()方法,您可以使用它来弄清楚它是什么。
- 我认为我发布在this answer中的代码将为您提供一种方法来选择一个临时目录来提取内容。
- 我认为我发布在this answer的代码可以帮助将ZipEntry内容复制到文件系统。
- 一旦这些项目在文件系统上,您已经拥有的用于迭代目录的代码仍然可以工作。您可以在上面的代码中为FileUtils类添加一个新方法,并能够像现在这样查找所有文件。
有可能是一个更好的方法来做到这一点,但我的头顶我认为这将工作。
如何使用ant build脚本将所有类和资源文件添加到自我可执行Jar文件中? – Pool 2010-03-07 00:10:33
听起来不错,但我不知道蚂蚁不幸的是:(为什么有人downvote这个? – Martin 2010-03-07 01:49:28
如承诺我已经发布了一些代码作为一个新的答案。它可能会帮助你开始寻找在JAR内的每个目录中的文件 – TofuBeer 2010-03-07 17:36:52