File: miniunz.pas

package info (click to toggle)
fpc 2.6.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 178,760 kB
  • ctags: 83,946
  • sloc: pascal: 2,000,374; xml: 138,807; ansic: 9,617; asm: 7,843; yacc: 3,747; php: 3,271; sh: 2,626; makefile: 2,610; lex: 2,537; sql: 267; cpp: 145; sed: 132; perl: 126; csh: 34; tcl: 7
file content (575 lines) | stat: -rw-r--r-- 14,678 bytes parent folder | download | duplicates (4)
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
program MiniUnz;

{ mini unzip demo package by Gilles Vollant

  Usage : miniunz [-exvlo] file.zip [file_to_extract]

  -l or -v list the content of the zipfile.
        -e extract a specific file or all files if [file_to_extract] is missing
        -x like -e, but extract without path information
        -o overwrite an existing file without warning

  Pascal tranlastion
  Copyright (C) 2000 by Jacques Nomssi Nzali
  For conditions of distribution and use, see copyright notice in readme.txt
}{$ifdef WIN32}
  {$define Delphi}
  {$ifndef FPC}
    {$define Delphi32}
  {$endif}
{$endif}

uses
  sysutils,
  {$ifdef Delphi}
  Windows,
  {$else}
   zlib,ctypes,
  {$endif}
  ziputils,
  paszlib,
  ctypes,
  unzip;

const
  CASESENSITIVITY = 0;
  WRITEBUFFERSIZE = 8192;


{ change_file_date : change the date/time of a file
    filename : the filename of the file where date/time must be modified
    dosdate : the new date at the MSDos format (4 bytes)
    tmu_date : the SAME new date at the tm_unz format }

  procedure change_file_date(const filename: PChar; dosdate: longword; tmu_date: tm_unz);
{$ifdef Delphi32}
  var
    hFile: THandle;
    ftm, ftLocal, ftCreate, ftLastAcc, ftLastWrite: TFileTime;
  begin
    hFile := CreateFile(filename, GENERIC_READ or GENERIC_WRITE,
      0, nil, OPEN_EXISTING, 0, 0);
    GetFileTime(hFile, @ftCreate, @ftLastAcc, @ftLastWrite);
    DosDateTimeToFileTime(word((dosdate shl 16)), word(dosdate), ftLocal);
    LocalFileTimeToFileTime(ftLocal, ftm);
    SetFileTime(hFile, @ftm, @ftLastAcc, @ftm);
    CloseHandle(hFile);
  end;

{$else}
{$if defined(FPC) and defined(win32)}
var
  hFile : THandle;
  ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite : TFileTime;
begin
  hFile := CreateFile(filename,GENERIC_READ or GENERIC_WRITE,
                      0,NIL,OPEN_EXISTING,0,0);
  GetFileTime(hFile, @ftCreate, @ftLastAcc, @ftLastWrite);
  DosDateTimeToFileTime(WORD((dosdate shl 16)), WORD(dosdate), @ftLocal);
  LocalFileTimeToFileTime(ftLocal, ftm);
  SetFileTime(hFile,ftm, ftLastAcc, ftm);
  CloseHandle(hFile);
end;
  {$else} {  msdos }
begin
  FileSetDate(filename,dosdate);
end;
  {$endif}
{$endif}


