Subversion Repositories SvarDOS

Rev

Details | 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
3
 * returns 0 on success
4
 *
5
 * this file is part of pkg (SvarDOS)
6
 * copyright (C) 2021 Mateusz Viste
7
 */
8
 
9
#include <stdio.h>
10
 
11
#include "fileexst.h"
12
#include "helpers.h"
13
#include "kprintf.h"
14
#include "libunzip.h"
15
 
16
#include "unzip.h"
17
 
18
 
19
int unzip(const char *zipfile) {
20
  struct ziplist *zlist, *znode;
21
  FILE *fd;
22
  int r = 0;
23
 
24
  fd = fopen(zipfile, "rb");
25
  if (fd == NULL) {
26
    kitten_puts(10, 1, "ERROR: Failed to open the archive file");
27
    return(1);
28
  }
29
 
30
  zlist = zip_listfiles(fd);
31
  if (zlist == NULL) {
32
    kitten_puts(10, 2, "ERROR: Invalid ZIP archive");
33
    fclose(fd);
34
    return(-1);
35
  }
36
 
37
  /* examine the list of zipped files - make sure that no file currently
38
   * exists and that files are neither encrypted nor compressed with an
39
   * unsupported method */
40
  for (znode = zlist; znode != NULL; znode = znode->nextfile) {
41
    int zres;
42
    /* convert slash to backslash, print filename and create the directories path */
43
    slash2backslash(znode->filename);
44
    printf("%s ", znode->filename);
45
    mkpath(znode->filename);
46
    /* if a dir, we good already */
47
    if (znode->flags == ZIP_FLAG_ISADIR) goto OK;
48
    /* file already exists? */
49
    if (fileexists(znode->filename) != 0) {
50
      kitten_puts(10, 3, "ERROR: File already exists");
51
      r = 1;
52
      continue;
53
    }
54
    /* uncompress */
55
    zres = zip_unzip(fd, znode, znode->filename);
56
    if (zres != 0) {
57
      kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
58
      puts("");
59
      continue;
60
    }
61
    OK:
62
    kitten_puts(10, 0, "OK");
63
  }
64
 
65
  zip_freelist(&zlist);
66
  fclose(fd);
67
  return(r);
68
}