Subversion Repositories SvarDOS

Rev

Rev 614 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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