summaryrefslogtreecommitdiff
path: root/packages/paszlib/src/zip.pas
blob: ca90e727ab0b9d34544a4e50a3ea60c4036f10ed (plain)
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
unit Zip;

{ zip.c -- IO on .zip files using zlib
  zip.h -- IO for compress .zip files using zlib
   Version 0.15 alpha, Mar 19th, 1998,

   Copyright (C) 1998 Gilles Vollant

   This package allows to create .ZIP file, compatible with PKZip 2.04g
     WinZip, InfoZip tools and compatible.
   Encryption and multi volume ZipFile (span) are not supported.
   Old compressions used by old PKZip 1.x are not supported

  For decompression of .zip files, look at unzip.pas

  Pascal tranlastion
  Copyright (C) 2000 by Jacques Nomssi Nzali
  For conditions of distribution and use, see copyright notice in readme.txt }


interface

{$ifdef WIN32}
  {$define Delphi}
{$endif}

uses
  //zutil,
  zbase,
  //zLib,
  ziputils;

const
  ZIP_OK    = (0);
  ZIP_ERRNO = (Z_ERRNO);
  ZIP_PARAMERROR = (-102);
  ZIP_INTERNALERROR = (-104);
  Z_DEFAULT_COMPRESSION = -(1);
  Z_DEFLATED = 8;

(*
{ tm_zip contain date/time info }
type
  tm_zip = record
     tm_sec : integer;            { seconds after the minute - [0,59] }
     tm_min : integer;            { minutes after the hour - [0,59] }
     tm_hour : integer;           { hours since midnight - [0,23] }
     tm_mday : integer;           { day of the month - [1,31] }
     tm_mon : integer;            { months since January - [0,11] }
     tm_year : integer;           { years - [1980..2044] }
  end;
*)
type
  zip_fileinfo = record
    tmz_date: tm_zip;        { date in understandable format           }
    dosDate:  longword;         { if dos_date = 0, tmu_date is used       }
    {   flag : longint;       }{ general purpose bit flag        2 bytes }

    internal_fa: longint;    { internal file attributes        2 bytes }
    external_fa: longint;    { external file attributes        4 bytes }
  end;
  zip_fileinfo_ptr = ^zip_fileinfo;

function zipOpen(const pathname: PChar; append: longint): zipFile; {ZEXPORT}
{ Create a zipfile.
  pathname contain on Windows NT a filename like "c:\\zlib\\zlib111.zip" or on
  an Unix computer "zlib/zlib111.zip".
  if the file pathname exist and append=1, the zip will be created at the end
  of the file. (useful if the file contain a self extractor code)
  If the zipfile cannot be opened, the return value is NIL.
  Else, the return value is a zipFile Handle, usable with other function
  of this zip package. }

