[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to maintain permissions when unzipping .zip file



> I'll give it a shot, though I don't how what you want to could be
> really useful...with my default rights, I am not able to give
> ownership to another user usually...

Thanks for your scipt.  It wasn't exactly what I needed because I want
to be able to decompress the archive into an existing directory
structure without changing the already existing files.  But what you
provided gave me an idea how to do it.

Also, what you said about ownership is true, requiring a root login sometimes.

Below is the script I wrote that will unzip a .zip archive and change
the ownership and permissions to match those of the archive file. 
It's the first script I've written as well as my first time using awk,
so I'm sure I could have done some things in a simpler way, but here
it is for anyone who is interested.

Thanks everyone for your help.

--- 8< -----------------------------------------------------------------

#!/bin/bash
# Usage: 'zipchange.sh archive' where archive is a zipped archive

#read user, group, and mod permissions for archive using ls
ZIPUSER=`ls -l $1 | awk '{print $3}'`
ZIPGROUP=`ls -l $1 | awk '{print $4}'`
ZIPMOD=`ls -l $1 | awk '{print $1}'`

#seperate mod permissions into 3 seperate groups
ZIPOWN=`echo $ZIPMOD | awk '{x=substr($1,2,3);print x}'`
ZIPGRP=`echo $ZIPMOD | awk '{y=substr($1,5,3);print y}'`
ZIPALL=`echo $ZIPMOD | awk '{z=substr($1,8,3);print z}'`

#convert rwx type into octel number
OCTOWN=0
OCTGRP=0
OCTALL=0

if echo "$ZIPOWN" | grep -q "r"
  then let "OCTOWN += 4"
fi
if echo "$ZIPOWN" | grep -q "w"
  then let "OCTOWN += 2"
fi
if echo "$ZIPOWN" | grep -q "x"
  then let "OCTOWN += 1"
fi

if echo "$ZIPGRP" | grep -q "r"
  then let "OCTGRP += 4"
fi
if echo "$ZIPGRP" | grep -q "w"
  then let "OCTGRP += 2"
fi
if echo "$ZIPGRP" | grep -q "x"
  then let "OCTGRP += 1"
fi

if echo "$ZIPALL" | grep -q "r"
  then let "OCTALL += 4"
fi
if echo "$ZIPALL" | grep -q "w"
  then let "OCTALL += 2"
fi
if echo "$ZIPALL" | grep -q "x"
  then let "OCTALL += 1"
fi

#generate a list of unzipped files using zipinfo command
FILES=`zipinfo $1 | awk '{print $9}'`

#unzip archive
unzip $1

#change ownership and permissions of unzipped files to match zipped archive
for file in $FILES
do
  chown -v $ZIPUSER.$ZIPGROUP $file
  chmod -v $OCTOWN$OCTGRP$OCTALL $file
done

exit

--- 8< -----------------------------------------------------------------


-- 
Take care, eh.
Chris



Reply to: