Subversion Repositories SvarDOS

Rev

Rev 1965 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
296 mateuszvis 1
/*
2
 * simple unzip tool that unzips the content of a zip archive to current directory
1889 mateusz.vi 3
 * if listonly is set to a non-zero value then unzip() only lists the files
296 mateuszvis 4
 * returns 0 on success
5
 *
6
 * this file is part of pkg (SvarDOS)
1889 mateusz.vi 7
 * copyright (C) 2021-2024 Mateusz Viste
296 mateuszvis 8
 */
9
 
10
#include <stdio.h>
11
 
12
#include "fileexst.h"
13
#include "helpers.h"
14
#include "libunzip.h"
613 mateuszvis 15
#include "svarlang.lib\svarlang.h"
296 mateuszvis 16
 
17
#include "unzip.h"
18
 
19
 
1965 mateusz.vi 20
int unzip(const char *zipfile, unsigned char listonly, unsigned char *buff15k) {
296 mateuszvis 21
  struct ziplist *zlist, *znode;
22
  FILE *fd;
23
  int r = 0;
24
 
25
  fd = fopen(zipfile, "rb");
26
  if (fd == NULL) {
1963 mateusz.vi 27
    outputnl(svarlang_str(10, 1)); /* "ERROR: Failed to open the archive file" */
296 mateuszvis 28
    return(1);
29
  }
30
 
31
  zlist = zip_listfiles(fd);
32
  if (zlist == NULL) {
1963 mateusz.vi 33
    outputnl(svarlang_str(10, 2)); /* "ERROR: Invalid ZIP archive" */
296 mateuszvis 34
    fclose(fd);
35
    return(-1);
36
  }
37
 
1889 mateusz.vi 38
  if (listonly != 0) {
39
    /* just list the files inside the archive */
40
    for (znode = zlist; znode != NULL; znode = znode->nextfile) {
1963 mateusz.vi 41
      outputnl(znode->filename);
296 mateuszvis 42
    }
1889 mateusz.vi 43
  } else {
44
    /* examine the list of zipped files - make sure that no file currently
45
     * exists and that files are neither encrypted nor compressed with an
46
     * unsupported method */
47
    for (znode = zlist; znode != NULL; znode = znode->nextfile) {
48
      int zres;
49
      /* convert slash to backslash, print filename and create the directories path */
50
      slash2backslash(znode->filename);
1966 mateusz.vi 51
      output(znode->filename);
52
      output(" ");
1889 mateusz.vi 53
      mkpath(znode->filename);
54
      /* if a dir, we good already */
55
      if (znode->flags == ZIP_FLAG_ISADIR) goto OK;
56
      /* file already exists? */
57
      if (fileexists(znode->filename) != 0) {
1963 mateusz.vi 58
        outputnl(svarlang_str(10, 3)); /* "ERROR: File already exists" */
1889 mateusz.vi 59
        r = 1;
60
        continue;
61
      }
62
      /* uncompress */
1965 mateusz.vi 63
      zres = zip_unzip(fd, znode, znode->filename, buff15k);
1889 mateusz.vi 64
      if (zres != 0) {
1965 mateusz.vi 65
        sprintf(buff15k, svarlang_str(10,4), zres);
66
        outputnl(buff15k);
1889 mateusz.vi 67
        continue;
68
      }
69
      OK:
1963 mateusz.vi 70
      outputnl(svarlang_str(10, 0)); /* "OK" */
296 mateuszvis 71
    }
72
  }
73
 
74
  zip_freelist(&zlist);
75
  fclose(fd);
76
  return(r);
77
}