无法从Mac中的zip存档中提取可执行文件

时间:2015-01-26 12:28:43

标签: java macos unzip

我想从zip archieve中提取Unix可执行文件。它在Windows上正常工作但在Mac上不起作用。我的代码下载了zip文件并将其提取到特定位置。它确实提取文件,但文件是可执行文件,并将其作为TextEdit文档提取。当我通过手动提取它时,我得到了可执行文件。我应该强迫某些东西还是有任何许可?提前谢谢。

这是我的解压缩功能:

    public void decompress(String zipFilePath, String extractedFilePath) {
    byte[] buffer = new byte[1024];
    try {
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(extractedFilePath.toString()));
        ZipEntry ze = zipInputStream.getNextEntry();
        while (ze != null) {
            String filename = ze.getName();
            File newFile = new File(zipFilePath +_config.configProperties().getPathSeparator()+ filename + _config.configProperties().getVersion());

            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zipInputStream.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            ze = zipInputStream.getNextEntry();
        }
        zipInputStream.closeEntry();
        zipInputStream.close();
    }catch (IOException ex){
        ex.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:1)

默认情况下,Mac不知道如何处理.exe文件,除非您的系统上有Windows虚拟机(在这种情况下,虚拟机将打开.exe文件双击)。该文件可能与您的代码完全提取(很可能),但如果它有.exe扩展或根本没有扩展,那么它将默认为TextEdit文档,因为Mac不知道还有什么用它

您可以在压缩之前和之后验证文件的SHA1校验和,以确保它们完全相同。

your-mac:demo$ shasum manual_extraction.bin

your-mac:demo$ shasum code_extracted_version.bin

对于手动提取的版本(完美的版本)和您的代码提取的版本(已损坏的版本),终端上的Mac上的以下命令的结果会很有趣:

your-mac:demo$ xxd manual_extraction.bin| head

your-mac:demo$ xxd code_extracted_version.bin| head

只是旁注 - 我注意到您将extractedFilePath参数作为String传入,但稍后仍在extractedFilePath.toString()中调用ZipInputStream()

答案 1 :(得分:1)

伙计们我找到了解决方案。

 Runtime.getRuntime().exec("chmod u+x " + filepath);

当我把它放在我的代码块中时它起作用了。我之前猜到它需要这个权限。谢谢你们。

相关问题