Unzipping file whilst getting correct permissions?

17

6

I unzipped a file on my server using:

unzip filename

However this seems to have caused all the files having owner/group permissions of 0 0.

The files I uploaded were a PHP software script, so I'm wondering how I set the permissions properly when unzipping the files and what they should be set to?

Brett

Posted 2013-06-03T10:07:56.207

Reputation: 761

Answers

10

Zip doesn't support saving file ownership/permission as far as I know. You can try setting the umask so unzip should create the files with these permissions. Run

umask 644

before unzip.

lawl0r

Posted 2013-06-03T10:07:56.207

Reputation: 243

8@lawl0r yes it does. Use the -Z option to see them. – OrangeDog – 2015-07-27T16:31:24.650

It was actually zipped with window, sorry. – Brett – 2013-06-03T10:31:30.977

1It doesn't matter if it was created on windows or *nix. Zip still doesn't support unix file permissions. Regardless where it was created. – lawl0r – 2013-06-03T10:33:56.767

Yeah, I know.... but you said I could try setting the umask so it would create the zip with these permissions; but don't think I can do that on windows? – Brett – 2013-06-03T10:40:03.367

Before unziping – lawl0r – 2013-06-03T10:41:10.557

Oh ok..... got a bit confused when you said "so zip should create the files with these permissions". :) – Brett – 2013-06-03T10:49:15.353

I thought in tar terms, where you use the same command to zip and unzip. Edited my answer. – lawl0r – 2013-06-03T11:58:44.187

20

Actually, some answers here are not correct. ZIP files can also have file permissions. (*) You can list the permissions of the files in your ZIP file with:

unzip -Z

Maybe the tool you used to create the ZIP file didn't store the permissions or didn't store them correctly.

So, if you made the ZIP file yourself, check the tool you made the ZIP file with. Maybe there is a way to set permissions before zipping (like with maven), or it preserves the original permissions (but that would only work on a system that supports permissions - i.e. not on Windows).

If you didn't make the ZIP file yourself, your only chance is to set the correct the permissions after unzipping, for example with

chmod -R [permissions] [directory]

(*) We use that feature in combination with the maven assembly plugin, where you can specify the fileMode for files that go into the ZIP file.

David Tanzer

Posted 2013-06-03T10:07:56.207

Reputation: 309

1i think you are right, but the way how you propose the answer it not right. you should focus to provide a good answer than criticize in a new one... – Francisco Tapia – 2015-07-14T15:20:39.497

2@FranciscoTapia I updated my answer (with some things one could try), but I'll leave the hint that other answers are incorrect in there. I think it's important. – David Tanzer – 2015-07-22T07:38:47.697

Which kind of zip unzip are you using? Because under some version -Z is for the compression method -Z cm or --compression-method cm... – Hastur – 2015-12-18T17:03:01.637

1UnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. on Windows / mingw. I currently don't have access to my linux box, but there -Z works too. Anyway, thanks for the hint! – David Tanzer – 2015-12-23T11:15:30.423

3@David Tanzer, your answer is the best even while it's not accepted. Indeed, the origin of an archive (e.g. Windows, Linux, etc) is important. I've tested on Linux (Zip 3.0, UnZip 6.00). Once a file is put into an archive the permissions are saved. And they being restored on the extracted file (e.g. when the file is extracted on Linux). Obviously, the permissions will be not applied when extracting on Windows box. It will be really cool to see in unzip a specific switch (like tar's --no-same-permissions) that doesn't preserve permissions and respects umask while extracting. – flaz14 – 2017-06-06T17:58:30.300

Not really sure if -Z is hepful, in my case (Linux) it only listed files as executable, which contained a windows-executable-ish file extension like .bat – Daniel F – 2018-06-21T17:15:41.460

1@DanielF Permissions are only there if the creating program put them there, so if your archive was created on Windows, zipinfo/unzip -Z can only guess. To see if the permissions are there, use zipinfo -v/unzip -Zv and look for “non-MSDOS external file attributes”; the two most significant bytes are the UNIX permissions (in hex, not octal!) provided the “file system or operating system of origin” is UNIX. – Alex Shpilkin – 2019-04-10T12:39:05.517