Is there any way to use the shell command to find the offset of a file inside a zip without actually extracting the zip?
Have tried zipinfo XXX.zip which provides sizes which had to add up sizes to find the file.
Is there any better way to do it?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityTry running zipdetails on the file. [Full disclosure - I am the author of zipdetails]
In the example below, the offset for the file abc.txt
is listed on the PAYLOAD
line. The value in this case 41 hex
$ echo abc >abc.txt
$ zip test.zip abc.txt
adding: abc.txt (stored 0%)
$ zipdetails test.zip
0000 LOCAL HEADER #1 04034B50
0004 Extract Zip Spec 0A '1.0'
0005 Extract OS 00 'MS-DOS'
0006 General Purpose Flag 0000
0008 Compression Method 0000 'Stored'
000A Last Mod Time 52984C29 'Sat Apr 24 09:33:18 2021'
000E CRC 4788814E
0012 Compressed Length 00000004
0016 Uncompressed Length 00000004
001A Filename Length 0007
001C Extra Length 001C
001E Filename 'abc.txt'
0025 Extra ID #0001 5455 'UT: Extended Timestamp'
0027 Length 0009
0029 Flags '03 mod access'
002A Mod Time 6083D7CE 'Sat Apr 24 09:33:18 2021'
002E Access Time 6083D7CE 'Sat Apr 24 09:33:18 2021'
0032 Extra ID #0002 7875 'ux: Unix Extra Type 3'
0034 Length 000B
0036 Version 01
0037 UID Size 04
0038 UID 000003E8
003C GID Size 04
003D GID 000003E8
0041 PAYLOAD abc.
0045 CENTRAL HEADER #1 02014B50
0049 Created Zip Spec 1E '3.0'
004A Created OS 03 'Unix'
004B Extract Zip Spec 0A '1.0'
004C Extract OS 00 'MS-DOS'
004D General Purpose Flag 0000
004F Compression Method 0000 'Stored'
0051 Last Mod Time 52984C29 'Sat Apr 24 09:33:18 2021'
0055 CRC 4788814E
0059 Compressed Length 00000004
005D Uncompressed Length 00000004
0061 Filename Length 0007
0063 Extra Length 0018
0065 Comment Length 0000
0067 Disk Start 0000
0069 Int File Attributes 0001
[Bit 0] 1 Text Data
006B Ext File Attributes 81B40000
006F Local Header Offset 00000000
0073 Filename 'abc.txt'
007A Extra ID #0001 5455 'UT: Extended Timestamp'
007C Length 0005
007E Flags '03 mod access'
007F Mod Time 6083D7CE 'Sat Apr 24 09:33:18 2021'
0083 Extra ID #0002 7875 'ux: Unix Extra Type 3'
0085 Length 000B
0087 Version 01
0088 UID Size 04
0089 UID 000003E8
008D GID Size 04
008E GID 000003E8
0092 END CENTRAL HEADER 06054B50
0096 Number of this disk 0000
0098 Central Dir Disk no 0000
009A Entries in this disk 0001
009C Total Entries 0001
009E Size of Central Dir 0000004D
00A2 Offset to Central Dir 00000045
00A6 Comment Length 0000
Done
Dumping just the filneame & payload offset can be done as follows
$ zipdetails test.zip | perl -00 -ne 'print "$1,$2\n" if /^\S+\s+Filename\s+'"'"'(.*?)'"'"'.*?^(\S+?)\s+PAYLOAD/ms'
abc.txt,0041
There a pile of shell escaping going on in that, so here is the perl statement without escaping
print "$1,$2\n"
if /^\S+\s+Filename\s+'(.*?)'.*?^(\S+?)\s+PAYLOAD/ms;