{ mymkdir and change_file_date are not 100 % portable
  As I don't know well Unix, I wait feedback for the unix portion }

  function mymkdir(dirname: PChar): boolean;
  var
    S: string;
  begin
    S := StrPas(dirname);
  {$I-}
    mkdir(S);
    mymkdir := IOresult = 0;
  end;

  function makedir(newdir: PChar): boolean;
  var
    buffer: PChar;
    p:      PChar;
    len:    cint;
  var
    hold:   char;
  begin
    makedir := False;
    len     := strlen(newdir);

    if (len <= 0) then
      exit;

    buffer := PChar(allocmem( len + 1));

    strcopy(buffer, newdir);

    if (buffer[len - 1] = '/') then
      buffer[len - 1] := #0;

    if mymkdir(buffer) then
    begin
      if Assigned(buffer) then
        freemem( buffer);
      makedir := True;
      exit;
    end;

    p := buffer + 1;
    while True do
    begin
      while ((p^ <> #0) and (p^ <> '\') and (p^ <> '/')) do
        Inc(p);
      hold := p^;
      p^   := #0;
      if (not mymkdir(buffer)) {and (errno = ENOENT)} then
      begin
        WriteLn('couldn''t create directory ', buffer);
        if Assigned(buffer) then
          freemem( buffer);
        exit;
      end;
      if (hold = #0) then
        break;
      p^ := hold;
      Inc(p);
    end;
    if Assigned(buffer) then
      freemem( buffer);
    makedir := True;
  end;

  procedure do_banner;
  begin
    WriteLn('MiniUnz 0.15, demo package written by Gilles Vollant');
    WriteLn('Pascal port by Jacques Nomssi Nzali');
    WriteLn('more info at http://wwww.tu-chemnitz.de/~nomssi/paszlib.html');
    WriteLn;
  end;

  procedure do_help;
  begin
    WriteLn('Usage : miniunz [-exvlo] file.zip [file_to_extract]');
    WriteLn;
  end;

  function LeadingZero(w: word): string;
  var
    s: string;
  begin
    Str(w: 0, s);
    if Length(s) = 1 then
      s := '0' + s;
    LeadingZero := s;
  end;

  function HexToStr(w: clong): string;
  const
    ByteToChar: array[0..$F] of char = '0123456789ABCDEF';
  var
    s: string;
    i: cint;
    x: clong;
  begin
    s := '';
    x := w;
    for i := 0 to 3 do
    begin
      s := ByteToChar[byte(x) shr 4] + ByteToChar[byte(x) and $F] + s;
      x := x shr 8;
    end;
    HexToStr := s;
  end;

  function do_list(uf: unzFile): cint;
  var
    i:      longword;
    gi:     unz_global_info;
    err:    cint;
  var
    filename_inzip: array[0..255] of char;
    file_info: unz_file_info;
    ratio:  longword;
    string_method: string[255];
  var
    iLevel: cuInt;
  begin
    err := unzGetGlobalInfo(uf, gi);
    if (err <> UNZ_OK) then
      WriteLn('error ', err, ' with zipfile in unzGetGlobalInfo');
    WriteLn(' Length  Method   Size  Ratio   Date    Time   CRC-32     Name');
    WriteLn(' ------  ------   ----  -----   ----    ----   ------     ----');
    for i := 0 to gi.number_entry - 1 do
    begin
      ratio := 0;
      err   := unzGetCurrentFileInfo(uf, @file_info, filename_inzip, sizeof(filename_inzip), nil, 0, nil, 0);
      if (err <> UNZ_OK) then
      begin
        WriteLn('error ', err, ' with zipfile in unzGetCurrentFileInfo');
        break;
      end;
      if (file_info.uncompressed_size > 0) then
        ratio := (file_info.compressed_size * 100) div file_info.uncompressed_size;

      if (file_info.compression_method = 0) then
        string_method := 'Stored'
      else
      if (file_info.compression_method = Z_DEFLATED) then
      begin
        iLevel := cuInt((file_info.flag and $06) div 2);
        case iLevel of
          0: string_method    := 'Defl:N';
          1: string_method    := 'Defl:X';
          2, 3: string_method := 'Defl:F'; { 2:fast , 3 : extra fast}
          else
            string_method := 'Unkn. ';
        end;
      end;

      WriteLn(file_info.uncompressed_size: 7, '  ',
        string_method: 6, ' ',
        file_info.compressed_size: 7, ' ',
        ratio: 3, '%  ', LeadingZero(longword(file_info.tmu_date.tm_mon) + 1), '-',
        LeadingZero(longword(file_info.tmu_date.tm_mday)): 2, '-',
        LeadingZero(longword(file_info.tmu_date.tm_year mod 100)): 2, '  ',
        LeadingZero(longword(file_info.tmu_date.tm_hour)), ':',
        LeadingZero(longword(file_info.tmu_date.tm_min)), '  ',
        HexToStr(longword(file_info.crc)), '  ',
        filename_inzip);

      if ((i + 1) < gi.number_entry) then
      begin
        err := unzGoToNextFile(uf);
        if (err <> UNZ_OK) then
        begin
          WriteLn('error ', err, ' with zipfile in unzGoToNextFile');
          break;
        end;
      end;
    end;

    do_list := 0;
  end;


  function do_extract_currentfile(uf: unzFile; const popt_extract_without_path: cint; var popt_overwrite: cint): cint;
  var
    filename_inzip: packed array[0..255] of char;
    filename_withoutpath: PChar;
    p:      PChar;
    err:    cint;
    fout:   FILEptr;
    buf:    pointer;
    size_buf: cuInt;
    file_info: unz_file_info;
  var
    write_filename: PChar;
    skip:   cint;
  var
    rep:    char;
    ftestexist: FILEptr;
  var
    answer: string[127];
  var
    c:      char;
  begin
    fout := nil;

    err := unzGetCurrentFileInfo(uf, @file_info, filename_inzip,
      sizeof(filename_inzip), nil, 0, nil, 0);

    if (err <> UNZ_OK) then
    begin
      WriteLn('error ', err, ' with zipfile in unzGetCurrentFileInfo');
      do_extract_currentfile := err;
      exit;
    end;

    size_buf := WRITEBUFFERSIZE;
    buf      := allocmem(size_buf);
    if (buf = nil) then
    begin
      WriteLn('Error allocating memory');
      do_extract_currentfile := UNZ_INTERNALERROR;
      exit;
    end;

    filename_withoutpath := filename_inzip;
    p := filename_withoutpath;
    while (p^ <> #0) do
    begin
      if (p^ = '/') or (p^ = '\') then
        filename_withoutpath := p + 1;
      Inc(p);
    end;

    if (filename_withoutpath^ = #0) then
    begin
      if (popt_extract_without_path = 0) then
      begin
        WriteLn('creating directory: ', filename_inzip);
        mymkdir(filename_inzip);
      end;
    end
    else
    begin

      skip := 0;
      if (popt_extract_without_path = 0) then
        write_filename := filename_inzip
      else
        write_filename := filename_withoutpath;

      err := unzOpenCurrentFile(uf);
      if (err <> UNZ_OK) then
        WriteLn('error ', err, ' with zipfile in unzOpenCurrentFile');


      if ((popt_overwrite = 0) and (err = UNZ_OK)) then
      begin
        rep := #0;

        ftestexist := fopen(write_filename, fopenread);
        if (ftestexist <> nil) then
        begin
          fclose(ftestexist);
          repeat
            Write('The file ', write_filename,
              ' exist. Overwrite ? [y]es, [n]o, [A]ll: ');
            ReadLn(answer);

            rep := answer[1];
            if ((rep >= 'a') and (rep <= 'z')) then
              Dec(rep, $20);
          until (rep = 'Y') or (rep = 'N') or (rep = 'A');
        end;

        if (rep = 'N') then
          skip := 1;

        if (rep = 'A') then
          popt_overwrite := 1;
      end;

      if (skip = 0) and (err = UNZ_OK) then
      begin
        fout := fopen(write_filename, fopenwrite);

        { some zipfile don't contain directory alone before file }
        if (fout = nil) and (popt_extract_without_path = 0) and
          (filename_withoutpath <> PChar(@filename_inzip)) then
        begin
          c := (filename_withoutpath - 1)^;
          (filename_withoutpath -1)^ := #0;
          makedir(write_filename);
          (filename_withoutpath -1)^ := c;
          fout := fopen(write_filename, fopenwrite);
        end;

        if (fout = nil) then
          WriteLn('error opening ', write_filename);
      end;

      if (fout <> nil) then
      begin
        WriteLn(' extracting: ', write_filename);

        repeat
          err := unzReadCurrentFile(uf, buf, size_buf);
          if (err < 0) then
          begin
            WriteLn('error ', err, ' with zipfile in unzReadCurrentFile');
            break;
          end;
          if (err > 0) then
            if (fwrite(buf, err, 1, fout) <> 1) then
            begin
              WriteLn('error in writing extracted file');
              err := UNZ_ERRNO;
              break;
            end;
        until (err = 0);
        fclose(fout);
        if (err = 0) then
          change_file_date(write_filename, file_info.dosDate,
            file_info.tmu_date);
      end;

      if (err = UNZ_OK) then
      begin
        err := unzCloseCurrentFile(uf);
        if (err <> UNZ_OK) then
          WriteLn('error ', err, ' with zipfile in unzCloseCurrentFile')
        else
          unzCloseCurrentFile(uf); { don't lose the error }
      end;
    end;

    if buf <> nil then
      freemem( buf);
    do_extract_currentfile := err;
  end;


  function do_extract(uf: unzFile; opt_extract_without_path: cint; opt_overwrite: cint): cint;
  var
    i:   longword;
    gi:  unz_global_info;
    err: cint;
  begin
    err := unzGetGlobalInfo(uf, gi);
    if (err <> UNZ_OK) then
      WriteLn('error ', err, ' with zipfile in unzGetGlobalInfo ');

    for i := 0 to gi.number_entry - 1 do
    begin
      if (do_extract_currentfile(uf, opt_extract_without_path,
        opt_overwrite) <> UNZ_OK) then
        break;

      if ((i + 1) < gi.number_entry) then
      begin
        err := unzGoToNextFile(uf);
        if (err <> UNZ_OK) then
        begin
          WriteLn('error ', err, ' with zipfile in unzGoToNextFile');
          break;
        end;
      end;
    end;

    do_extract := 0;
  end;

  function do_extract_onefile(uf: unzFile; const filename: PChar; opt_extract_without_path: cint; opt_overwrite: cint): cint;
  begin
    if (unzLocateFile(uf, filename, CASESENSITIVITY) <> UNZ_OK) then
    begin
      WriteLn('file ', filename, ' not found in the zipfile');
      do_extract_onefile := 2;
      exit;
    end;

    if (do_extract_currentfile(uf, opt_extract_without_path,
      opt_overwrite) = UNZ_OK) then
      do_extract_onefile := 0
    else
      do_extract_onefile := 1;
  end;

  { -------------------------------------------------------------------- }
  function main: cint;
  const
    zipfilename: PChar = nil;
    filename_to_extract: PChar = nil;
  var
    i:    cint;
    opt_do_list: cint;
    opt_do_extract: cint;
    opt_do_extract_withoutpath: cint;
    opt_overwrite: cint;
    filename_try: array[0..512 - 1] of char;
    uf:   unzFile;
  var
    p:    cint;
    pstr: string[255];
    c:    char;
  begin
    opt_do_list := 0;
    opt_do_extract := 1;
    opt_do_extract_withoutpath := 0;
    opt_overwrite := 0;
    uf := nil;

    do_banner;
    if (ParamCount = 0) then
    begin
      do_help;
      Halt(0);
    end
    else
      for i := 1 to ParamCount do
      begin
        pstr := ParamStr(i);
        if pstr[1] = '-' then
          for p := 2 to Length(pstr) do
          begin
            c := pstr[p];
            case UpCase(c) of
              'L',
              'V': opt_do_list    := 1;
              'X': opt_do_extract := 1;
              'E':
              begin
                opt_do_extract := 1;
                opt_do_extract_withoutpath := 1;
              end;
              'O': opt_overwrite := 1;
            end;
          end
        else
        begin
          pstr := pstr + #0;
          if (zipfilename = nil) then
            zipfilename := StrNew(PChar(@pstr[1]))
          else
          if (filename_to_extract = nil) then
            filename_to_extract := StrNew(PChar(@pstr[1]));
        end;
      end{ for };

    if (zipfilename <> nil) then
    begin
      strcopy(filename_try, zipfilename);
      uf := unzOpen(zipfilename);
      if (uf = nil) then
      begin
        strcat(filename_try, '.zip');
        uf := unzOpen(filename_try);
      end;
    end;

    if (uf = nil) then
    begin
      WriteLn('Cannot open ', zipfilename, ' or ', zipfilename, '.zip');
      Halt(1);
    end;

    WriteLn(filename_try, ' opened');

    if (opt_do_list = 1) then
    begin
      main := do_list(uf);
      exit;
    end
    else
    if (opt_do_extract = 1) then
      if (filename_to_extract = nil) then
      begin
        main := do_extract(uf, opt_do_extract_withoutpath, opt_overwrite);
        exit;
      end
      else
      begin
        main := do_extract_onefile(uf, filename_to_extract,
          opt_do_extract_withoutpath, opt_overwrite);
        exit;
      end;

    unzCloseCurrentFile(uf);

    strDispose(zipfilename);
    strDispose(filename_to_extract);
    main := 0;
  end;

begin
  main;
  Write('Done...');
  Readln;
end.