如何在java中实用地从jar中删除特定的File/Folder .
我有一个jar包含ABC.jar文件夹,另一个jar说child.jar。在child.jar下,我想删除一个特定的文件。我该怎么办?使ABC.jar结构保持不变。
任何帮助将不胜感激。
提前感谢。
正如@icza所回答的,我们必须遍历原始jar文件并删除我们不想要的条目。这是你可以参考的java代码。
public static void main(String[] args) throws IOException {
String jarName = args[0];
String fileName = args[1];
// Create file descriptors for the jar and a temp jar.
File jarFile = new File(jarName);
File tempJarFile = new File(jarName + ".tmp");
// Open the jar file.
JarFile jar = new JarFile(jarFile);
System.out.println(jarName + " opened.");
// Initialize a flag that will indicate that the jar was updated.
boolean jarUpdated = false;
try {
// Create a temp jar file with no manifest. (The manifest will
// be copied when the entries are copied.)
Manifest jarManifest = jar.getManifest();
JarOutputStream tempJar =
new JarOutputStream(new FileOutputStream(tempJarFile));
// Allocate a buffer for reading entry data.
byte[] buffer = new byte[1024];
int bytesRead;
try {
// Open the given file.
FileInputStream file = new FileInputStream(fileName);
try {
// Create a jar entry and add it to the temp jar.
JarEntry entry = new JarEntry(fileName);
tempJar.putNextEntry(entry);
// Read the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " added.");
}
finally {
file.close();
}
// Loop through the jar entries and add them to the temp jar,
// skipping the entry that was added to the temp jar already.
for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
// Get the next entry.
JarEntry entry = (JarEntry) entries.nextElement();
// If the entry has not been added already, add it.
if (! entry.getName().equals(fileName)) {
// Get an input stream for the entry.
InputStream entryStream = jar.getInputStream(entry);
// Read the entry and write it to the temp jar.
tempJar.putNextEntry(entry);
while ((bytesRead = entryStream.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
}
}
jarUpdated = true;
}
catch (Exception ex) {
System.out.println(ex);
// Add a stub entry here, so that the jar will close without an
// exception.
tempJar.putNextEntry(new JarEntry("stub"));
}
finally {
tempJar.close();
}
}
finally {
jar.close();
System.out.println(jarName + " closed.");
// If the jar was not updated, delete the temp jar file.
if (! jarUpdated) {
tempJarFile.delete();
}
}
// If the jar was updated, delete the original jar file and rename the
// temp jar file to the original name.
if (jarUpdated) {
jarFile.delete();
tempJarFile.renameTo(jarFile);
System.out.println(jarName + " updated.");
}
}
Sun/Oracle错误数据库要求在java api中实现此功能。
这里的检查
有一种从JAR中删除文件的简单方法,即在运行时执行命令shell。执行下面的命令完成以下工作:
Runtime.getRuntime().exec("zip -d pathmy.jar some_file.txt");
其中path是jar文件的绝对路径,some_file.txt是要删除的文件。在本例中,该文件位于jar的主文件夹中。如果文件位于不同的文件夹
,则可能需要提供其相对路径。jar本身的路径,你可能提前知道,或者可以根据你正在执行的命令shell的类相对地找到它:
String path = SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
您可以通过监听可用的流来跟踪进程的执行:
Process p = Runtime.getRuntime().exec("zip -d pathmy.jar some_file.txt");
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine())!= null) {
sb.append(line + "n");
System.out.println(line);
}
System.out.println(sb.toString());
Jar/zip文件不可编辑。你不能从jar/zip文件中删除条目。
你能做的是像这样"重新创建"jar文件:启动一个新的jar文件,遍历当前jar文件的条目,并将这些条目添加到你不想删除的新jar文件中。
理论上可以这样删除条目(但是标准Java库不适合这样,这意味着ZipFile
, ZipInputStream
, JarFile
, JarInputStream
类):
jar/zip文件中的条目是顺序的。每个条目都有一个标题,其中包含有关条目的信息(可能是文件或文件夹条目)。这个报头还包含表项的字节长度。所以你可以按顺序遍历条目,如果你遇到一个想要删除的条目(并且你可以从它的头知道它的大小),你可以将文件的剩余部分(该条目之后的内容)复制到当前条目的开头(显然文件大小应该被当前/删除条目的长度截断)。
或者你的其他选择包括不通过Java,而是使用外部工具,如zip -d
命令本身。
public static void filterJar(Path jarInFileName, String skipRegex, Path jarOutFileName) throws IOException {
ZipEntry entry;
ZipInputStream zis = null;
JarOutputStream os = null;
FileInputStream is = null;
try {
is = new FileInputStream(jarInFileName.toFile());
Pattern pattern = Pattern.compile(skipRegex);
zis = new ZipInputStream(is);
os = new JarOutputStream(new FileOutputStream(jarOutFileName.toFile()));
while ((entry = zis.getNextEntry()) != null) {
if (pattern.matcher(entry.getName()).matches()) continue;
os.putNextEntry(entry);
if (!entry.isDirectory()) {
byte[] bytes = toBytes(zis);
os.write(bytes);
}
}
}
catch (Exception ex) {
throw new IOException("unable to filter jar:" + ex.getMessage());
}
finally {
closeQuietly(is);
closeQuietly(os);
}
}
public static void closeQuietly(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
}
catch (final Exception e) {}
}
public static byte[] toBytes(InputStream aInput) throws IOException {
byte[] bucket = new byte[5 * 1024];
ByteArrayOutputStream result = null;
result = new ByteArrayOutputStream(bucket.length);
int bytesRead = 0;
while (bytesRead != -1) {
bytesRead = aInput.read(bucket);
if (bytesRead > 0) {
result.write(bucket, 0, bytesRead);
}
}
return result.toByteArray();
}
public static void main(String[] args) throws IOException {
filterJar(Paths.get("./old.jar"), "BOOT-INF/lib.*", Paths.get("./new.jar"));
}
您可以使用Zip文件系统将Zip/jar文件视为文件系统。这将允许您编辑,删除和添加文件到jar文件。
参见使用Java将文件附加到zip文件