Subversion Repositories SvarDOS

Rev

Rev 1963 | Rev 1966 | Go to most recent revision | 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);
51
      printf("%s ", znode->filename);
52
      mkpath(znode->filename);
53
      /* if a dir, we good already */
54
      if (znode->flags == ZIP_FLAG_ISADIR) goto OK;
55
      /* file already exists? */
56
      if (fileexists(znode->filename) != 0) {
1963 mateusz.vi 57
        outputnl(svarlang_str(10, 3)); /* "ERROR: File already exists" */
1889 mateusz.vi 58
        r = 1;
59
        continue;
60
      }
61
      /* uncompress */
1965 mateusz.vi 62
      zres = zip_unzip(fd, znode, znode->filename, buff15k);
1889 mateusz.vi 63
      if (zres != 0) {
1965 mateusz.vi 64
        sprintf(buff15k, svarlang_str(10,4), zres);
65
        outputnl(buff15k);
1889 mateusz.vi 66
        continue;
67
      }
68
      OK:
1963 mateusz.vi 69
      outputnl(svarlang_str(10, 0)); /* "OK" */
296 mateuszvis 70
    }
71
  }
72
 
73
  zip_freelist(&zlist);
74
  fclose(fd);
75
  return(r);
76
}