如何知道压缩文件是否是Jar文件?

发布于 2024-11-30 04:56:47 字数 484 浏览 1 评论 0原文

问题是我们尝试使用 JarFile 类来确定文件是否是 Jar 文件。

但是,如果我们这样做,docx 文件也会被视为 Jar 文件。

因为docx文件实际上是一个zip文件。

这是我的代码:

   public static boolean detectJarFile(String file) throws IOException {
      try {
        JarFile jarFile = new JarFile(file);

      } catch (java.util.zip.ZipException e) {
        // the uploaded file is NOT JAR file
        return false;
      } 

      return true;
    }

我怎样才能知道一个文件是否真的是一个 Jar 文件?

有什么想法吗?

The problem is we try to use JarFile class to determine whether a file is Jar file or not.

However, if we do it this way, a docx file will also be considered as a Jar file.

Because the docx file is actually a zip file.

Here is my code:

   public static boolean detectJarFile(String file) throws IOException {
      try {
        JarFile jarFile = new JarFile(file);

      } catch (java.util.zip.ZipException e) {
        // the uploaded file is NOT JAR file
        return false;
      } 

      return true;
    }

How can I know whether a file is really a Jar file or not?

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森末i 2024-12-07 04:56:47

一般来说,Jar 文件只不过是具有一些特殊属性的 zip 文件(“普通”zip 文件可能具有,但不是必须具有)。

来自 JAR 文件规范

JAR 文件是一种基于流行的 ZIP 文件格式的文件格式,用于聚合许多
文件合二为一。 JAR 文件本质上是一个 zip 文件,其中包含可选的 META-INF 目录。

所以,看看你的候选人是否有这样的目录。如果有(并且至少有一个具有正确格式的 MANIFEST.MF),那么它就是一个 jar 文件。

但作为一个 jar 文件并没有什么特别的,这些知识并不能真正给你任何东西。这并不一定意味着其中有可以执行的类。

举个例子,OpenOffice.org 文件格式(至少是旧的,我不确定 OpenDocument)是基于 jar 格式的。

In general, a Jar file is nothing else than a zip file with some special properties (which a "normal" zip file might have, but not have to have).

From the JAR File Specification:

JAR file is a file format based on the popular ZIP file format and is used for aggregating many
files into one. A JAR file is essentially a zip file that contains an optional META-INF directory.

So, have a look if your candidate has such a directory. If it has (and there is at least a MANIFEST.MF with the right format in it), it is a jar file.

But being a jar file is nothing special, this knowledge does not really give you anything. It does not have to mean that there are classes in there which you can execute.

As an example, the OpenOffice.org file format (at least the old one, I'm not sure about OpenDocument) is based on the jar format.

何时共饮酒 2024-12-07 04:56:47

// the uploaded file is NOT JAR file

Yes, it is. The problem here is that you assume certain things about a JAR that aren't true. Contrary to some of the other answers here, the only requirement for a JAR file is that it's in the ZIP format. The META-INF/MANIFEST.MF file is optional. A jar isn't required to have .class files in it. It's not required to be anything but a ZIP file. You should reformulate your question to specify exactly what it is you're looking for because it's not a JAR. I suspect that you want to verify that something's not there instead.

浅沫记忆 2024-12-07 04:56:47

您可以检查文件是否包含 MANIFEST.MF入口。

public static boolean detectJarFile(String file) throws IOException {
    ...
    // First check if this is a zip file.
    // Then do this.
    ZipFile zip = new ZipFile(file);
    boolean hasManifestEntry = zip.getEntry("META-INF/MANIFEST.MF") != null;
    zip.close();
    return hasManifestEntry;
}

You could check if the file contains a MANIFEST.MF entry.

public static boolean detectJarFile(String file) throws IOException {
    ...
    // First check if this is a zip file.
    // Then do this.
    ZipFile zip = new ZipFile(file);
    boolean hasManifestEntry = zip.getEntry("META-INF/MANIFEST.MF") != null;
    zip.close();
    return hasManifestEntry;
}
已下线请稍等 2024-12-07 04:56:47

除了 Kal 提供的答案之外,请检查该文件是否至少包含一个扩展名为“.class”的文件。否则,jar 文件将无法工作,并且几乎所有“.docx”文件都不会包含这样的文件。

In addition to the answer provided by Kal, check whether the file contains at least one file with the extension '.class'. A jar file won't work otherwise, and almost all '.docx' files will not have such a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文