function zipOpenNewFileInZip(afile: zipFile;
  {const} filename: PChar; const zipfi: zip_fileinfo_ptr; const extrafield_local: pointer; size_extrafield_local: integer; const extrafield_global: pointer; size_extrafield_global: integer; const comment: PChar; method: longint; level: longint): longint; {ZEXPORT}
{ Open a file in the ZIP for writing.
  filename : the filename in zip (if NIL, '-' without quote will be used
  zipfi^ contain supplemental information
  if extrafield_local<>NIL and size_extrafield_local>0, extrafield_local
    contains the extrafield data the the local header
  if extrafield_global<>NIL and size_extrafield_global>0, extrafield_global
    contains the extrafield data the the local header
  if comment <> NIL, comment contain the comment string
  method contain the compression method (0 for store, Z_DEFLATED for deflate)
  level contain the level of compression (can be Z_DEFAULT_COMPRESSION) }

function zipWriteInFileInZip(afile: zipFile; const buf: pointer; len: cardinal): longint; {ZEXPORT}
{ Write data in the zipfile }

function zipCloseFileInZip(afile: zipFile): longint; {ZEXPORT}
 { Close the current file in the zipfile }

function zipClose(afile: zipFile; const global_comment: PChar): longint; {ZEXPORT}
 { Close the zipfile }

implementation

uses
  {$ifdef Delphi}
  SysUtils,
  {$else}
  strings,
  {$endif}
  zDeflate, crc;

const
  VERSIONMADEBY = ($0); { platform depedent }

const
  zip_copyright: PChar = ' zip 0.15 Copyright 1998 Gilles Vollant ';


const
  SIZEDATA_INDATABLOCK = (4096 - (4 * 4));

  LOCALHEADERMAGIC = $04034b50;
  {CENTRALHEADERMAGIC = $02014b50;}
  ENDHEADERMAGIC   = $06054b50;

  FLAG_LOCALHEADER_OFFSET = $06;
  CRC_LOCALHEADER_OFFSET  = $0e;

  SIZECENTRALHEADER = $2e; { 46 }

type
  linkedlist_datablock_internal_ptr = ^linkedlist_datablock_internal;

  linkedlist_datablock_internal = record
    next_datablock: linkedlist_datablock_internal_ptr;
    avail_in_this_block: longint;
    filled_in_this_block: longint;
    unused: longint; { for future use and alignement }
    Data:   array[0..SIZEDATA_INDATABLOCK - 1] of byte;
  end;

type
  linkedlist_data = record
    first_block: linkedlist_datablock_internal_ptr;
    last_block:  linkedlist_datablock_internal_ptr;
  end;
  linkedlist_data_ptr = ^linkedlist_data;

type
  curfile_info = record
    stream: z_stream;            { zLib stream structure for inflate }
    stream_initialised: boolean; { TRUE is stream is initialised }
    pos_in_buffered_data: integer;  { last written byte in buffered_data }

    pos_local_header: longint;     { offset of the local header of the file
                                    currenty writing }
    central_header: PChar;       { central header data for the current file }
    size_centralheader: longint;   { size of the central header for cur file }
    flag: longint;                 { flag of the file currently writing }

    method:  longint;                { compression method of file currenty wr.}
    buffered_data: array[0..Z_BUFSIZE - 1] of byte;{ buffer contain compressed data to be written}
    dosDate: longint;
    crc32:   longint;
  end;

type
  zip_internal = record
    filezip: FILEptr;
    central_dir: linkedlist_data;  { datablock with central dir in construction}
    in_opened_file_inzip: boolean; { TRUE if a file in the zip is currently writ.}
    ci: curfile_info;              { info on the file curretly writing }

    begin_pos:    longint;            { position of the beginning of the zipfile }
    number_entry: longint;
  end;
  zip_internal_ptr = ^zip_internal;

function allocate_new_datablock: linkedlist_datablock_internal_ptr;
var
  ldi: linkedlist_datablock_internal_ptr;
begin
  ldi := linkedlist_datablock_internal_ptr(GetMem(sizeof(linkedlist_datablock_internal)));
  if (ldi <> nil) then
  begin
    ldi^.next_datablock      := nil;
    ldi^.filled_in_this_block := 0;
    ldi^.avail_in_this_block := SIZEDATA_INDATABLOCK;
  end;
  allocate_new_datablock := ldi;
end;

procedure free_datablock(var ldi: linkedlist_datablock_internal_ptr);
var
  ldinext: linkedlist_datablock_internal_ptr;
begin
  while (ldi <> nil) do
  begin
    ldinext := ldi^.next_datablock;
    FreeMem(ldi);
    ldi := ldinext;
  end;
end;

procedure init_linkedlist(var ll: linkedlist_data);
begin
  ll.last_block  := nil;
  ll.first_block := nil;
end;

procedure free_linkedlist(var ll: linkedlist_data);
begin
  free_datablock(ll.first_block);
  ll.last_block  := nil;
  ll.first_block := nil;
end;

function add_data_in_datablock(ll: linkedlist_data_ptr; const buf: pointer; len: longint): longint;
var
  ldi: linkedlist_datablock_internal_ptr;
  from_copy: {const} Pbyte;
var
  copy_this: integer;
  i:   integer;
  to_copy: Pbyte;
begin
  if (ll = nil) then
  begin
    add_data_in_datablock := ZIP_INTERNALERROR;
    exit;
  end;

  if (ll^.last_block = nil) then
  begin
    ll^.last_block  := allocate_new_datablock;
    ll^.first_block := ll^.last_block;
    if (ll^.first_block = nil) then
    begin
      add_data_in_datablock := ZIP_INTERNALERROR;
      exit;
    end;
  end;

  ldi := ll^.last_block;
  from_copy := Pbyte(buf);

  while (len > 0) do
  begin
    if (ldi^.avail_in_this_block = 0) then
    begin
      ldi^.next_datablock := allocate_new_datablock;
      if (ldi^.next_datablock = nil) then
      begin
        add_data_in_datablock := ZIP_INTERNALERROR;
        exit;
      end;
      ldi := ldi^.next_datablock;
      ll^.last_block := ldi;
    end;

    if (ldi^.avail_in_this_block < len) then
      copy_this := integer(ldi^.avail_in_this_block)
    else
      copy_this := integer(len);

    to_copy := @(ldi^.Data[ldi^.filled_in_this_block]);

    for i := 0 to copy_this - 1 do
      Pbytearray(to_copy)^[i] := Pbytearray(from_copy)^[i];

    Inc(ldi^.filled_in_this_block, copy_this);
    Dec(ldi^.avail_in_this_block, copy_this);
    Inc(from_copy, copy_this);
    Dec(len, copy_this);
  end;
  add_data_in_datablock := ZIP_OK;
end;


function write_datablock(fout: FILEptr; ll: linkedlist_data_ptr): longint;
var
  ldi: linkedlist_datablock_internal_ptr;
begin
  ldi := ll^.first_block;
  while (ldi <> nil) do
  begin
    if (ldi^.filled_in_this_block > 0) then
      if (fwrite(@ldi^.Data, integer(ldi^.filled_in_this_block), 1, fout) <> 1) then
      begin
        write_datablock := ZIP_ERRNO;
        exit;
      end;
    ldi := ldi^.next_datablock;
  end;
  write_datablock := ZIP_OK;
end;

{**************************************************************************}

{ ===========================================================================
   Outputs a long in LSB order to the given file
   nbByte = 1, 2 or 4 (byte, short or long)  }

function ziplocal_putValue(afile: FILEptr; x: longint; nbByte: longint): longint;
var
  buf: array[0..4 - 1] of byte;
  n:   longint;
begin
  for n := 0 to nbByte - 1 do
  begin
    buf[n] := byte(x and $ff);
    x      := x shr 8;
  end;
  if (fwrite(@buf, nbByte, 1, afile) <> 1) then
    ziplocal_putValue := ZIP_ERRNO
  else
    ziplocal_putValue := ZIP_OK;
end;

procedure ziplocal_putValue_inmemory(dest: pointer; x: longint; nbByte: longint);
var
  buf: Pbytearray;
  n:   longint;
begin
  buf := Pbytearray(dest);
  for n := 0 to nbByte - 1 do
  begin
    buf^[n] := Byte(x and $ff);
    x := x shr 8;
  end;
end;

{**************************************************************************}


function ziplocal_TmzDateToDosDate(var ptm: tm_zip; dosDate: longint): longint;
var
  year: longint;
begin
  year := longint(ptm.tm_year);
  if (year > 1980) then
    Dec(year, 1980)
  else
  if (year > 80) then
    Dec(year, 80);
  ziplocal_TmzDateToDosDate := longint(
    ((ptm.tm_mday) + (32 * (ptm.tm_mon + 1)) + (512 * year)) shl 16) or
    ((ptm.tm_sec div 2) + (32 * ptm.tm_min) + (2048 * longint(ptm.tm_hour)));
end;


{**************************************************************************}

function zipOpen(const pathname: PChar; append: longint): zipFile; {ZEXPORT}
var
  ziinit: zip_internal;
  zi:     zip_internal_ptr;
begin
  if (append = 0) then
    ziinit.filezip := fopen(pathname, fopenwrite)
  else
    ziinit.filezip := fopen(pathname, fappendwrite);

  if (ziinit.filezip = nil) then
  begin
    zipOpen := nil;
    exit;
  end;
  ziinit.begin_pos    := ftell(ziinit.filezip);
  ziinit.in_opened_file_inzip := False;
  ziinit.ci.stream_initialised := False;
  ziinit.number_entry := 0;
  init_linkedlist(ziinit.central_dir);

  zi := zip_internal_ptr(AllocMem(sizeof(zip_internal)));
  if (zi = nil) then
  begin
    fclose(ziinit.filezip);
    zipOpen := nil;
    exit;
  end;

  zi^     := ziinit;
  zipOpen := zipFile(zi);
end;

function zipOpenNewFileInZip(afile: zipFile;
  {const} filename: PChar; const zipfi: zip_fileinfo_ptr; const extrafield_local: pointer; size_extrafield_local: integer; const extrafield_global: pointer; size_extrafield_global: integer; const comment: PChar; method: longint; level: longint): longint; {ZEXPORT}
var
  zi:  zip_internal_ptr;
  size_filename: integer;
  size_comment: integer;
  i:   integer;
  err: longint;
begin
  err := ZIP_OK;
  if (afile = nil) then
  begin
    zipOpenNewFileInZip := ZIP_PARAMERROR;
    exit;
  end;
  if ((method <> 0) and (method <> Z_DEFLATED)) then
  begin
    zipOpenNewFileInZip := ZIP_PARAMERROR;
    exit;
  end;

  zi := zip_internal_ptr(afile);

  if (zi^.in_opened_file_inzip = True) then
  begin
    err := zipCloseFileInZip(afile);
    if (err <> ZIP_OK) then
    begin
      zipOpenNewFileInZip := err;
      exit;
    end;
  end;

  if (filename = nil) then
    filename := '-';

  if (comment = nil) then
    size_comment := 0
  else
    size_comment := strlen(comment);

  size_filename := strlen(filename);

  if (zipfi = nil) then
    zi^.ci.dosDate := 0
  else
  if (zipfi^.dosDate <> 0) then
    zi^.ci.dosDate := zipfi^.dosDate
  else
    zi^.ci.dosDate := ziplocal_TmzDateToDosDate(zipfi^.tmz_date, zipfi^.dosDate);
  zi^.ci.flag := 0;
  if ((level = 8) or (level = 9)) then
    zi^.ci.flag := zi^.ci.flag or 2;
  if ((level = 2)) then
    zi^.ci.flag := zi^.ci.flag or 4;
  if ((level = 1)) then
    zi^.ci.flag := zi^.ci.flag or 6;

  zi^.ci.crc32  := 0;
  zi^.ci.method := method;
  zi^.ci.stream_initialised := False;
  zi^.ci.pos_in_buffered_data := 0;
  zi^.ci.pos_local_header := ftell(zi^.filezip);
  zi^.ci.size_centralheader := SIZECENTRALHEADER + size_filename +
    size_extrafield_global + size_comment;
  zi^.ci.central_header := PChar(AllocMem(integer(zi^.ci.size_centralheader)));

  ziplocal_putValue_inmemory(zi^.ci.central_header, longint(CENTRALHEADERMAGIC), 4);
  { version info }
  ziplocal_putValue_inmemory(zi^.ci.central_header + 4, longint(VERSIONMADEBY), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 6, longint(20), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 8, longint(zi^.ci.flag), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 10, longint(zi^.ci.method), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 12, longint(zi^.ci.dosDate), 4);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 16, longint(0), 4); {crc}
  ziplocal_putValue_inmemory(zi^.ci.central_header + 20, longint(0), 4); {compr size}
  ziplocal_putValue_inmemory(zi^.ci.central_header + 24, longint(0), 4); {uncompr size}
  ziplocal_putValue_inmemory(zi^.ci.central_header + 28, longint(size_filename), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 30, longint(size_extrafield_global), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 32, longint(size_comment), 2);
  ziplocal_putValue_inmemory(zi^.ci.central_header + 34, longint(0), 2); {disk nm start}

  if (zipfi = nil) then
    ziplocal_putValue_inmemory(zi^.ci.central_header + 36, longint(0), 2)
  else
    ziplocal_putValue_inmemory(zi^.ci.central_header + 36, longint(zipfi^.internal_fa), 2);

  if (zipfi = nil) then
    ziplocal_putValue_inmemory(zi^.ci.central_header + 38, longint(0), 4)
  else
    ziplocal_putValue_inmemory(zi^.ci.central_header + 38, longint(zipfi^.external_fa), 4);

  ziplocal_putValue_inmemory(zi^.ci.central_header + 42, longint(zi^.ci.pos_local_header), 4);

  i := 0;
  while (i < size_filename) do
  begin
    (zi^.ci.central_header +SIZECENTRALHEADER + i)^ := (filename + i)^;
    Inc(i);
  end;

  i := 0;
  while (i < size_extrafield_global) do
  begin
    (zi^.ci.central_header +SIZECENTRALHEADER + size_filename + i)^ :=
      ({const} PChar(extrafield_global) + i)^;
    Inc(i);
  end;

  i := 0;
  while (i < size_comment) do
  begin
    (zi^.ci.central_header +SIZECENTRALHEADER + size_filename + size_extrafield_global + i)^ := (filename + i)^;
    Inc(i);
  end;
  if (zi^.ci.central_header = nil) then
  begin
    zipOpenNewFileInZip := ZIP_INTERNALERROR;
    exit;
  end;

  { write the local header }
  err := ziplocal_putValue(zi^.filezip, longint(LOCALHEADERMAGIC), 4);

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(20), 2); { version needed to extract }
  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.flag), 2);

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.method), 2);

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.dosDate), 4);

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(0), 4); { crc 32, unknown }
  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(0), 4); { compressed size, unknown }
  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(0), 4); { uncompressed size, unknown }

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(size_filename), 2);

  if (err = ZIP_OK) then
    err := ziplocal_putValue(zi^.filezip, longint(size_extrafield_local), 2);

  if ((err = ZIP_OK) and (size_filename > 0)) then
    if (fwrite(filename, integer(size_filename), 1, zi^.filezip) <> 1) then
      err := ZIP_ERRNO;

  if ((err = ZIP_OK) and (size_extrafield_local > 0)) then
    if (fwrite(extrafield_local, integer(size_extrafield_local), 1, zi^.filezip) <> 1) then
      err := ZIP_ERRNO;

  zi^.ci.stream.avail_in  := integer(0);
  zi^.ci.stream.avail_out := integer(Z_BUFSIZE);
  zi^.ci.stream.next_out  := Pbyte(@zi^.ci.buffered_data);
  zi^.ci.stream.total_in  := 0;
  zi^.ci.stream.total_out := 0;

  if ((err = ZIP_OK) and (zi^.ci.method = Z_DEFLATED)) then
  begin
    err := deflateInit2(zi^.ci.stream, level,
      Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);

    if (err = Z_OK) then
      zi^.ci.stream_initialised := True;
  end;

  if (err = Z_OK) then
    zi^.in_opened_file_inzip := True;
  zipOpenNewFileInZip := err;
