如何在Java中实用地从jar中删除特定的文件/文件夹 [英] How to delete a specific File/Folder from a jar pragmatically in java

查看:351
本文介绍了如何在Java中实用地从jar中删除特定的文件/文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to delete a specific File/Folder from a jar pragmatically in java.

我有一个jar ABC.jar包含文件,文件夹和另一个jar,例如child.jar. 在child.jar下,我想删除一个特定的文件.我能怎么做?这样我的ABC.jar结构保持不变.

I have a jar ABC.jar contains files, folder and another jars say child.jar. under child.jar I want to delete a specific file. How can I do? so that my ABC.jar structure remains same.

任何帮助将不胜感激.

Any help will be appreciate.

在此先感谢.

推荐答案

@icza回答,我们必须遍历原始jar文件并删除不需要的条目. 这是您可以参考的Java代码.

As answered by @icza we have to iterate through original jar file and deleting the entry we don't want. Here is the java code you can refer.

 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中删除特定的文件/文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