无法从 Mac 中的 zip 存档中提取可执行文件
Cannot extract the executable file from zip archieve in Mac
我想从 zip archieve 中提取 Unix 可执行文件。它在 Windows 上正常工作,但在 Mac 上不工作。我的代码下载 zip 文件并将其解压缩到特定位置。它确实提取了文件,但该文件是可执行文件,并将其提取为 TextEdit 文档。当我手动提取它时,我得到了可执行文件 file.Should 我强制执行某些操作或是否有任何权限?提前致谢。
这是我的解压函数:
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();
}
}
默认情况下,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
传入,但稍后仍会在 ZipInputStream()
中调用 extractedFilePath.toString()
。
伙计们,我找到了解决方案。
Runtime.getRuntime().exec("chmod u+x " + filepath);
当我将其放入我的代码块时 worked.As 我之前猜到它需要此权限。谢谢你们。
我想从 zip archieve 中提取 Unix 可执行文件。它在 Windows 上正常工作,但在 Mac 上不工作。我的代码下载 zip 文件并将其解压缩到特定位置。它确实提取了文件,但该文件是可执行文件,并将其提取为 TextEdit 文档。当我手动提取它时,我得到了可执行文件 file.Should 我强制执行某些操作或是否有任何权限?提前致谢。
这是我的解压函数:
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();
}
}
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
传入,但稍后仍会在 ZipInputStream()
中调用 extractedFilePath.toString()
。
伙计们,我找到了解决方案。
Runtime.getRuntime().exec("chmod u+x " + filepath);
当我将其放入我的代码块时 worked.As 我之前猜到它需要此权限。谢谢你们。