Subversion Repositories SvarDOS

Rev

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