1

客户端应该将压缩文件上传到 S3 文件夹中。然后下载压缩文件并解压缩以对其包含的文件执行各种操作。最初我们告诉我们的客户将其文件压缩成ZIP文件,但这对我们的客户来说太难了。相反,它提交了一个带有 ZIP 扩展名的RAR文件......多么聪明。由于显而易见的原因,无法使用ZIP解压缩算法解压缩RAR文件。

因此,我正在寻找一种方法来找出 S3 下载文件的文件类型,因为我正在使用 Amazon 的 SDK 在 Linux 操作系统上开发 Java 项目。我将根据获得的文件类型来处理如何解压文件。

我看过很多堆栈溢出问题,比如这个,但仅通过查看它们(及其评论),似乎没有一个是 100% 有效的。

找出压缩文件类型的最佳方法是什么?

4

1 回答 1

4

TL;博士;

当以编程方式将文件上传到 Amazon S3 时,可以指定对象的Content-Type. 如果没有指定,正如@Michael-bot 澄清的那样,默认分配的值将是binary/octet-stream. 或者,如果您决定通过 Amazon S3 的 GUI 上传文件,该文件会从其Content-Type文件扩展名中获取(遗憾的是,不是其内容)。如果您可以信任上传文件的人来Content-Type正确设置,请继续查看ObjectMetadata,但如果您不能(像我一样),您将需要另一个解决方案。

因此,如果您正在寻找适用于最常见文件压缩类型的解决方案,Files.probeContentTypeApache TikaSimpleMagic似乎是可以接受的解决方案。

最后我选择了Files.probeContentType因为它不需要额外的库并且在 Linux 机器上工作得很好(只要文件没有错误的扩展名,有一个解决方法:删除文件扩展名并让它发挥它的魔力)。


测试设置

一开始会认为从 Amazon 的 S3 下载文件时的响应对象包含文件类型。它确实包含此信息,但是当文件的扩展名与其内容不匹配时,就会出现问题。

import com.amazonaws.services.s3.model.S3Object;

final S3Object s3Object = ...;
final String contentType = s3Object.getObjectMetadata().getContentType();

application/zip即使文件的内容是 Rar 文件,此代码也会返回。所以这个解决方案对我不起作用。

出于这个原因,我花时间构建了一个示例项目,该项目使用不同的方法和可用的库测试了各种场景。顺便说一句,我正在使用Java 8

测试的文件类型有:

  • 带有 Zip 扩展名和不带扩展名的 Zip 文件
  • 带有 Rar 扩展名、Zip 扩展名和无扩展名的 Rar 文件
  • 具有 7z 扩展名、Zip 扩展名和无扩展名的 7z 文件
  • 带有 Tar.xz 扩展名、Zip 扩展名和不带扩展名的 Tar.xz
  • 带有 Tar.gz 扩展名、Zip 扩展名和不带扩展名的 Tar.gz

请注意,此处介绍的实现仅用于测试目的。它们不以任何方式被认可用于生产代码,因为它们不考虑文件锁定问题以及我的想象无法考虑的其他问题。=)


MimetypesFileTypeMap

执行

import java.io.File;
import javax.activation.MimetypesFileTypeMap;

