为什么不可能从jar文件中删除条目?

Vol*_*lyy 5 java jar

可以使用jar工具向jar文件添加新条目.
可以使用jar工具修改jar文件中的一些条目.
但是不可能从jar文件中删除一些条目.
为什么?

icz*_*cza 5

没有人真正回答最初的Why?问题,答案只是包含如何仍然做到这一点的提示。

所以为什么?

Jar 和 zip 文件是具有一组文件或目录(通常称为条目)的压缩文件。jar/zip 文件的结构/格式是条目的顺序枚举(除了 zip 文件格式标头之外)。

每个条目都有一个标头,其中包含有关该条目的信息,例如其名称、类型、字节长度(以及其他)。

现在使用这个顺序条目列表,您将如何从文件中间删除条目?它会在文件中间留下一个“洞”。删除条目的唯一方法(无需重新创建没有可删除条目的存档)需要将条目之后开始的 zip 文件内容复制到当前(可删除)条目的开头,并截断 zip 文件length 除以已删除条目的长度。

想象一下,如果您有数十或数百 MB 的存档,这意味着什么?如果要在开头删除一个条目,则必须将文件的几乎全部内容复制回几个字节(或千字节),以免在文件中留下间隙。

所以这就是为什么。

这种结构允许轻松添加新条目,因为它们可以轻松附加到文件末尾,但无法有效执行(条目)删除。


Rom*_*eau 3

jar 文件的目的是打包 Java 应用程序。当您打包应用程序进行部署时,实际上不需要删除条目。

以下是三种可能的方法。

  1. 您可以使用 winzip 或其他一些 zip 实用程序
  2. 您可以只使用带有正确条目的 jar (删除 jar 并重新打包)。
  3. 您可以通过编程来完成...

...有点,见下文:

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.jar.*;


public class JarUpdate
{
    /**
     * main()
     */
    public static void main(String[] args) throws IOException
    {
        // Get the jar name and entry name from the command-line.

        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.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)