Subversion Repositories SvarDOS

Rev

Rev 296 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 296 Rev 613
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
 * 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 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
 
16
 
16
#include "unzip.h"
17
#include "unzip.h"
17
 
18
 
18
 
19
 
19
int unzip(const char *zipfile) {
20
int unzip(const char *zipfile) {
Line 21... Line 22...
21
  FILE *fd;
22
  FILE *fd;
22
  int r = 0;
23
  int r = 0;
23
 
24
 
24
  fd = fopen(zipfile, "rb");
25
  fd = fopen(zipfile, "rb");
25
  if (fd == NULL) {
26
  if (fd == NULL) {
26
    kitten_puts(10, 1, "ERROR: Failed to open the archive file");
27
    puts(svarlang_str(10, 1)); /* "ERROR: Failed to open the archive file" */
27
    return(1);
28
    return(1);
28
  }
29
  }
29
 
30
 
30
  zlist = zip_listfiles(fd);
31
  zlist = zip_listfiles(fd);
31
  if (zlist == NULL) {
32
  if (zlist == NULL) {
32
    kitten_puts(10, 2, "ERROR: Invalid ZIP archive");
33
    puts(svarlang_str(10, 2)); /* "ERROR: Invalid ZIP archive" */
33
    fclose(fd);
34
    fclose(fd);
34
    return(-1);
35
    return(-1);
35
  }
36
  }
36
 
37
 
37
  /* 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
Line 45... Line 46...
45
    mkpath(znode->filename);
46
    mkpath(znode->filename);
46
    /* if a dir, we good already */
47
    /* if a dir, we good already */
47
    if (znode->flags == ZIP_FLAG_ISADIR) goto OK;
48
    if (znode->flags == ZIP_FLAG_ISADIR) goto OK;
48
    /* file already exists? */
49
    /* file already exists? */
49
    if (fileexists(znode->filename) != 0) {
50
    if (fileexists(znode->filename) != 0) {
50
      kitten_puts(10, 3, "ERROR: File already exists");
51
      puts(svarlang_str(10, 3)); /* "ERROR: File already exists" */
51
      r = 1;
52
      r = 1;
52
      continue;
53
      continue;
53
    }
54
    }
54
    /* uncompress */
55
    /* uncompress */
55
    zres = zip_unzip(fd, znode, znode->filename);
56
    zres = zip_unzip(fd, znode, znode->filename);
Line 57... Line 58...
57
      kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
58
      kitten_printf(10, 4, "ERROR: unzip failure (%d)", zres);
58
      puts("");
59
      puts("");
59
      continue;
60
      continue;
60
    }
61
    }
61
    OK:
62
    OK:
62
    kitten_puts(10, 0, "OK");
63
    puts(svarlang_str(10, 0)); /* "OK" */
63
  }
64
  }
64
 
65
 
65
  zip_freelist(&zlist);
66
  zip_freelist(&zlist);
66
  fclose(fd);
67
  fclose(fd);
67
  return(r);
68
  return(r);