-
Notifications
You must be signed in to change notification settings - Fork 3
/
_zipclose.prg
297 lines (200 loc) · 9.28 KB
/
_zipclose.prg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
*!* _zipclose
*!* Overall .ZIP file format:
*!* ------------------------------------------------------------------------------------------------------
*!* [local file header 1]
*!* [encryption header 1]
*!* [file data 1]
*!* [data descriptor 1]
*!* .
*!* .
*!* .
*!* [local file header n]
*!* [encryption header n]
*!* [file data n]
*!* [data descriptor n]
*!* [archive decryption header]
*!* [archive extra data record]
*!* [central directory header 1]
*!* .
*!* .
*!* .
*!* [central directory header n]
*!* [zip64 end of central directory record]
*!* [zip64 end of central directory locator]
*!* [end of central directory record]
*!* ------------------------------------------------------------------------------------------------------
*!* Local file header:
*!* ------------------------------------------------------------------------------------------------------
*!* local file header signature 4 bytes (0x04034b50)
*!* version needed to extract 2 bytes
*!* general purpose bit flag 2 bytes
*!* compression method 2 bytes
*!* last mod file time 2 bytes
*!* last mod file date 2 bytes
*!* crc - 32 4 bytes
*!* compressed size 4 bytes
*!* uncompressed size 4 bytes
*!* file name length 2 bytes
*!* extra field length 2 bytes
*!* file name variable size
*!* extra field variable size
*!* ------------------------------------------------------------------------------------------------------
*!* Data descriptor:
*!* ------------------------------------------------------------------------------------------------------
*!* crc - 32 4 bytes
*!* compressed size 4 bytes
*!* uncompressed size 4 bytes
*!* ------------------------------------------------------------------------------------------------------
*!* Central Directory Header:
*!* ------------------------------------------------------------------------------------------------------
*!* central file header signature 4 bytes (0x02014b50)
*!* version made by 2 bytes
*!* version needed to extract 2 bytes
*!* general purpose bit flag 2 bytes
*!* compression method 2 bytes
*!* last mod file time 2 bytes
*!* last mod file date 2 bytes
*!* crc - 32 4 bytes
*!* compressed size 4 bytes
*!* uncompressed size 4 bytes
*!* file name length 2 bytes
*!* extra field length 2 bytes
*!* file comment length 2 bytes
*!* disk number start 2 bytes
*!* internal file attributes 2 bytes
*!* external file attributes 4 bytes
*!* relative offset of local header 4 bytes
*!* file name variable size
*!* extra field variable size
*!* file comment variable size
*!* ------------------------------------------------------------------------------------------------------
*!* End of central directory record:
*!* ------------------------------------------------------------------------------------------------------
*!* end of central dir signature 4 bytes (0x06054b50)
*!* number of this disk 2 bytes
*!* number of the disk with the start of the central directory 2 bytes
*!* total number of entries in the central directory on this disk 2 bytes
*!* total number of entries in the central directory 2 bytes
*!* size of the central directory 4 bytes
*!* offset of start of central directory with respect to the starting disk number 4 bytes
*!* .ZIP file comment length 2 bytes
*!* .ZIP file comment variable size
*!* ------------------------------------------------------------------------------------------------------
*!* version needed to extract
*!* ------------------------------------------------------------------------------------------------------
*!* The minimum supported ZIP specification version needed to extract the file. The value / 10 indicates the major
*!* version number, and the value mod 10 is the minor version number.
*!* 1.0 - 0h0A00 Default value
*!* 2.0 - 0h1400 File is compressed using Deflate compression
*!* 6.3 - 0h3F00 File is compressed using LZMA
*!* ------------------------------------------------------------------------------------------------------
*!* external file attributes LINUX (Right 2 bytes. Left 2 bytes are for MSDOS)
*!* ------------------------------------------------------------------------------------------------------
*!* 0x81E40000 for sh files
*!* 0x81A40000 for normal files
*!* 0x41ED0000 for folders
*!* ------------------------------------------------------------------------------------------------------
*!* https://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute
*!* ------------------------------------------------------------------------------------------------------
*!* THE FOLLOWING VALUES ARE OCTAL!!
*!*
*!* #define S_IFDIR 0040000 /* directory */
*!* #define S_IFREG 0100000 /* regular */
*!* PERMISSIONS: 755 folders, 644 other files, 744 sh files
#define EFA_DOS_FOLDER 0x00000010
#define EFA_DOS_FILE 0x00000020
#define EFA_UNIX_FOLDER 0x41ed0000 && octal 040000 + 0755 << 16
#define EFA_UNIX_FILE 0x81a40000 && octal 0100000 + 0644 << 16
#define EFA_UNIX_FILE_SH 0x81e40000 && octal 0100000 + 0744 << 16
#define EFA_FOLDER (EFA_DOS_FOLDER + EFA_UNIX_FOLDER)
#define EFA_FILE (EFA_DOS_FILE + EFA_UNIX_FILE)
#define EFA_FILE_SH (EFA_DOS_FILE + EFA_UNIX_FILE_SH)
lparameters pzipfilename, pzipfilecomment, puselzma
local ozip as 'zip' of '_zip.vcx'
local fcreationtime, flastaccesstime, flastwritetime, hfile, lfhlastmodfiledatetime, narea
local ntfslastmodfiledatetime
m.narea = select()
select * from 'zip_temp' order by zip_temp.zfilepath into cursor 'zip_cursor' readwrite
use in 'zip_temp'
select 'zip_cursor'
m.hfile = fcreate(m.pzipfilename, 0)
if m.hfile < 0 then
error 'FCREATE ' + m.pzipfilename
endif
m.ozip = newobject('zip', '_zip.vcx')
m.ozip.zipuselzma = m.puselzma
m.ozip.eocdsizeofthecentraldirectory = 0
m.lfhlastmodfiledatetime = datetime()
m.ntfslastmodfiledatetime = _zugetsystemtimeasfiletime()
scan
wait 'ZIPPING FILES...' window nowait
m.ozip.cfhfilecomment = zip_cursor.filecmnt
if not empty(zip_cursor.filetime)
m.ozip.lfhlastmodfiledatetime = zip_cursor.filetime
else
if directory(zip_cursor.sfilepath) or file(zip_cursor.sfilepath)
m.fcreationtime = 0h00000000000000000000000000000000
m.flastaccesstime = 0h00000000000000000000000000000000
m.flastwritetime = 0h00000000000000000000000000000000
m.ozip.lfhlastmodfiledatetime = _zugetfiletimes(zip_cursor.sfilepath, @m.fcreationtime, @m.flastaccesstime, @m.flastwritetime )
m.ozip.ntfscreationtime = m.fcreationtime
m.ozip.ntfslastaccesstime = m.flastaccesstime
m.ozip.ntfslastwritetime = m.flastwritetime
else
m.ozip.lfhlastmodfiledatetime = m.lfhlastmodfiledatetime
m.ozip.ntfscreationtime = m.ntfslastmodfiledatetime
m.ozip.ntfslastaccesstime = m.ntfslastmodfiledatetime
m.ozip.ntfslastwritetime = m.ntfslastmodfiledatetime
endif
endif
if zip_cursor.isfile = 0
*!* FOLDER
m.ozip.lfhfilename = chrtran(addbs(zip_cursor.zfilepath), '\', '/')
m.ozip.lfhuncompressedbytes = ''
m.ozip.cfhexternalfileattributes = EFA_FOLDER
else
*!* FILE
m.ozip.lfhfilename = chrtran(zip_cursor.zfilepath, '\', '/')
if not file(zip_cursor.sfilepath) and empty(zip_cursor.blobdata)
error 'FILE NOT FOUND AND BLOBDATA IS EMPTY'
endif
if file(zip_cursor.sfilepath)
m.ozip.lfhuncompressedbytes = filetostr(zip_cursor.sfilepath)
else
m.ozip.lfhuncompressedbytes = zip_cursor.blobdata
endif
if lower(justext(zip_cursor.sfilepath)) == 'sh'
m.ozip.cfhexternalfileattributes = EFA_FILE_SH
else
m.ozip.cfhexternalfileattributes = EFA_FILE
endif
endif
*!* SAVE OFFSET OF LOCAL FILE HEADER FOR CENTRAL FILE HEADER
m.ozip.cfhrelativeoffsetoflocalheader = fseek(m.hfile, 0, 1)
*!* WRITE LOCAL FILE HEADER
fwrite(m.hfile, m.ozip.zipgetlocalfileheader())
*!* SAVE COMPRESSED FILE DATA
fwrite(m.hfile, m.ozip.lfhcompressedbytes)
*!* WRITE DATA DESCRIPTOR
fwrite(m.hfile, m.ozip.zipgetdatadescriptor())
m.ozip.eocdtotalnumber = m.ozip.eocdtotalnumber + 1
*!* SAVE CURRENT CENTRAL FILE HEADER RECORD TO CURSOR
replace zip_cursor.cfheader with m.ozip.zipgetcentralfileheader() in 'zip_cursor'
m.ozip.eocdsizeofthecentraldirectory = m.ozip.eocdsizeofthecentraldirectory + len(zip_cursor.cfheader)
endscan
wait clear
*!* SAVE OFFSET OF START OF CENTRAL DIRECTORY
m.ozip.eocdoffsetofstartofcentraldirectory = fseek(m.hfile, 0, 1)
*!* WRITE CENTRAL FILE HEADERS
scan
fwrite(m.hfile, zip_cursor.cfheader)
endscan
wait clear
*!* WRITE END OF CENTRAL DIRECTORY RECORD
if vartype(m.pzipfilecomment) $ 'CQ'
m.ozip.eocdzipfilecomment = m.pzipfilecomment
endif
fwrite(m.hfile, m.ozip.zipgeteocdrecord())
fclose(m.hfile)
use in 'zip_cursor'
_zuselect(m.narea)