]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/ZIP/src/fmi_zip_zip.c
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / ZIP / src / fmi_zip_zip.c
1 /*
2     Copyright (C) 2012 Modelon AB
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the BSD style license.
6
7      This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     FMILIB_License.txt file for more details.
11
12     You should have received a copy of the FMILIB_License.txt file
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <minizip.h>
19
20 #include <JM/jm_types.h>
21 #include <JM/jm_callbacks.h>
22 #include <JM/jm_portability.h>
23
24 jm_status_enu_t fmi_zip_zip(const char* zip_file_path, int n_files_to_zip, const char** files_to_zip, jm_callbacks* callbacks)
25 {
26         /* A call to minizip may change the current directory and therefore we must change it back */
27         char cd[FILENAME_MAX];
28
29 #define N_BASIC_ARGS 4
30         int argc;
31         char** argv;
32         int k;
33         int status;
34
35         /* Temporary save the current directory */
36         if (jm_portability_get_current_working_directory(cd, sizeof(cd) / sizeof(char)) == jm_status_error) {
37                 jm_log(callbacks, "UNZIP", jm_log_level_error, "Could not get Current Directory");\r
38                 return jm_status_error;
39         }
40
41         argc = N_BASIC_ARGS + n_files_to_zip;
42         argv = callbacks->calloc(sizeof(char*), argc);  
43         /* Failed to allocate memory, return error */
44         if (argv == NULL) {
45                 callbacks->logger(NULL, "FMIZIP", jm_log_level_error, "Failed to allocate memory.");
46                 return jm_status_error; 
47         }
48
49         /* Input arguments to the corresponding minizip main() function call */
50         /*
51         Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\r
52 \r
53         -o  Overwrite existing file.zip\r
54         -a  Append to existing file.zip\r
55         -0  Store only\r
56         -1  Compress faster\r
57         -9  Compress better\r
58 \r
59         -j  exclude path. store only the file name.
60         */
61         argv[0]="minizip";
62         argv[1]="-o";
63         argv[2]="-1";
64         argv[3]=(char*)zip_file_path;
65
66         /* Append the input argument list with the files to unzip */
67         jm_log_info(callbacks, "FMIZIP", "Will compress following files: \n");
68         for (k = 0; k < n_files_to_zip; k++) {
69                 jm_log_info(callbacks, "FMIZIP", "\t%s\n", files_to_zip[k]);
70                 argv[N_BASIC_ARGS + k] = (char*)files_to_zip[k];
71         }
72
73         /* Zip */
74         status = minizip(argc, (char**)argv);
75
76         /* Free allocated memory */
77         callbacks->free(argv);
78
79         /* Reset the current directory */
80         if (jm_portability_set_current_working_directory(cd) == jm_status_error) {\r
81                 jm_log(callbacks, "UNZIP", jm_log_level_warning, "Could not change back Current Directory");\r
82                 return jm_status_warning;\r
83         }
84
85         /* Return error status */
86         if (status == 0) {
87                 return jm_status_success;
88         } else {
89                 return jm_status_error; 
90         }
91 }