5

I have a Java process (daemon) which is running. This process is used like a service. With that service I can download a signed zip archive from a trusted server. After I unpack the ZIP archive I want to execute a binary in the "unpacked" folder.

The problem is that this binary is not executable (no 'x' in its file mode) ... I know it's possible to set it with Java but I want a generic way.

Do you have any ideas how to do the deployment in a nice way?

(Yes, it needs to be Java)

Basically I want to restore the Unix file mode bits after I unpacked the ZIP archive. Is there a Java Lib which can do this?

Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
alexvetter
  • 1,998
  • 2
  • 16
  • 42
  • What do you mean by a nice way? If you have control over files going into the archive then just make sure the binary has the proper modes before packing it up. – Perception Mar 01 '12 at 18:50
  • @Perception: Not all zip compressors support storing the Unix file mode bits. And not all zip decompressors support restoring it... – thkala Mar 01 '12 at 18:53
  • @thkala That's what I need. Is there a Java Lib which can do this? – alexvetter Mar 01 '12 at 18:56
  • @alexvetter: I am not aware of any Java Zip library that supports Unix permissions. There is at least one related SO question [here](http://stackoverflow.com/q/1050560/507519). – thkala Mar 01 '12 at 19:07
  • Hmm, apparently [TrueZip](http://truezip.java.net/truezip-driver/truezip-driver-zip/index.html) has some initial support for extended file attributes, but it does not seem complete to the point of being usable for Unix permissions... – thkala Mar 01 '12 at 19:18

2 Answers2

3

You could easily call the chmod command from your Java application to add that missing x bit. chmod is as standard on Unix as the executable (x) bit itself.

Or you could do it straight from Java.

If you want the executable bit to remain set at decompression, though, you should probably not use the Java decompression libraries. Try using the Unix unzip command to decompress your archive - it has supported storing and restoring the Unix file mode bits for quite some time. If your Unix vendor does not have it, you can get it from the source.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 1
    Sorry, you're not helping. I can set the executeable flag right from Java. I don't know about all binaries so I can't set the flag. Nicest thing would be to "unpack" the archive and the flags are already set. – alexvetter Mar 01 '12 at 18:53
  • 'unzip -X' keeps the GID/UID. Problem is that jarsigner (tool which I use for signing) doesn't preserve the flags. So it will not work with out of the box. I'm think about adding a metainfofile which contains all executable files. 'find . -executable -type f' can help with that. – alexvetter Mar 02 '12 at 13:56
  • @alexvetter: so by the time your application receives the archive, the executable bits have already been lost :-/ – thkala Mar 02 '12 at 14:04
  • Apache Commons Compress' ZipArchiveEntry has the method getExternalAttributes(). With it you can get the flags (777) (you have to convert the long into octal representation). But anyway as I said this won't work for me (jarsigner problem).If it comes to a normal zip file your answer would be the best. – alexvetter Mar 02 '12 at 14:08
  • I'm going to implement another way of ensure that zip files were not changed. – alexvetter Mar 02 '12 at 14:18
0

If you pack your files with tar and then optionally compress, it keeps your permissions as they were before packing. Use --preserve-permissions when unpacking.

Erik Ekman
  • 2,051
  • 12
  • 13