以编程方式将一个 jar 文件复制到另一个 jar 文件
Programatically copy one jar file into another jar file
我想用下面的代码复制一个jar文件到一个文件夹中
public static void copyJarFile(JarFile jarFile, File destDir) throws IOException {
String fileName = jarFile.getName();
String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
File destFile = new File(destDir, fileNameLastPart);
JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
InputStream is = jarFile.getInputStream(entry);
//jos.putNextEntry(entry);
//create a new entry to avoid ZipException: invalid entry compressed size
jos.putNextEntry(new JarEntry(entry.getName()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
is.close();
jos.flush();
jos.closeEntry();
}
jos.close();
}
该程序运行良好。但是当目标不是一个目录而是一个 jar 文件时,这个程序就不起作用了。即我想将一个 jar 文件复制到另一个 jar 文件中。我如何以编程方式做到这一点?
Jar 文件只是一个奇特的命名 zip 文件,具有特定的文件结构和 .jar 扩展名。因此,如果您能够使用它将所有内容转储到一个文件夹中,并且该文件夹具有您正在寻找的确切结构,您可以将该目录压缩为一个 zip 文件,然后重命名该 zip 文件 whatever.jar
而不是 whatever.zip
.
当你说 I want to copy one jar file into another jar file
时,你到底是什么意思?
假设你原来的原始jar名为a.jar
,而你想创建b.jar
。您的意思是希望 a.jar
中的所有项目都在 b.jar
中,就像它们原来一样?或者您希望 b.jar
包含一个名为 a.jar
的条目,它是 a.jar
的完整副本?
如果您只想 b.jar
包含 a.jar
中的所有项目,您当前的代码应该可以工作,尽管有更有效的方法可以做到这一点。如果这就是你的意思,我建议直接复制文件。
如果您想将 b.jar
中的整个 a.jar
作为条目添加,而不是循环遍历 a.jar
中的条目,您只需创建一个 FileInputStream
a.jar
并从中阅读。
InputStream is = new FileInputStream("a.jar");
jos.putNextEntry(new JarEntry("a.jar"));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
我想用下面的代码复制一个jar文件到一个文件夹中
public static void copyJarFile(JarFile jarFile, File destDir) throws IOException {
String fileName = jarFile.getName();
String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
File destFile = new File(destDir, fileNameLastPart);
JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
InputStream is = jarFile.getInputStream(entry);
//jos.putNextEntry(entry);
//create a new entry to avoid ZipException: invalid entry compressed size
jos.putNextEntry(new JarEntry(entry.getName()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
is.close();
jos.flush();
jos.closeEntry();
}
jos.close();
}
该程序运行良好。但是当目标不是一个目录而是一个 jar 文件时,这个程序就不起作用了。即我想将一个 jar 文件复制到另一个 jar 文件中。我如何以编程方式做到这一点?
Jar 文件只是一个奇特的命名 zip 文件,具有特定的文件结构和 .jar 扩展名。因此,如果您能够使用它将所有内容转储到一个文件夹中,并且该文件夹具有您正在寻找的确切结构,您可以将该目录压缩为一个 zip 文件,然后重命名该 zip 文件 whatever.jar
而不是 whatever.zip
.
当你说 I want to copy one jar file into another jar file
时,你到底是什么意思?
假设你原来的原始jar名为a.jar
,而你想创建b.jar
。您的意思是希望 a.jar
中的所有项目都在 b.jar
中,就像它们原来一样?或者您希望 b.jar
包含一个名为 a.jar
的条目,它是 a.jar
的完整副本?
如果您只想 b.jar
包含 a.jar
中的所有项目,您当前的代码应该可以工作,尽管有更有效的方法可以做到这一点。如果这就是你的意思,我建议直接复制文件。
如果您想将 b.jar
中的整个 a.jar
作为条目添加,而不是循环遍历 a.jar
中的条目,您只需创建一个 FileInputStream
a.jar
并从中阅读。
InputStream is = new FileInputStream("a.jar");
jos.putNextEntry(new JarEntry("a.jar"));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}