gpt4 book ai didi

java - 从当前运行的 jar 中提取一个 jar

转载 作者:行者123 更新时间:2023-12-04 05:47:32 24 4
gpt4 key购买 nike

我正在尝试从当前运行的 jar 中提取 2 个 jar 文件,但是它们总是以 2kb 结束,即使它们的大小是 104kb 和 1.7m,这是我得到的

public static boolean extractFromJar(String fileName, String dest) {
if (Configuration.getRunningJarPath() == null) {
return false;
}
File file = new File(dest + fileName);
if (file.exists()) {
return false;
}

if (file.isDirectory()) {
file.mkdir();
return false;
}
try {
JarFile jar = new JarFile(Configuration.getRunningJarPath());
Enumeration<JarEntry> e = jar.entries();
while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
InputStream in = new BufferedInputStream(jar.getInputStream(je));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
copyInputStream(in, out);
}
return true;
} catch (Exception e) {
Methods.debug(e);
return false;
}
}

private final static void copyInputStream(InputStream in, OutputStream out)
throws IOException {
while (in.available() > 0) {
out.write(in.read());
}
out.flush();
out.close();
in.close();
}

最佳答案

这应该比依赖 InputStream.available() 方法效果更好:

private final static void copyInputStream(InputStream in, OutputStream out)
throws IOException {
byte[] buff = new byte[4096];
int n;
while ((n = in.read(buff)) > 0) {
out.write(buff, 0, n);
}
out.flush();
out.close();
in.close();
}

关于java - 从当前运行的 jar 中提取一个 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com