Search code examples
javamacosunzip

Cannot extract the executable file from zip archieve in Mac


I want to extract Unix executable file from zip archieve. It works properly on Windows but does not work on Mac. My code downloads the zip file and extracts it into specific location. It does extract the file but the file is executable file and it extracts it as a TextEdit Document. When I extract it by manual, I get the executable file.Should I force something or is there any permission ? Thanks in advance.

Here is my decompress function :

    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();
    }
}

Solution

  • Guys I found the solution.

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

    When I put this in my code block it worked.As I guessed earlier it needs this permission. Thank you all.