end;

function zipWriteInFileInZip(afile: zipFile; const buf: pointer; len: cardinal): longint; {ZEXPORT}
var
  zi:  zip_internal_ptr;
  err: longint;
var
  uTotalOutBefore: longint;
var
  copy_this, i: integer;
begin
  err := ZIP_OK;

  if (afile = nil) then
  begin
    zipWriteInFileInZip := ZIP_PARAMERROR;
    exit;
  end;
  zi := zip_internal_ptr(afile);

  if (zi^.in_opened_file_inzip = False) then
  begin
    zipWriteInFileInZip := ZIP_PARAMERROR;
    exit;
  end;

  zi^.ci.stream.next_in := buf;
  zi^.ci.stream.avail_in := len;
  zi^.ci.crc32 := crc32(zi^.ci.crc32, buf, len);

  while ((err = ZIP_OK) and (zi^.ci.stream.avail_in > 0)) do
  begin
    if (zi^.ci.stream.avail_out = 0) then
    begin
      if fwrite(@zi^.ci.buffered_data, integer(zi^.ci.pos_in_buffered_data), 1, zi^.filezip) <> 1 then
        err := ZIP_ERRNO;
      zi^.ci.pos_in_buffered_data := 0;
      zi^.ci.stream.avail_out := integer(Z_BUFSIZE);
      zi^.ci.stream.next_out  := Pbyte(@zi^.ci.buffered_data);
    end;

    if (zi^.ci.method = Z_DEFLATED) then
    begin
      uTotalOutBefore := zi^.ci.stream.total_out;
      err := deflate(zi^.ci.stream, Z_NO_FLUSH);
      Inc(zi^.ci.pos_in_buffered_data, integer(zi^.ci.stream.total_out - uTotalOutBefore));
    end
    else
    begin
      if (zi^.ci.stream.avail_in < zi^.ci.stream.avail_out) then
        copy_this := zi^.ci.stream.avail_in
      else
        copy_this := zi^.ci.stream.avail_out;

      for i := 0 to copy_this - 1 do
        (PChar(zi^.ci.stream.next_out) +i)^ :=
          ( {const} PChar(zi^.ci.stream.next_in) + i)^;


      Dec(zi^.ci.stream.avail_in, copy_this);
      Dec(zi^.ci.stream.avail_out, copy_this);
      Inc(zi^.ci.stream.next_in, copy_this);
      Inc(zi^.ci.stream.next_out, copy_this);
      Inc(zi^.ci.stream.total_in, copy_this);
      Inc(zi^.ci.stream.total_out, copy_this);
      Inc(zi^.ci.pos_in_buffered_data, copy_this);
    end;
  end;

  zipWriteInFileInZip := 0;
