23 #ifdef CRN_USING_LIBZIP
31 #if LIBZIP_VERSION_MINOR == 10
32 #define zip_dir_add(arch, name, flags) zip_add_dir(arch, name)
33 #define zip_file_add(arch, name, source, flags) zip_add(arch, name, source)
34 using zip_flags_t = int;
35 #define ZIP_FL_OVERWRITE 0
36 #define ZIP_FL_ENC_UTF_8 0
37 #define ZIP_TRUNCATE 0
44 Impl(
const Path &f,
bool c):zipfile(nullptr),autosave(
true),saved(
false),fname(f),check(c) { }
51 #if LIBZIP_VERSION_MINOR != 10
55 int res = zip_close(zipfile);
67 #if LIBZIP_VERSION_MINOR != 10
77 #if LIBZIP_VERSION_MINOR == 10
102 Zip Zip::NewFromFile(
const Path &fname,
bool check_consistency)
104 return Zip(fname,
false,
false, check_consistency);
117 Zip Zip::Create(
const Path &fname,
bool overwrite,
bool check_consistency)
119 return Zip(fname,
true, overwrite, check_consistency);
132 Zip::Zip(
const Path &fname,
bool create,
bool overwrite,
bool check_consistency):
133 pimpl(std::make_unique<Impl>(fname, check_consistency))
140 flags |= ZIP_TRUNCATE;
143 pimpl->saved =
false;
145 if (check_consistency)
146 flags |= ZIP_CHECKCONS;
148 zip *zfile = zip_open(fname.
CStr(), flags, &error);
150 pimpl->zipfile = zfile;
180 Zip::Zip(Zip &&z) noexcept:
181 pimpl(std::move(z.pimpl))
185 Zip& Zip::operator=(Zip &&z) noexcept
187 pimpl = std::move(z.pimpl);
197 int res = zip_close(pimpl->zipfile);
198 pimpl->zipfile =
nullptr;
211 zip *zfile = zip_open(pimpl->fname.CStr(), 0, &error);
213 pimpl->zipfile = zfile;
245 void Zip::SetAutoSave(
bool autosave)
247 pimpl->autosave = autosave;
257 void Zip::AddFile(
const StringUTF8 &path,
const void *data,
size_t len,
bool overwrite)
259 zip_flags_t flags = ZIP_FL_ENC_UTF_8;
261 flags |= ZIP_FL_OVERWRITE;
262 zip_source *source = zip_source_buffer(pimpl->zipfile, data, len, 0);
265 zip_int64_t res = zip_file_add(pimpl->zipfile, path.
CStr(), source, flags);
268 zip_source_free(source);
279 void Zip::AddFile(
const StringUTF8 &path,
const Path &original_file,
bool overwrite)
281 zip_flags_t flags = ZIP_FL_ENC_UTF_8;
283 flags |= ZIP_FL_OVERWRITE;
284 zip_source *source = zip_source_file(pimpl->zipfile, original_file.
CStr(), 0, 0);
287 zip_int64_t res = zip_file_add(pimpl->zipfile, path.
CStr(), source, flags);
290 zip_source_free(source);
303 struct zip_stat fstat;
304 if (zip_stat(pimpl->zipfile, path.
CStr(), ZIP_STAT_SIZE, &fstat) == -1)
308 zip_file *f = zip_fopen(pimpl->zipfile, path.
CStr(), 0);
314 zip_int64_t res = zip_fread(f, &(str[0]), fstat.size);
329 void Zip::AddDirectory(
const StringUTF8 &path)
331 if (zip_dir_add(pimpl->zipfile, path.
CStr(), ZIP_FL_ENC_UTF_8) == -1)
342 struct zip_stat fstat;
343 if (zip_stat(pimpl->zipfile, path.
CStr(), ZIP_STAT_NAME, &fstat) == -1)
349 #endif// CRN_USING_LIBZIP
const char * CStr() const noexcept
Conversion to UTF8 cstring.
A convenience class for file paths.
A character string class.