Subversion Repositories SvarDOS

Rev

Rev 614 | Rev 1963 | 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 "kprintf.h"
15
#include "libunzip.h"
613 mateuszvis 16
#include "svarlang.lib\svarlang.h"
296 mateuszvis 17
 
18
#include "unzip.h"
19
 
20
 
1889 mateusz.vi 21
int unzip(const char *zipfile, unsigned char listonly) {
296 mateuszvis 22
  struct ziplist *zlist, *znode;
23
  FILE *fd;
24
  int r = 0;
25
 
26
  fd = fopen(zipfile, "rb");
27
  if (fd == NULL) {
613 mateuszvis 28
    puts(svarlang_str(10, 1)); /* "ERROR: Failed to open the archive file" */
296 mateuszvis 29
    return(1);
30
  }
31
 
32
  zlist = zip_listfiles(fd);
33
  if (zlist == NULL) {
613 mateuszvis 34
    puts(svarlang_str(10, 2)); /* "ERROR: Invalid ZIP archive" */
296 mateuszvis 35
    fclose(fd);
36
    return(-1);
37
  }
38
 
1889 mateusz.vi 39
  if (listonly != 0) {
40
    /* just list the files inside the archive */
41
    for (znode = zlist; znode != NULL; znode = znode->nextfile) {
42
      puts(znode->filename);
296 mateuszvis 43
    }
1889 mateusz.vi 44
  } else {
45
    /* examine the list of zipped files - make sure that no file currently
46
     * exists and that files are neither encrypted nor compressed with an
47
     * unsupported method */
48
    for (znode = zlist; znode != NULL; znode = znode->nextfile) {
49
      int zres;
50
      /* convert slash to backslash, print filename and create the directories path */
51
      slash2backslash(znode->filename);
52
      printf("%s ", znode->filename);
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) {
58
        puts(svarlang_str(10, 3)); /* "ERROR: File already exists" */
59
        r = 1;
60
        continue;
61
      }
62
      /* uncompress */
63
      zres = zip_unzip(fd, znode, znode->filename);
64
      if (zres != 0) {
65
        kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
66
        puts("");
67
        continue;
68
      }
69
      OK:
70
      puts(svarlang_str(10, 0)); /* "OK" */
296 mateuszvis 71
    }
72
  }
73
 
74
  zip_freelist(&zlist);
75
  fclose(fd);
76
  return(r);
77
}