Subversion Repositories SvarDOS

Rev

Rev 614 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 614 Rev 1889
Line 1... Line 1...
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
 * if listonly is set to a non-zero value then unzip() only lists the files
3
 * returns 0 on success
4
 * returns 0 on success
4
 *
5
 *
5
 * this file is part of pkg (SvarDOS)
6
 * this file is part of pkg (SvarDOS)
6
 * copyright (C) 2021-2022 Mateusz Viste
7
 * copyright (C) 2021-2024 Mateusz Viste
7
 */
8
 */
8
 
9
 
9
#include <stdio.h>
10
#include <stdio.h>
10
 
11
 
11
#include "fileexst.h"
12
#include "fileexst.h"
Line 15... Line 16...
15
#include "svarlang.lib\svarlang.h"
16
#include "svarlang.lib\svarlang.h"
16
 
17
 
17
#include "unzip.h"
18
#include "unzip.h"
18
 
19
 
19
 
20
 
20
int unzip(const char *zipfile) {
21
int unzip(const char *zipfile, unsigned char listonly) {
21
  struct ziplist *zlist, *znode;
22
  struct ziplist *zlist, *znode;
22
  FILE *fd;
23
  FILE *fd;
23
  int r = 0;
24
  int r = 0;
24
 
25
 
25
  fd = fopen(zipfile, "rb");
26
  fd = fopen(zipfile, "rb");
Line 33... Line 34...
33
    puts(svarlang_str(10, 2)); /* "ERROR: Invalid ZIP archive" */
34
    puts(svarlang_str(10, 2)); /* "ERROR: Invalid ZIP archive" */
34
    fclose(fd);
35
    fclose(fd);
35
    return(-1);
36
    return(-1);
36
  }
37
  }
37
 
38
 
38
  /* examine the list of zipped files - make sure that no file currently
39
  if (listonly != 0) {
39
   * exists and that files are neither encrypted nor compressed with an
40
    /* just list the files inside the archive */
40
   * unsupported method */
-
 
41
  for (znode = zlist; znode != NULL; znode = znode->nextfile) {
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);
42
      puts(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) {
-
 
51
      puts(svarlang_str(10, 3)); /* "ERROR: File already exists" */
-
 
52
      r = 1;
-
 
53
      continue;
-
 
54
    }
43
    }
-
 
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
      }
55
    /* uncompress */
62
      /* uncompress */
56
    zres = zip_unzip(fd, znode, znode->filename);
63
      zres = zip_unzip(fd, znode, znode->filename);
57
    if (zres != 0) {
64
      if (zres != 0) {
58
      kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
65
        kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
59
      puts("");
66
        puts("");
60
      continue;
67
        continue;
-
 
68
      }
-
 
69
      OK:
-
 
70
      puts(svarlang_str(10, 0)); /* "OK" */
61
    }
71
    }
62
    OK:
-
 
63
    puts(svarlang_str(10, 0)); /* "OK" */
-
 
64
  }
72
  }
65
 
73
 
66
  zip_freelist(&zlist);
74
  zip_freelist(&zlist);
67
  fclose(fd);
75
  fclose(fd);
68
  return(r);
76
  return(r);