如何在java中以实际方式从jar中删除特定的文件/文件夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在java中以实际方式从jar中删除特定的文件/文件夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2667字,纯文字阅读大概需要4分钟。
内容图文
![如何在java中以实际方式从jar中删除特定的文件/文件夹](/upload/InfoBanner/zyjiaocheng/771/178d07164f064c16a859f7fa8546ae72.jpg)
如何在java中以实际方式从jar中删除特定的文件/文件夹.
我有一个罐子ABC.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.");
}
}
内容总结
以上是互联网集市为您收集整理的如何在java中以实际方式从jar中删除特定的文件/文件夹全部内容,希望文章能够帮你解决如何在java中以实际方式从jar中删除特定的文件/文件夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。