如何在Java中实用地从jar中删除特定的文件/文件夹。

我有一个jar ABC.jar包含文件,文件夹,另一个jar是child.jar。

在child.jar下,我想删除特定文件。 我能怎么做? 这样我的ABC.jar结构保持不变。

任何帮助将不胜感激。

提前致谢。

Jar是zip,因此您始终可以解压缩jar,删除要删除的内容,然后再压缩回jar。 Java具有用于管理zip文件的util软件包:docs.oracle.com/javase/7/docs/api/java/util/zip/

您将必须将内容从源Jar复制到临时Jar,跳过不需要的内容,删除原始jar,然后将tmp重命名为其位置...

我想在递归模式下做。

是否可能有一种快速的方法,可以从Jar / war中删除文件而无需解压缩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.");
}
}
@Raj Kantaria,这不是我所需要的。
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"));
}

通过在运行时执行命令外壳,有一种简单的方法可以从JAR中删除文件。

执行以下命令即可完成该工作:

Runtime.getRuntime().exec("zip -d path\my.jar some_file.txt");

其中path是jar文件的绝对路径,some_file.txt是要删除的文件。在此示例中,文件位于jar的主文件夹中。如果文件位于其他文件夹中,则可能需要提供其相对路径

您可能预先知道或可能根据正在执行命令外壳程序的类相对地找到它的jar本身的路径:

String path = SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

您可以通过侦听可用的流来跟踪流程的执行情况:

Process p = Runtime.getRuntime().exec("zip -d path\my.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 +"
");
System.out.println(line);
}
System.out.println(sb.toString());

如何在子jar中或从大子jar中删除文件? 建议的代码仅在同一个jar /文件夹中有用,而对子jar不有用。

我认为rn的最简单方法是为子jar提供一些静态方法,让我们说deleteFile(filepath)以便父jar可以调用

我没有得到。 你什么意思?

您可以在child.jar中实现delete方法。 当您要删除某些文件时,ABC.jar将调用child.jar-> delete()方法。

我必须先删除子jar才能删除吗?

让我们继续聊天中的讨论。

Sun / Oracle错误数据库要求在Java api中实现此功能。

在这里检查

Bug提交日期:" 1998-04-16" ...今天的日期:" 2014-08-22" ...拥有它会很酷,但是在本世纪,我不会指望它。

Jar / zip文件不可编辑。您无法从jar / zip文件中删除条目。

您可以做的是像这样"重新创建" jar文件:启动一个新的jar文件,遍历当前jar文件的条目,然后将这些条目添加到您不想删除的新jar文件中。

从理论上讲,可以删除这样的条目(但是标准Java库不适合此条目,这意味着ZipFile,ZipInputStream,JarFile,JarInputStream类):

jar / zip文件中的条目是顺序的。每个条目都有一个标头,其中包含有关该条目的信息(可以是文件或文件夹条目)。此标头还包含条目的字节长度。因此,您可以依次遍历这些条目,如果遇到要删除的条目(并且从其标题知道其大小),则可以将文件的其余部分(此条目之后的内容)复制到当前条目(显然,文件大小应被当前/已删除条目的长度截断)。

或者,您的其他选择包括不通过Java来执行此操作,而是使用zip -d命令本身之类的外部工具。

感谢icza的建议。 您可以在示例程序上帮我吗?

哪一个? 是"重新创建"还是"理论"解决方案?