final File file = new File(basePath + "/" + fileName);
try {
    return MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(file);
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/octet-stream
Rar with Zip extension is:       application/octet-stream
Zip with Zip extension is:       application/octet-stream
7z with 7z extension is:         application/octet-stream
7z with Zip extension is:        application/octet-stream
Tar.xz with Tar.xz extension is: application/octet-stream
Tar.xz with Zip extension is:    application/octet-stream
Tar.gz with Tar.gz extension is: application/octet-stream
Tar.gz with Zip extension is:    application/octet-stream
Rar without extension is:        application/octet-stream
Zip without extension is:        application/octet-stream
7z without extension is:         application/octet-stream
Tar.xz without extension is:     application/octet-stream
Tar.gz without extension is:     application/octet-stream

结论

当文件类型未被识别时,此方法返回的值为application/octet-stream. 似乎所有场景都失败了,所以我们应该放弃这种方法。


URLConnection.guessContentTypeFromStream

执行

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.net.URLConnection;

final File file = new File(basePath + "/" + fileName);
try {
    final FileInputStream fileInputStream = new FileInputStream(file);
    final InputStream inputStream = new BufferedInputStream(fileInputStream);

    return URLConnection.guessContentTypeFromStream(inputStream);
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       null
Rar with Zip extension is:       null
Zip with Zip extension is:       null
7z with 7z extension is:         null
7z with Zip extension is:        null
Tar.xz with Tar.xz extension is: null
Tar.xz with Zip extension is:    null
Tar.gz with Tar.gz extension is: null
Tar.gz with Zip extension is:    null
Rar without extension is:        null
Zip without extension is:        null
7z without extension is:         null
Tar.xz without extension is:     null
Tar.gz without extension is:     null

结论

同样,此方法在所有情况下都失败。看来它的支持非常有限


Files.probeContentType

执行

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

try {
    final Path path = Paths.get(basePath + "/" + fileName);
    return Files.probeContentType(path);
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/vnd.rar
Rar with Zip extension is:       application/zip
Zip with Zip extension is:       application/zip
7z with 7z extension is:         application/x-7z-compressed
7z with Zip extension is:        application/zip
Tar.xz with Tar.xz extension is: application/x-xz-compressed-tar
Tar.xz with Zip extension is:    application/zip
Tar.gz with Tar.gz extension is: application/x-compressed-tar
Tar.gz with Zip extension is:    application/zip
Rar without extension is:        application/vnd.rar
Zip without extension is:        application/zip
7z without extension is:         application/x-7z-compressed
Tar.xz without extension is:     application/x-xz
Tar.gz without extension is:     application/gzip

结论

这种方法效果出奇的好,但不要被愚弄,有一种情况会一直失败。如果文件的扩展名错误(不匹配的是内容),它将报告文件类型为扩展名。它不应该经常发生,但如果一个人非常挑剔,则不要使用此方法。

此外,有些人警告说他的方法在 Windows 中效果不佳

解决方法:如果设法从文件名中删除扩展名,这将为所有给定方案返回正确的值。


阿帕奇蒂卡(tika-eval 1.18)

这个库似乎有很多种风格(应用程序、服务器、评估等),但网络上的许多人抱怨它有点“依赖重”。

执行

import org.apache.tika.Tika;

try {
    return new Tika().detect(new File(basePath + "/" + fileName));
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/x-rar-compressed
Rar with Zip extension is:       application/x-rar-compressed
Zip with Zip extension is:       application/zip
7z with 7z extension is:         application/x-7z-compressed
7z with Zip extension is:        application/x-7z-compressed
Tar.xz with Tar.xz extension is: application/x-xz
Tar.xz with Zip extension is:    application/x-xz
Tar.gz with Tar.gz extension is: application/gzip
Tar.gz with Zip extension is:    application/gzip
Rar without extension is:        application/x-rar-compressed
Zip without extension is:        application/zip
7z without extension is:         application/x-7z-compressed
Tar.xz without extension is:     application/x-xz
Tar.gz without extension is:     application/gzip

结论

所有文件都被正确识别,但由于它有它的优点,它也有它的缺点。

优点:

  • 由阿帕奇维护。
  • 不会被扩展所迷惑。

缺点:

  • 真的很重,特别是如果只想检查获取文件类型。Tika-eval Jar 重量为 +40MB。

URL连接

执行

import java.net.URL;
import java.net.URLConnection;

try {
    final URL url = new URL("file://" + basePath + "/" + fileName);
    final URLConnection urlConnection = url.openConnection();
    return urlConnection.getContentType();
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       content/unknown
Rar with Zip extension is:       application/zip
Zip with Zip extension is:       application/zip
7z with 7z extension is:         content/unknown
7z with Zip extension is:        application/zip
Tar.xz with Tar.xz extension is: content/unknown
Tar.xz with Zip extension is:    application/zip
Tar.gz with Tar.gz extension is: application/octet-stream
Tar.gz with Zip extension is:    application/zip
Rar without extension is:        content/unknown
Zip without extension is:        content/unknown
7z without extension is:         content/unknown
Tar.xz without extension is:     content/unknown
Tar.gz without extension is:     content/unknown

结论

它几乎不识别任何文件压缩格式,并通过扩展名而不是其内容来引导自己。


简单魔术 1.14

这个项目似乎每年至少更新一次

执行

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;

try {
    final ContentInfoUtil util = new ContentInfoUtil();
    final ContentInfo info = util.findMatch(basePath + "/" + fileName);

    return info.getMimeType();
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/x-rar
Rar with Zip extension is:       application/x-rar
Zip with Zip extension is:       application/zip
7z with 7z extension is:         application/x-7z-compressed
7z with Zip extension is:        application/x-7z-compressed
Tar.xz with Tar.xz extension is: <EXCEPTION: null>
Tar.xz with Zip extension is:    <EXCEPTION: null>
Tar.gz with Tar.gz extension is: application/x-gzip
Tar.gz with Zip extension is:    application/x-gzip
Rar without extension is:        application/x-rar
Zip without extension is:        application/zip
7z without extension is:         application/x-7z-compressed
Tar.xz without extension is:     <EXCEPTION: null>
Tar.gz without extension is:     application/x-gzip

结论

它几乎适用于我们所有的场景,但似乎对于最“晦涩”的压缩格式,如 Tar.xz,它无法检测到它们(并在此过程中抛出异常)。


MimeUtil 2.1.3

这个项目自 2010 年以来没有被修改过,所以不要指望支持或更新。为了完整起见,仅在此处列出。

执行

import eu.medsea.mimeutil.MimeUtil2;

try {
    final MimeUtil2 mimeUtil = new MimeUtil2();
        mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");

    return MimeUtil2.getMostSpecificMimeType(mimeUtil.getMimeTypes(basePath + "/" + fileName)).toString();
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/x-rar
Rar with Zip extension is:       application/x-rar
Zip with Zip extension is:       application/zip
7z with 7z extension is:         application/octet-stream
7z with Zip extension is:        application/octet-stream
Tar.xz with Tar.xz extension is: application/octet-stream
Tar.xz with Zip extension is:    application/octet-stream
Tar.gz with Tar.gz extension is: application/x-gzip
Tar.gz with Zip extension is:    application/x-gzip
Rar without extension is:        application/x-rar
Zip without extension is:        application/zip
7z without extension is:         application/octet-stream
Tar.xz without extension is:     application/octet-stream
Tar.gz without extension is:     application/x-gzip

结论

它识别了一些最流行的文件类型,但无法识别 Tar.xz 和 7z。


文件 - 命令行

不是最漂亮的解决方案,但必须尝试:Ubuntu file command

执行

import java.io.BufferedReader;
import java.io.InputStreamReader;

try {
    final Process process = Runtime.getRuntime().exec("file --mime-type " + basePath + "/" + fileName);

    final BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String text = "";

    String s;
    while ((s = stdInput.readLine()) != null) {
        text += s;
    }

    return text.split(": ")[1];
} catch (final Exception exception) {
    return "<EXCEPTION: " + exception.getMessage() + ">";
}

结果

Rar with Rar extension is:       application/x-rar
Rar with Zip extension is:       application/x-rar
Zip with Zip extension is:       application/zip
7z with 7z extension is:         application/x-7z-compressed
7z with Zip extension is:        application/x-7z-compressed
Tar.xz with Tar.xz extension is: application/x-xz
Tar.xz with Zip extension is:    application/x-xz
Tar.gz with Tar.gz extension is: application/gzip
Tar.gz with Zip extension is:    application/gzip
Rar without extension is:        application/x-rar
Zip without extension is:        application/zip
7z without extension is:         application/x-7z-compressed
Tar.xz without extension is:     application/x-xz
Tar.gz without extension is:     application/gzip

结论

它适用于我们所有的场景,但同样,这依赖于File运行代码的系统上存在的命令。

于 2018-08-23T21:57:34.437 回答