-
Notifications
You must be signed in to change notification settings - Fork 3
/
_Library.zip.class.xml
1140 lines (1049 loc) · 31.9 KB
/
_Library.zip.class.xml
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="%Library.zip">
<Description><![CDATA[
Class to create and extract zip<br/>
https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT<br/>
https://www.zlib.net/manual.htm<br/>
win 32 bit date time format https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx<br/>
source https://github.com/nlzv/isc_zip<br/>
<br/>
example how to zip
<EXAMPLE>
set zip=##class(%zip).%New()
set zip.SourceDir="/mnt/tmp/zip/testpack/"
set zip.files(1)="test1.txt"
set zip.files(2)="test2.txt"
set zip.packfile="/mnt/tmp/zip/testpack/test.zip"
do zip.pack()
</EXAMPLE>
example how to unzip
<EXAMPLE>
set zip=##class(%zip).open("/mnt/tmp/zip/testunpack/test.zip")
set zip.TargetDir="/mnt/tmp/zip/testunpack/"
do zip.unpack()
</EXAMPLE>
limitations<br/>
on unicode cache kernel only 8bits file names (i.e. in latin)<br/>]]></Description>
<Super>%RegisteredObject</Super>
<TimeCreated>64918,52465.852234</TimeCreated>
<Property name="files">
<Description><![CDATA[
files LFH (list file header) array<br/>
also used as files to add in zip arhive<br/>]]></Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="idxlfhfiles">
<Description>
relative file path to LFH index map </Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="cdrfiles">
<Description><![CDATA[
files CDR (common diretory record) array<br/>]]></Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="idxcdrfiles">
<Description>
relative file path to CDR index map </Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="fcomments">
<Description>
file comments array, LFH index</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="LFHo">
<Description>
offsets of LFHs</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="LFHbin">
<Description>
binary images of LFHs</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="CDRo">
<Description>
CDRs (common directory name) offsets</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="CDRbin">
<Description>
CDRs binary images</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="DDbin">
<Description>
data descriptor</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="packfile">
<Description>
the name of zip/pack file</Description>
<Type>%String</Type>
</Property>
<Property name="lfhlen">
<Description>
length of static part of list file header</Description>
<Type>%Integer</Type>
</Property>
<Property name="eocdr64len">
<Type>%Integer</Type>
</Property>
<Property name="eocdl64len">
<Type>%Integer</Type>
</Property>
<Property name="cdrlen">
<Description>
length of static part of common directory record</Description>
<Type>%Integer</Type>
</Property>
<Property name="gzipminhdrlen">
<Type>%Integer</Type>
</Property>
<Property name="lfhoffset">
<Description>
auxilary var to track location of LFH in packfile</Description>
<Type>%Integer</Type>
</Property>
<Property name="error">
<Type>%String</Type>
</Property>
<Property name="ZipComment">
<Description>
zip comment</Description>
<Type>%String</Type>
</Property>
<Property name="SourceDir">
<Description>
source directory
add file from this directory
it becomes current during pack()</Description>
<Type>%String</Type>
</Property>
<Property name="TargetDir">
<Description>
directory to unpack files</Description>
<Type>%String</Type>
</Property>
<Property name="TargetSubDirs">
<Description>
check wether Target sub directory is created</Description>
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="use">
<Description><![CDATA[
use = 0 existed archive, unpack<br/>
use = 1 create archive, pack, add file to arhive<br/>]]></Description>
<Type>%Boolean</Type>
</Property>
<Property name="zip64">
<Description><![CDATA[
need use zip64 extension due to some component larger then 2**32-1 bytes<br/>]]></Description>
<Type>%Boolean</Type>
</Property>
<Property name="forcezip64">
<Description><![CDATA[
1 use zip64 extra extension even file size less than 2**32-1<br/>
0 use zip64 extra extension if file size more than 2**32-1<br/>]]></Description>
<Type>%Boolean</Type>
</Property>
<Property name="ZE">
<Description>
$ZERROR saved</Description>
<Type>%String</Type>
</Property>
<Property name="endian">
<Description>
0 little endian platform
1 big endian platform</Description>
<Type>%Boolean</Type>
</Property>
<Property name="time">
<Description>
time of the last pack/unpack function</Description>
<Type>%String</Type>
</Property>
<Property name="rsize">
<Description><![CDATA[
size during pack/unpack functions<br/>
in future it will be rsize ]]></Description>
<Type>%String</Type>
</Property>
<Property name="wsize">
<Description><![CDATA[
writing size during pack/unpack functions<br/>
not yet calculated]]></Description>
<Type>%String</Type>
</Property>
<Property name="unpackErrors">
<Type>%String</Type>
<MultiDimensional>1</MultiDimensional>
</Property>
<Method name="open">
<Description><![CDATA[
open existed zip archive<br/>
return %zip object<br/>
files property is populated by relative filenames in archive<br/>]]></Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>zipfile</FormalSpec>
<Implementation><![CDATA[
set eofold=$SYSTEM.Process.SetZEOF(1)
set zip=..%New()
try { close zipfile } catch {} open zipfile:("RUK\RAW\"):1
if $test='1 set zip.error="open file" quit zip
set lfhsig=..lfhsig()
set cdrsig=..cdrsig()
set eocdrsig=..eocdrsig()
set zip64eocdrsig=..zip64eocdrsig()
set zip64eocdlsig=..zip64eocdlsig()
set lfhlen=..structlen(..lfhmeta())
set cdrlen=..structlen(..cdrmeta())
set eocdrlen=..structlen(..eocdrmeta())
use zipfile
set foffset=0
set ilfh=0 set icdr=0
set fnlen=0,ziplen=0,extralen=0
set err=0
set maxsize=4294967295
#;B ; open
read bin#4 set foffset=foffset+4
if $E(bin,$L(bin)-3,$L(bin))'=lfhsig { for { read x#1 if $zeof=-1 { set err=1 quit } if foffset>512 { set err=2 quit } set bin=bin_x set foffset=foffset+1 if $E(bin,$L(bin)-3,$L(bin))=lfhsig || ($E(bin,$L(bin)-3,$L(bin))=cdrsig) { quit } }}
if err>0 set zip.error=err quit zip
#;B ; open start zip
for {
if bin=lfhsig {
#;B ;LFH start
set lfhbin=bin read bin#(lfhlen-4) set lfhbin=lfhbin_bin set ilfh=ilfh+1
set zip.LFHo(ilfh)=foffset-4
do ..getLFH(lfhbin,.sig,.version,.flag,.method,.time,.date,.crc,.zipsize,.size,.fnlen,.extralen)
if fnlen>0 { read fname#fnlen set zip.files(ilfh)=fname } else { set fname="" }
if extralen>0 { read extra#extralen } else { set extra="" }
#; data descriptor
if $zb(flag,8,1) {
B ;data descriptor not implemented
}
set lfhbin=lfhbin_fname_extra
if size=maxsize || (zipsize=maxsize) do zip.LFHgetFileSizes(.lfhbin,.size,.zipsize,.zip64)
set zip.LFHbin(ilfh)=lfhbin
set foffset=foffset+lfhlen-4+fnlen+extralen+zipsize
use zipfile:foffset
#;B ;LFH end
}
#; file data 1
#; data descrriptor 1
#; common directory record
elseif bin=cdrsig {
#;B ; CDR start
set cdrbin=bin read bin#(cdrlen-4) set icdr=icdr+1 set cdrbin=cdrbin_bin
set zip.CDRo(icdr)=foffset-4
do ..getCDH(cdrbin, .sig, .madever, .extrver, .flag, .meth, .ftime, .fdate, .crc, .zipsize, .size, .fnlen, .extralen, .fclen, .dns, .ifa, .efa, .rolh)
set foffset=foffset+cdrlen-4
if fnlen>0 { read fname#fnlen set zip.cdrfiles(icdr)=fname } else { set fname="" }
if extralen>0 { read extra#extralen } else { set extra=""}
if fclen>0 { read fcomment#fclen set zip.fcomment(icdr)=fcomment } else { set fcomment="" }
set zip.CDRbin(icdr)=cdrbin_fname_extra_fcomment
set foffset=foffset+fnlen+extralen+fclen
use zipfile:foffset
#;B ; CDR end
}
#; zip64eocdr
elseif bin=zip64eocdrsig {
#;B ; zip64eocdrsig start
read eocdr64bin#(zip.eocdr64len-4)
set eocdr64bin=bin_eocdr64bin
set len=$ZQA(eocdr64bin,5)
set len=len-zip.eocdr64len+12
if len'=0 read extbin#len
set foffset=foffset+zip.eocdr64len+len
#;B ; zip64eocdrsig end
}
#; zip64eocdlsig
elseif bin=zip64eocdlsig {
#;B ; zip64eocdlsig start
read eocdl64bin#(zip.eocdl64len-4)
set eocdl64bin=bin_eocdl64bin
set foffset=foffset+zip.eocdl64len
#;B ; zip64eocdlsig end
}
#; end of central directory record
elseif bin=eocdrsig {
#;B ; end of central directory start
set eocdrbin=bin read bin#eocdrlen-4 set eocdrbin=eocdrbin_bin
do ..getEOCDR(eocdrbin,.sig,.zipcommentlen)
if zipcommentlen>0 read v#zipcommentlen set zip.ZipComment=v
#;B ; end of central directory end
quit
}
read bin#4 set foffset=foffset+4
}
use 0 close zipfile
set zip.packfile=zipfile
do zip.BuildFileIndex()
if eofold=0 do $SYSTEM.Process.SetZEOF(0)
quit zip
]]></Implementation>
</Method>
<Method name="closepack">
<Description>
close pack file, i.e. create common directories records</Description>
<Implementation><![CDATA[
#; write CDRs
#;B ; start add CDRs
set cdroffset=..lfhoffset
set oeocdr=cdroffset
set zip64=0
set len=0
set i="" for {
set i=$order(..LFHbin(i),1,binlfh) quit:i=""
do ..mkCDRfromLFH(.bincdr,binlfh,..LFHo(i),,,,.zip64)
#;B ; mkdCDRfromLFH done
if zip64=1 { set ..zip64=1 }
set len=len+$L(bincdr)
W bincdr
}
#; write end of CDR
#;B ; write end of CDR
set n=$order(..files(""),-1)
if ..zip64=1 {
do ..mkEOCDR64(.bineocdr64,cdroffset,len,n)
W bineocdr64
do ..mkEOCDL64(.bineocdl64,cdroffset+len)
W bineocdl64
do ..mkEOCDR(.bineocdr,cdroffset#4294967296,len#4294967296,n)
#;do ..mkEOCDR(.bineocdr,4294967295,4294967295,n)
#;do ..mkEOCDR(.bineocdr,0,0,n)
W bineocdr
#;B ; eocdr64
}
else {
do ..mkEOCDR(.bineocdr,cdroffset,len,n)
W bineocdr
}
]]></Implementation>
</Method>
<Method name="pack">
<Description><![CDATA[
create zip archive with list of files in files property<br/>
SourceDir relative<br>
packfile argument override packfile property<br/>]]></Description>
<FormalSpec>packfile=""</FormalSpec>
<Implementation><![CDATA[
set ..time=$zh
set ..rsize=0
set ..zip64=0
if packfile="" { set packfile=..packfile }
else { set ..packfile=packfile }
try { close packfile } catch {} open packfile:("NWUK\RAW\")
use packfile
set dir=$SYSTEM.Process.CurrentDirectory(..SourceDir)
set ..lfhoffset=0
set i="" for { set i=$order(..files(i),1,file) quit:i="" do ..packbyid(i) }
use packfile:..lfhoffset
do ..closepack()
close packfile
do $SYSTEM.Process.CurrentDirectory(dir)
set ..time=$zh-..time
]]></Implementation>
</Method>
<Method name="packbyid">
<Description>
add one file to packfile</Description>
<FormalSpec>id</FormalSpec>
<Implementation><![CDATA[
#;B ; start pack add file by id
set file=..files(id)
set packfile=..packfile
set gzipminhdrlen=..gzipminhdrlen
set lfhoffset=..lfhoffset
use packfile:lfhoffset
set size=##class(%File).GetFileSize(file)
set ..rsize=..rsize+size
set hlocalmoddattime=$zu(140,2,file,0)
set tsodbc=$zdt(hlocalmoddattime,3)
set date=..dosDateYMDtoBin($E(tsodbc,1,4),$E(tsodbc,6,7),$E(tsodbc,9,10))
set time=..dosTimeHMStoBin($E(tsodbc,12,13),$E(tsodbc,15,16),$E(tsodbc,18,19))
set maxsize=2**32-1
if size>maxsize || ..forcezip64 { set zip64=1 }
else { set zip64=0 }
#; version 2.0 low order byte
set version=$C(20,0)
set flag=$C(0,0)
#; method deflate
set method=$C(8,0)
#;B ; before make lfh
if zip64=1 {
set extraZip64=$C(1,0)_$ZWC(16)_$ZQC(size)_$ZQC(0)
#if 0
#; data descriptor
set flag=$ZWC(8)
#; datadesc
set datadesc=$C(0,0,0,0)_$ZQC(zipsize)_$ZQC(size)
#;set datadesc= $$datadescsig()_datadesc
set ..DDbin(i)=datadesc
#endif
}
if zip64 { do ..mkLFH(.binlfh,version,flag,method,time,date,/*crc*/, maxsize, maxsize)}
else { do ..mkLFH(.binlfh,version,flag,method,time,date,/*crc*/,/*zipsize*/, size) }
#;B ; after make lfh
do ..LFHsetFile(.binlfh,file)
if zip64 { do ..LFHsetExtra(.binlfh,extraZip64) }
set ..LFHbin(id)=binlfh
use packfile:..lfhoffset
W binlfh
set rawoffset=..lfhoffset+..lfhFullLen(binlfh)-gzipminhdrlen
use packfile:rawoffset
use packfile:(/GZIP=1)
try { close file } catch {} open file:("RUK\RAW\")
set len=0
#;B ; zip
for {
use file read bin
use packfile write bin
set len=len+$L(bin)
if len'<size quit
}
close file
close packfile
set offset=##class(%File).GetFileSize(packfile)-8
open packfile:("RWUK\RAW\")
use packfile:(/GZIP=0)
use packfile:offset
#;B ; read gzip tailor
read crc#4 read isize#4
set zipsize=offset-..lfhoffset-..lfhFullLen(binlfh)
#;B ;zipsize is calculated
do ..LFHsetCRC(.binlfh,crc)
do ..LFHsetZipSize(.binlfh,zipsize)
use packfile:..lfhoffset
W binlfh
set ..LFHbin(id)=binlfh
set ..LFHo(id)=..lfhoffset
set ..lfhoffset=offset
#;B ; pack add file by id end
if zip64 { set ..zip64=1 }
]]></Implementation>
</Method>
<Method name="unpack">
<Description>
unzip archive</Description>
<FormalSpec>zipfile="",targetdir=""</FormalSpec>
<Implementation><![CDATA[
if targetdir'="" { set ..TargetDir=targetdir }
else { set targetdir=..TargetDir }
set curdir=$SYSTEM.Process.CurrentDirectory(targetdir)
kill ..TargetSubDirs
set idlast=$order(..files(""),-1)
for id=1:1:idlast { do ..unpackbyid(id,targetdir) }
do $SYSTEM.Process.CurrentDirectory(curdir)
]]></Implementation>
</Method>
<Method name="unpackSel">
<Description>
unpack only selected files</Description>
<FormalSpec>zipfile="",targetdir=""</FormalSpec>
</Method>
<Method name="unpackfile">
<Description>
unpack just one file</Description>
<FormalSpec>file</FormalSpec>
</Method>
<Method name="unpackbyid">
<FormalSpec>id,targetdir</FormalSpec>
<Implementation><![CDATA[
set $ztrap="error"
set ..ZE=""
set io=$io
set maxsize=4294967295
set zip64=0
set restorepack=0
set targetfile=""
set packfile=..packfile
set lfhbin=..LFHbin(id)
set filename=..files(id)
set offset=..LFHo(id)
close packfile
open packfile:("RWUK\RAW\") use packfile
do ..getLFH(..LFHbin(id),,.ver,.flag,.meth,.time,.date,.crc,.zipsize,.size,.fnlen,.extralen)
#;B ; unpackbyid.getLFH
if size=maxsize || (zipsize=maxsize ) { set zip64=1 }
if zip64=1 { do ..LFHgetFileSizes(..LFHbin(id),.size,.zipsize) }
set oraw=offset+$L(..LFHbin(id))
set gzipminhdr=..gzipminhdr()
set gziphdrlen=$L(gzipminhdr)
set ogziphdr=oraw-gziphdrlen
#;B ; gziphdr end
set ogziptlr=offset+..structlen(..lfhmeta())+fnlen+extralen+zipsize
set gziptlr=..gziptlr(crc,size)
set gziptlrlen=$L(gziptlr)
set restorepack=1
#; now put in file gziphdr
use packfile:ogziphdr
read savedgziphdr#gziphdrlen
use packfile:ogziphdr
write gzipminhdr
#; now put in file gziptlr
use packfile:ogziptlr
read savedgziptlr#gziptlrlen
use packfile:ogziptlr
write gziptlr
close packfile
set targetfile=targetdir_filename
#; check directory of file and create it if it is missed
set dir=##class(%File).GetDirectory(targetfile)
if '$D(..TargetSubDirs(dir)) {
if '##class(%File).DirectoryExists(dir) { set ret=##class(%File).CreateDirectoryChain(dir) }
set ..TargetSubDirs(dir)=""
}
#;B ;copy
open packfile:("RUK\RAW\")
use packfile:ogziphdr
use packfile:(/GZIP=1)
open targetfile:("NWUK\RAW\")
set len=0
set buflen=65536
#;B ;loop
for {
use packfile
if len+buflen<size { read bin#buflen }
else { read bin#(size-len)}
set len=len+$L(bin)
use targetfile write bin
if len'<size quit
}
exit
use io
try { close packfile } catch {}
try { close targetfile } catch {}
#; restore updates gzip header and tailer back
if restorepack {
open packfile:("WUK\RAW\")
if $data(savedgziphdr) {
use packfile:ogziphdr
write savedgziphdr
}
if $data(savedgziptlr) {
use packfile:ogziptlr
write savedgziptlr
}
use io close packfile
}
quit
error
set $ztrap=""
set ..ZE=$ZE
goto exit
]]></Implementation>
</Method>
<Method name="getLFH">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,magic,version,flag,method,time,date,crc,zipsize,size,fnlen,extralen</FormalSpec>
<Implementation><![CDATA[
set magic=$E(bin,1,4)
set version=$E(bin,5,6)
set flag=$E(bin,7,8)
set method=$E(bin,9,10)
set time=$E(bin,11,12)
set date=$E(bin,13,14)
set crc=$E(bin,15,18)
set zipsize=$ZLA($E(bin,19,22))
set size=$ZLA($E(bin,23,26))
set fnlen=$ZWA($E(bin,27,28))
set extralen=$ZWA($E(bin,29,30))
quit
]]></Implementation>
</Method>
<Method name="LFHgetLengths">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,fnlen,extralen</FormalSpec>
<Implementation><![CDATA[
set fnlen=$ZWA($E(bin,27,28))
set extralen=$ZWA($E(bin,29,30))
]]></Implementation>
</Method>
<Method name="lfhFullLen">
<FormalSpec>bin</FormalSpec>
<Implementation><![CDATA[
do ..LFHgetLengths(.bin,.fnlen,.extralen)
quit ..lfhlen+fnlen+extralen
]]></Implementation>
</Method>
<Method name="mkLFH">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,version="",flag="",method="",time="",date="",crc="",packsize="",size="",fnlen="",extralen=""</FormalSpec>
<Implementation><![CDATA[
set lfhlen=..lfhlen()
if $L($G(bin))<lfhlen set bin=$TR($J("",lfhlen)," ",$C(0))
set $E(bin,1,4)=..lfhsig()
#; default 2.0
if version'="" set $E(bin,5,6)=version
if flag'="" set $E(bin,7,8)=flag
if method'="" set $E(bin,9,10)=method
if time'="" && ($L(time)=2) set $E(bin,11,12)=time
if date'="" && ($L(date)=2) set $E(bin,13,14)=date
if packsize'="" set $E(bin,19,22)=$ZLC(packsize)
if size'="" set $E(bin,23,26)=$ZLC(size)
if fnlen'="" set $E(bin,27,28)=$ZWC(fnlen)
if extralen'="" set $E(bin,29,30)=$ZWC(extralen)
]]></Implementation>
</Method>
<Method name="LFHsetFile">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,file</FormalSpec>
<Implementation><![CDATA[
do ..getLFH(.bin,.fnlenold,.extralen)
set lfhlen=..structlen(..lfhmeta())
set binhead=$E(bin,1,lfhlen)
set fnold=$E(bin,lfhlen+1,lfhlen+fnlenold)
set binextra=$E(bin,lfhlen+fnlenold+1,lfhlen+fnlenold+extralen)
set $E(binhead,27,28)=$ZWC($L(file))
set bin=binhead_file_binextra
]]></Implementation>
</Method>
<Method name="LFHsetCRC">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,crc</FormalSpec>
<Implementation><![CDATA[ set $E(bin,15,18)=crc
]]></Implementation>
</Method>
<Method name="LFHsetZipSize">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,zipsize</FormalSpec>
<Implementation><![CDATA[
set lfhlen=..lfhlen()
set fnlen=$ZWA($E(bin,27,28))
set zipsizeold=$E(bin,19,22)
if zipsizeold=$C(255,255,255,255) {
set o=lfhlen+fnlen
set extrahid=$E(bin,o+1,o+2)
#; expect to be 1st
if extrahid=$C(1,0) {
set $E(bin,lfhlen+fnlen+##expression(1+4+8),lfhlen+fnlen+##expression(1+4+8+8))=$ZQC(zipsize)
}
#; this is error
else {}
}
else {
set $E(bin,19,22)=$ZLC(zipsize)
}
]]></Implementation>
</Method>
<Method name="LFHgetFileSizes">
<FormalSpec>bin,size,zipsize,zip64</FormalSpec>
<Implementation><![CDATA[
set lfhlen=..lfhlen
set zip64id=$C(1,0)
set maxsize=$C(255,255,255,255)
set fnlen=$ZWA(bin,27)
set extrasize=$ZWA(bin,29)
set size=$E(bin,19,22)
set zipsize=$E(bin,23,26)
if size'=maxsize || (zipsize'=maxsize) { set size=$ZLA(size) set zipsize=$ZLA(zipsize) quit }
set extra=$E(bin,lfhlen+fnlen+1,lfhlen+fnlen+extrasize)
set o=0 for {
set extrahid=$E(extra,o+1,o+2)
set extrahlen=$ZWA(extra,o+3)
if extrahid=zip64id quit
set o=o+4+extrahlen
if o'< extrasize quit
}
if extrahid=zip64id {
set size=$ZQA(extra,o+##expression(1+4))
set zipsize=$ZQA(extra,o+##expression(1+4+8))
}
else { set size="" set zipsize="" }
]]></Implementation>
</Method>
<Method name="LFHsetExtra">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,extra</FormalSpec>
<Implementation><![CDATA[
set extralen=$L(extra)
set lfhlen=30
do ..LFHgetLengths(.bin,.fnlen,.extrlenold)
set $E(bin,29,30)=$ZWC(extralen)
set bin=$E(bin,1,lfhlen+fnlen)_extra
]]></Implementation>
</Method>
<Method name="getLFSrawstream">
<FormalSpec>id</FormalSpec>
<Implementation><![CDATA[
set foffset=..LFHo(id)
do ..getLFH(..LFHbin(id),,.ver,.flag,.meth,.time,.date,.crc,.zipsize,.size,.fnlen,.extralen)
quit foffset+..structlen(..lfhmeta())+fnlen+extralen
]]></Implementation>
</Method>
<Method name="getCDH">
<Description>
parse Central Directory Header</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>bin,sig,madever,extrver,flag,meth,ftime,fdate,crc,zipsize,size,fnlen,extralen,fclen,dns,ifa,efa,rolh</FormalSpec>
<Implementation><![CDATA[
set sig=$E(bin,1,4)
set madever=$ZWA(bin,5)
set extrver=$ZWA(bin,7)
set flag=$E(bin,9,10)
set meth=$E(bin,11,12)
set ftime=$E(bin,13,14)
set fdate=$E(bin,15,16)
set crc=$E(bin,17,20)
set zipsize=$ZLA($E(bin,21,24))
set size=$ZLA($E(bin,25,28))
set fnlen=$ZWA($E(bin,29,30))
set extralen=$ZWA($E(bin,31,32))
set fclen=$ZWA($E(bin,33,34))
set dns=$ZWA($E(bin,35,36))
set ifa=$E(bin,37,38)
set efa=$E(bin,39,42)
set rolh=$E(bin,43,46)
quit
]]></Implementation>
</Method>
<Method name="mkCDRfromLFH">
<ClassMethod>1</ClassMethod>
<FormalSpec>cdrbin,lfhbin,lfho="",ifa="",efa="",fcomment="",zip64</FormalSpec>
<Implementation><![CDATA[
set maxsize=4294967295
set maxsizebin=$C(255,255,255,255)
set lfhlen=..structlen(..lfhmeta())
set cdrlen=..structlen(..cdrmeta())
set cdrbin=$TR($J("",cdrlen)," ",$C(0))
set $E(cdrbin,1,4)=..cdrsig()
set $E(cdrbin,5,6)=$E(lfhbin,5,6)
set $E(cdrbin,7,8)=$E(lfhbin,5,6)
#; flag
set $E(cdrbin,9,10)=$E(lfhbin,7,8)
#; method
set $E(cdrbin,11,12)=$E(lfhbin,9,10)
#; time
set $E(cdrbin,13,14)=$E(lfhbin,11,12)
#; date
set $E(cdrbin,15,16)=$E(lfhbin,13,14)
#; crc
set $E(cdrbin,17,20)=$E(lfhbin,15,18)
#; compressed size
set zipsize=$E(lfhbin,19,22)
set $E(cdrbin,21,24)=zipsize
if zipsize=maxsizebin { set zip64=1 }
else { set zip64=0 }
#; uncompressed size
set size=$E(lfhbin,23,26)
if size=maxsizebin ||(zip64=1) { set zip64=1 }
else { set zip64=0 }
set $E(cdrbin,25,28)=$E(lfhbin,23,26)
#; filename len
set fnlen=$E(lfhbin,27,28)
set $E(cdrbin,29,30)=fnlen
set fnlen=$ZWA(fnlen)
#; extra len
set extralen=$E(lfhbin,29,30)
set $E(cdrbin,31,32)=extralen
set extralen=$ZWA(extralen)
if lfho'="" {
if lfho>maxsize { set zip64=1 }
else { set $E(cdrbin,43,46)=$ZLC(lfho) }
}
#; fcomment
if fcomment'="" { set $E(bin,33,34)=$ZWC($L(fcomment)) }
if zip64 {
set extra=$E(lfhbin,lfhlen+fnlen+1,lfhlen+fnlen+extralen)
if extra="" {
set extra=$C(1,0)_$C(16,0)_$ZQC(size)_$ZQC(zipsize)
}
set $E(cdrbin,21,24)=maxsizebin
set $E(cdrbin,25,28)=maxsizebin
set $E(cdrbin,43,46)=maxsizebin
set extra=$E(extra,1,2)_$ZWC($ZWA(extra,3)+8)_$E(extra,##expression(4+1),##expression(4+16))_$ZQC(lfho)
set cdrbin=cdrbin_$E(lfhbin,lfhlen+1,lfhlen+fnlen)_extra_fcomment
set $E(cdrbin,31,32)=$ZWC($L(extra))
}
else {
set cdrbin=cdrbin_$E(lfhbin,lfhlen+1,lfhlen+fnlen)_$E(lfhbin,lfhlen+fnlen+1,lfhlen+fnlen+extralen)_fcomment
}
]]></Implementation>
</Method>
<Method name="dumpCDR">
<ClassMethod>1</ClassMethod>
<FormalSpec>bin</FormalSpec>
<Implementation><![CDATA[
do ..getCDH(bin, .sig, .madever, .extrver, .flag, .meth, .ftime, .fdate, .crc, .zipsize, .size, .fnlen, .extralen, .fclen, .dns, .ifa, .efa, .rolh)
W "file comment length",fclen,!
quit
]]></Implementation>
</Method>
<Method name="lfhsig">
<Description>
list file header signature</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[ quit $zlc($zhex("04034b50"))
]]></Implementation>
</Method>
<Method name="cdrsig">
<Description>
common directory record signature</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[ quit $zlc($zhex("02014b50"))
]]></Implementation>
</Method>
<Method name="eocdrsig">
<Description>
end of current directory record signature</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[ quit $zlc($zhex("06054b50"))
]]></Implementation>
</Method>
<Method name="zip64eocdrsig">
<Description>
zip64 end of current directory record signature</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[ quit $zlc($zhex("06064b50"))
]]></Implementation>
</Method>
<Method name="zip64eocdlsig">
<Description>
zip64 end of current directory locator signature</Description>
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[ quit $zlc($zhex("07064b50"))
]]></Implementation>
</Method>
<Method name="lfhlen">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
set LFHs=..lfhmeta()
set n=$LL(LFHs)
set len=0 for i=1:1:n set len=len+$LI($LI(LFHs,i),2)
quit len
]]></Implementation>
</Method>
<Method name="structlen">
<Description>
calculate length of struct basing on metainfo</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>meta</FormalSpec>
<Implementation><![CDATA[ set n=$LL(meta) set len=0 for i=1:1:n { set len=len+$LI($LI(meta,i),2) } quit len
]]></Implementation>
</Method>
<Method name="lfhmeta">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
quit $LB(
$LB("local file header signature",4),
$LB("version needed to extract",2),
$LB("general purpose bit flag",2),
$LB("compression method",2),
$LB("last mod file time",2),
$LB("last mod file date",2),
$LB("crc-32",4),
$LB("compressed size",4),
$LB("uncompressed size",4),
$LB("file name length",2),
$LB("extra field length",2)
)
]]></Implementation>
</Method>
<Method name="cdrmeta">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
quit $LB(
$LB("central file header signature",4),
$LB("version madeby",2),
$LB("version needed to extract",2),
$LB("general purpose bit flag",2),
$LB("compression method",2),
$LB("last mod file time",2),
$LB("last mod file date",2),
$LB("crc-32",4),
$LB("compressed size",4),
$LB("uncompressed size",4),
$LB("file name length",2),
$LB("extra field length",2),
$LB("file comment length",2),
$LB("disk number start",2),
$LB("internal file attributes",2),
$LB("external file attributes",4),
$LB("relative offset of local header",4)
)
]]></Implementation>
</Method>
<Method name="eocdrmeta">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
quit $LB(
$LB("end of central dir signature",4),
$LB("number of this disk",2),
$LB("number of the disk with the start of the central directory",2),
$LB("total number of entries in the central directory on this disk",2),
$LB("total number of entries in the central directory",2),
$LB("size of the central directory",4),
$LB("offset of start of central directory with respect to the starting disk number",4),
$LB("zip file comment length",2)
)
]]></Implementation>
</Method>
<Method name="eocdr64meta">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
quit $LB(
$LB("end of central dir signature",4),
$LB("size of zip64 end of central directory record",8),
$LB("version made by",2),
$LB("version needed to extract",2),
$LB("number of this disk",4),
$LB("number of the disk with the start of the central directory",4),
$LB("total number of entries in the central directory on this disk",8),
$LB("total number of entries in the central directory",8),
$LB("size of the central directory",8),
$LB("offset of start of central directory with respect to the starting disk number",8)
)
]]></Implementation>
</Method>
<Method name="eocdl64meta">
<ClassMethod>1</ClassMethod>
<Implementation><![CDATA[
quit $LB(
$LB("zip64 end of central dir locator signature",4),
$LB("number of the disk with the start of the zip64 end of central directory",4),
$LB("relative offset of the zip64 end of central directory record",8),
$LB("total number of disks",4)
)
]]></Implementation>