如何知道压缩文件是否是Jar文件?
问题是我们尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,Jar 文件只不过是具有一些特殊属性的 zip 文件(“普通”zip 文件可能具有,但不是必须具有)。
来自 JAR 文件规范:
所以,看看你的候选人是否有这样的目录。如果有(并且至少有一个具有正确格式的
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:
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.
是的,它是。这里的问题是您对 JAR 的某些假设是不正确的。与这里的一些其他答案相反,唯一要求< /a> 对于 JAR 文件来说,它是 ZIP 格式。 META-INF/MANIFEST.MF 文件是可选的。 jar 中不需要包含 .class 文件。它不需要是任何东西,只是 ZIP 文件。您应该重新表述您的问题,以准确指定您要查找的内容,因为它不是 JAR。我怀疑您想验证那里是否有东西不。
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.
您可以检查文件是否包含 MANIFEST.MF入口。
You could check if the file contains a MANIFEST.MF entry.
除了 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.