0040000
is the traditional value of S_IFDIR
, the file type flag representing a directory. The type uses the top 4 bits of the 16-bit st_mode
value, 0100000
is the value for regular files.
The high 16 bits of the external file attributes seem to be used for OS-specific permissions. The Unix values are the same as on traditional unix implementations. Other OSes use other values. Information about the formats used in a variety of different OSes can be found in the Info-ZIP source code (download or e.g in debian apt-get source [zip or unzip]
) - relevant files are zipinfo.c
in unzip
, and the platform-specific files in zip
.
These are conventionally defined in octal (base 8); this is represented in C and python by prefixing the number with a 0
.
These values can all be found in <sys/stat.h>
- link to 4.4BSD version. These are not in the POSIX standard (which defines test macros instead); but originate from AT&T Unix and BSD. (in GNU libc / Linux, the values themselves are defined as __S_IFDIR
etc in bits/stat.h
, though the kernel header might be easier to read - the values are all the same pretty much everywhere.)
#define S_IFIFO 0010000 /* named pipe (fifo) */
#define S_IFCHR 0020000 /* character special */
#define S_IFDIR 0040000 /* directory */
#define S_IFBLK 0060000 /* block special */
#define S_IFREG 0100000 /* regular */
#define S_IFLNK 0120000 /* symbolic link */
#define S_IFSOCK 0140000 /* socket */
And of course, the other 12 bits are for the permissions and setuid/setgid/sticky bits, the same as for chmod:
#define S_ISUID 0004000 /* set user id on execution */
#define S_ISGID 0002000 /* set group id on execution */
#define S_ISTXT 0001000 /* sticky bit */
#define S_IRWXU 0000700 /* RWX mask for owner */
#define S_IRUSR 0000400 /* R for owner */
#define S_IWUSR 0000200 /* W for owner */
#define S_IXUSR 0000100 /* X for owner */
#define S_IRWXG 0000070 /* RWX mask for group */
#define S_IRGRP 0000040 /* R for group */
#define S_IWGRP 0000020 /* W for group */
#define S_IXGRP 0000010 /* X for group */
#define S_IRWXO 0000007 /* RWX mask for other */
#define S_IROTH 0000004 /* R for other */
#define S_IWOTH 0000002 /* W for other */
#define S_IXOTH 0000001 /* X for other */
#define S_ISVTX 0001000 /* save swapped text even after use */
As a historical note, the reason 0100000
is for regular files instead of 0 is that in very early versions of unix, 0 was for 'small' files (these did not use indirect blocks in the filesystem) and the high bit of the mode flag was set for 'large' files which would use indirect blocks. The other two types using this bit were added in later unix-derived OSes, after the filesystem had changed.
So, to wrap up, the overall layout of the extended attributes field for Unix is
TTTTsstrwxrwxrwx0000000000ADVSHR
^^^^____________________________ file type as explained above
^^^_________________________ setuid, setgid, sticky
^^^^^^^^^________________ permissions
^^^^^^^^________ This is the "lower-middle byte" your post mentions
^^^^^^^^ DOS attribute bits
Use Python's zipfile library?
~/wrk/tmp$ ls test.zip
ls: cannot access test.zip: No such file or directory
Ok. There is no 'test.zip' right now...
~/wrk/tmp$ python -c 'import zipfile,sys ; zipfile.ZipFile(sys.argv[1],"a").write(sys.a
rgv[2],sys.argv[3])' test.zip /etc/motd text/motd
Let's add '/etc/motd' as 'text/motd' to the nonexisting zipfile...
~/wrk/tmp$ ls -l test.zip
-rw-r--r-- 1 yeti yeti 473 Mar 23 09:51 test.zip
The zipfile library was nice enough to create 'test.zip'.
~/wrk/tmp$ unzip -lv test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
357 Stored 357 0% 2014-03-20 15:47 ff1b8b7f text/motd
-------- ------- --- -------
357 357 0% 1 file
..and it seems to contain what I wated...
Let's check it by unzipping it to stdout...
~/wrk/tmp$ unzip -p test.zip text/motd
Linux aurora 3.2.0-0.bpo.4-amd64 #1 SMP Debian 3.2.54-2~bpo60+1 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Fine!
Now we add a 2nd file...
~/wrk/tmp$ python -c 'import zipfile,sys ; zipfile.ZipFile(sys.argv[1],"a").write(sys.argv[2],sys.argv[3])' test.zip /etc/issue otherdir/issue
~/wrk/tmp$ ls -l test.zip
-rw-r--r-- 1 yeti yeti 605 Mar 23 09:52 test.zip
(yeti@aurora:1)~/wrk/tmp$ unzip -lv test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
357 Stored 357 0% 2014-03-20 15:47 ff1b8b7f text/motd
28 Stored 28 0% 2012-09-21 22:52 f9c3990c otherdir/issue
-------- ------- --- -------
385 385 0% 2 files
~/wrk/tmp$ unzip -p test.zip otherdir/issue Debian GNU/Linux 6.0 \n \l
~/wrk/tmp$ _
Best Answer
[ source: https://linux.die.net/man/1/unzip ]