gpt4 book ai didi

java - 如何在 java 中实用地从 jar 中删除特定文件/文件夹

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:44 24 4
gpt4 key购买 nike

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

我有一个 jar ABC.jar 包含文件、文件夹和另一个 jars 说 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.");
}

关于java - 如何在 java 中实用地从 jar 中删除特定文件/文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25440540/

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