end;

function zipCloseFileInZip(afile: zipFile): longint; {ZEXPORT}
var
  zi:  zip_internal_ptr;
  err: longint;
var
  uTotalOutBefore: longint;
var
  cur_pos_inzip: longint;
begin
  err := ZIP_OK;

  if (afile = nil) then
  begin
    zipCloseFileInZip := ZIP_PARAMERROR;
    exit;
  end;
  zi := zip_internal_ptr(afile);

  if (zi^.in_opened_file_inzip = False) then
  begin
    zipCloseFileInZip := ZIP_PARAMERROR;
    exit;
  end;
  zi^.ci.stream.avail_in := 0;

  if (zi^.ci.method = Z_DEFLATED) then
    while (err = ZIP_OK) do
    begin
      if (zi^.ci.stream.avail_out = 0) then
      begin
        if fwrite(@zi^.ci.buffered_data, integer(zi^.ci.pos_in_buffered_data), 1, zi^.filezip) <> 1 then
          err := ZIP_ERRNO;
        zi^.ci.pos_in_buffered_data := 0;
        zi^.ci.stream.avail_out := integer(Z_BUFSIZE);
        zi^.ci.stream.next_out  := Pbyte(@zi^.ci.buffered_data);
      end;
      uTotalOutBefore := zi^.ci.stream.total_out;
      err := deflate(zi^.ci.stream, Z_FINISH);
      Inc(zi^.ci.pos_in_buffered_data, integer(zi^.ci.stream.total_out - uTotalOutBefore));
    end;

  if (err = Z_STREAM_END) then
    err := ZIP_OK; { this is normal }

  if (zi^.ci.pos_in_buffered_data > 0) and (err = ZIP_OK) then
    if fwrite(@zi^.ci.buffered_data, integer(zi^.ci.pos_in_buffered_data), 1, zi^.filezip) <> 1 then
      err := ZIP_ERRNO;

  if ((zi^.ci.method = Z_DEFLATED) and (err = ZIP_OK)) then
  begin
    err := deflateEnd(zi^.ci.stream);
    zi^.ci.stream_initialised := False;
  end;

  ziplocal_putValue_inmemory(zi^.ci.central_header + 16, longint(zi^.ci.crc32), 4); {crc}
  ziplocal_putValue_inmemory(zi^.ci.central_header + 20, longint(zi^.ci.stream.total_out), 4); {compr size}
  ziplocal_putValue_inmemory(zi^.ci.central_header + 24, longint(zi^.ci.stream.total_in), 4); {uncompr size}

  if (err = ZIP_OK) then
    err := add_data_in_datablock(@zi^.central_dir, zi^.ci.central_header, longint(zi^.ci.size_centralheader));

  FreeMem(zi^.ci.central_header);
  zi^.ci.central_header := nil;

  if (err = ZIP_OK) then
  begin
    cur_pos_inzip := ftell(zi^.filezip);
    if fseek(zi^.filezip, zi^.ci.pos_local_header + 14, SEEK_SET) <> 0 then
      err := ZIP_ERRNO;

    if (err = ZIP_OK) then
      err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.crc32), 4); { crc 32, unknown }

    if (err = ZIP_OK) then { compressed size, unknown }
      err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.stream.total_out), 4);

    if (err = ZIP_OK) then { uncompressed size, unknown }
      err := ziplocal_putValue(zi^.filezip, longint(zi^.ci.stream.total_in), 4);

    if fseek(zi^.filezip, cur_pos_inzip, SEEK_SET) <> 0 then
      err := ZIP_ERRNO;
  end;

  Inc(zi^.number_entry);
  zi^.in_opened_file_inzip := False;

  zipCloseFileInZip := err;
