Preserve file permissions when unzipping and the zipping files using ant

12,205

Solution 1

Turns out that ant will destroy all information on permissions when unzipping due to a restriction in Java. However, what is possible is to add files to an existing zip file which preserves the permissions of the existing files:

<!-- Add to zip -->
<zip destfile="${existingZipFiledirectory}.zip"
   basedir="${directoryOfFilesToAdd}"
   update="true"
/>

The above script will update the zip file specified with the content in basedir, preserving file permissions in the original zip.

Solution 2

I encountered same issue when using Ant unzip target:

<unzip src="${project.build.directory}/${project.build.finalName}.zip" dest="${user.home}/apps" overwrite="true" />

The permission of shell scripts inside the zip file was lost when using the unzip target above.

After some investigation, I use the follow 'exec' ant target with unzip command line parameters, it worked.

<!-- Use command line unzip to keep file permissions -->
<exec executable="unzip" spawn="true">
    <arg line="-o ${project.build.directory}/${project.build.finalName}.zip -d ${user.home}/apps" />
</exec>

I hope this can help someone else when encountering this kind of issues.

Thanks, J

Solution 3

You can't get the zip task to preserve file permissions, but you can set them explicitly:

<zip destfile="installer.zip" >
<zipfileset filemode="755" dir="../" includes="artisan/install.*" />
</zip>

(This worked for me on Windows and OSX)

Share:
12,205
pheelicks
Author by

pheelicks

http://www.pheelicks.com http://github.com/felixpalmer https://twitter.com/pheeelicks

Updated on June 04, 2022

Comments

  • pheelicks
    pheelicks almost 2 years

    I'm writing an ant build.xml file which does the following:

    • Takes a zipped folder (.zip)
    • Unzips it
    • Adds a number of files
    • Zips up the resulting files

    An extract of the code from build.xml:

    <!-- Unzip SDK to a temporary directory -->
    <unzip src="${zipFile}" dest="tmp"/>
    
    <!-- pull in the files from another directory -->
    <copy todir="tmp/someDirectory" >
      <fileset dir="${addedFiles}" />
    </copy>
    
    <!-- Zip up modified SDK -->
    <zip destfile="${destDir}" basedir="tmp"/>
    

    This all works perfectly, except that the permissions set for the zipped files prior to running the ant build are lost in the zip file created by the ant build. For example, files which were previously executable no longer are.

    So my question: is it possible to use ant to add files to a zip archive without destroying the permissions of the already present files?

    I'm using Ant 1.7.1

  • Ian Roberts
    Ian Roberts over 11 years
    No, preserve0permissions is different, that's to do with the case where you're adding entries to one zip file from another (using <zipfileset src="...">). Ant has always been able to copy permissions from one zip to another, but prior to Ant 1.8 if the source zip file had a zero value in the permissions field for a given entry, the entry in the target file would always be created with the default permissions (typically 644 or 755). preserve0permissions="yes" will instead preserve the zero value in the target zip file.
  • gouessej
    gouessej over 9 years
    It works under GNU Linux too, I've just tested with Mageia Linux 4. Thanks.
  • fscore
    fscore almost 9 years
    This unzips your zip file and you got the same permissions as you did when it was zipped up?