2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 static int __append_file(zipFile zf, const char *src, const char *dest)
30 zip_fileinfo zfi = { 0 };
40 fp = fopen(src, "rb");
43 ret = zipOpenNewFileInZip(zf, dest, &zfi,
44 NULL, 0, NULL, 0, NULL,
45 Z_DEFLATED, Z_DEFAULT_COMPRESSION);
46 retvm_if(ret != ZIP_OK, -1, "failed to zipOpenNewFileInZip() for %s", dest);
51 size_read = (int)fread(buf, 1, 1024, fp);
52 if (size_read < 1024) {
54 _E("error in reading %s", dest);
60 ret = zipWriteInFileInZip(zf, buf, size_read);
62 _E("error in writing %s in the zipfile", dest);
64 } while ((ret == ZIP_OK) && (size_read > 0));
66 zipCloseFileInZip(zf);
73 ttd_zip *ttd_zip_open(const char *zip_name)
77 retv_if(!zip_name, NULL);
79 zip = g_try_malloc(sizeof(ttd_zip));
81 zf = zipOpen(zip_name, APPEND_STATUS_CREATE);
83 _E("failed to open zip file - %s", zip_name);
89 zip->zip_name = g_strdup(zip_name);
94 void ttd_zip_close(ttd_zip *zip)
98 _D("closing file[%s]", zip->zip_name);
101 zipClose(zip->zf, NULL);
103 g_free(zip->zip_name);
107 const char *ttd_zip_get_name(ttd_zip *zip)
111 return zip->zip_name;
114 int ttd_zip_append_file(
115 ttd_zip *zip, const char *src_name, const char *dest_name)
117 gboolean is_dir = FALSE;
121 retv_if(!src_name, -1);
122 retv_if(!dest_name, -1);
124 is_dir = g_file_test(src_name, G_FILE_TEST_IS_DIR);
126 const char *name = NULL;
127 unsigned int count = 0;
128 char *dir_name = NULL;
130 GError *error = NULL;
132 _D("appending directory[%s] to [%s]", src_name, zip->zip_name);
133 gdir = g_dir_open(src_name, 0, &error);
135 _E("failed to open directory[%s] - %s", src_name, error->message);
140 while ((name = g_dir_read_name(gdir))) {
145 s_name = g_build_filename(src_name, name, NULL);
146 d_name = g_build_filename(dest_name, name, NULL);
148 ret_d = ttd_zip_append_file(zip, s_name, d_name);
158 _W("nothing in the dir[%s]", src_name);
160 _D("appending file[%s] to [%s]", src_name, zip->zip_name);
161 ret = __append_file(zip->zf, src_name, dest_name);
163 _E("failed to append file[%s] to [%s]", src_name, zip->zip_name);