我已经高低搜索了一种方法,找到各种方法来解决这个问题,但是没有一种方法可以帮助我。
我只需要一种方法将文件附加到Jar文件中,如果它们已经存在则覆盖,我想要最快的方法来执行此操作。我尝试将其转换为Zip文件并使用 Zip文件系统添加文件但我遇到错误,例如"找不到邮件END标题",其他方法有效但是速度很慢(6 MB的文件需要3分钟)。
将文件附加到Jar文件的最快方法是什么?
编辑:是的,继续投票,而不是回答,非常富有成效。
答案 0 :(得分:2)
您的更新方式与更新文本文件相同:
在您的情况下,这意味着:
ZipInputStream
。ZipOutputStream
。你当然可以翻转订单。
ZipOutputStream
。ZipInputStream
。答案 1 :(得分:0)
这是我能找到的最佳方式:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
public class JarUtil {
public static void updateJarFile(File srcJarFile, boolean update, File ...filesToAdd) throws IOException {
File tmpJarFile = File.createTempFile("tempJar", ".tmp");
JarFile jarFile = new JarFile(srcJarFile);
boolean jarUpdated = false;
List<String> fileNames = new ArrayList<String>();
try {
JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(tmpJarFile));
try {
// Added the new files to the jar.
for (int i = 0; i < filesToAdd.length; i++) {
File file = filesToAdd[i];
FileInputStream fis = new FileInputStream(file);
try {
byte[] buffer = new byte[1024];
int bytesRead = 0;
JarEntry entry = new JarEntry(file.getName());
fileNames.add(entry.getName());
tempJarOutputStream.putNextEntry(entry);
while ((bytesRead = fis.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead);
}
// System.out.println(entry.getName() + " added.");
} finally {
fis.close();
}
}
// Copy original jar file to the temporary one.
Enumeration<?> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry entry = (JarEntry) jarEntries.nextElement();
/*
* Ignore classes from the original jar which are being
* replaced
*/
String[] fileNameArray = (String[]) fileNames
.toArray(new String[0]);
Arrays.sort(fileNameArray);// required for binary search
if (Arrays.binarySearch(fileNameArray, entry.getName()) < 0) {
InputStream entryInputStream = jarFile
.getInputStream(entry);
tempJarOutputStream.putNextEntry(entry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = entryInputStream.read(buffer)) != -1) {
tempJarOutputStream.write(buffer, 0, bytesRead);
}
} else if (!update) {
throw new IOException(
"Jar Update Aborted: Entry "
+ entry.getName()
+ " could not be added to the jar"
+ " file because it already exists and the update parameter was false");
}
}
jarUpdated = true;
} catch (Exception ex) {
System.err.println("Unable to update jar file");
tempJarOutputStream.putNextEntry(new JarEntry("stub"));
} finally {
tempJarOutputStream.close();
}
} finally {
jarFile.close();
// System.out.println(srcJarFile.getAbsolutePath() + " closed.");
if (!jarUpdated) {
tmpJarFile.delete();
}
}
if (jarUpdated) {
srcJarFile.delete();
tmpJarFile.renameTo(srcJarFile);
// System.out.println(srcJarFile.getAbsolutePath() + " updated.");
}
}
}
原始来源(已修改): https://subversivebytes.wordpress.com/2012/10/11/java-programmatically-update-jar-file/ 希望这会有所帮助。