end;

function zipClose(afile: zipFile; const global_comment: PChar): longint; {ZEXPORT}
var
  zi:  zip_internal_ptr;
  err: longint;
  size_centraldir: longint;
  centraldir_pos_inzip: longint;
  size_global_comment: integer;
var
  ldi: linkedlist_datablock_internal_ptr;
begin
  err := 0;
  size_centraldir := 0;
  if (afile = nil) then
  begin
    zipClose := ZIP_PARAMERROR;
    exit;
  end;
  zi := zip_internal_ptr(afile);

  if (zi^.in_opened_file_inzip = True) then
    err := zipCloseFileInZip(afile);

  if (global_comment = nil) then
    size_global_comment := 0
  else
    size_global_comment := strlen(global_comment);

  centraldir_pos_inzip := ftell(zi^.filezip);
  if (err = ZIP_OK) then
  begin
    ldi := zi^.central_dir.first_block;
    while (ldi <> nil) do
    begin
      if ((err = ZIP_OK) and (ldi^.filled_in_this_block > 0)) then
        if fwrite(@ldi^.Data, integer(ldi^.filled_in_this_block), 1, zi^.filezip) <> 1 then
          err := ZIP_ERRNO;

      Inc(size_centraldir, ldi^.filled_in_this_block);
      ldi := ldi^.next_datablock;
    end;
  end;
  free_datablock(zi^.central_dir.first_block);

  if (err = ZIP_OK) then { Magic End }
    err := ziplocal_putValue(zi^.filezip, longint(ENDHEADERMAGIC), 4);

  if (err = ZIP_OK) then { number of this disk }
    err := ziplocal_putValue(zi^.filezip, longint(0), 2);

  if (err = ZIP_OK) then { number of the disk with the start of the central directory }
    err := ziplocal_putValue(zi^.filezip, longint(0), 2);

  if (err = ZIP_OK) then { total number of entries in the central dir on this disk }
    err := ziplocal_putValue(zi^.filezip, longint(zi^.number_entry), 2);

  if (err = ZIP_OK) then { total number of entries in the central dir }
    err := ziplocal_putValue(zi^.filezip, longint(zi^.number_entry), 2);

  if (err = ZIP_OK) then { size of the central directory }
    err := ziplocal_putValue(zi^.filezip, longint(size_centraldir), 4);

  if (err = ZIP_OK) then { offset of start of central directory with respect to the
                          starting disk number }
    err := ziplocal_putValue(zi^.filezip, longint(centraldir_pos_inzip), 4);

  if (err = ZIP_OK) then { zipfile comment length }
    err := ziplocal_putValue(zi^.filezip, longint(size_global_comment), 2);

  if ((err = ZIP_OK) and (size_global_comment > 0)) then
    if fwrite(global_comment, integer(size_global_comment), 1, zi^.filezip) <> 1 then
      err := ZIP_ERRNO;
  fclose(zi^.filezip);
  FreeMem(zi);

  zipClose := err;
end;

end.