Go to the documentation of this file.
30 # if !defined(_CRT_SECURE_NO_WARNINGS)
31 # define _CRT_SECURE_NO_WARNINGS
53 typedef struct zip_stat
stat;
122 using source = std::function<
struct zip_source* (
struct zip*)>;
129 std::unique_ptr<
struct zip_file, int (*)(
struct zip_file*)> handle_;
132 file& operator=(
const file&) =
delete;
141 : handle_(
file, zip_fclose)
168 return zip_fread(handle_.get(), data, length);
177 template <
size_t Size>
178 inline int read(
char (&data)[Size]) noexcept
180 return read(data, Size);
195 result.resize(length);
196 auto count =
read(&result[0], length);
201 result.resize(count);
215 return [data = std::move(data)] (
struct zip*
archive) ->
struct zip_source* {
216 auto size = data.size();
217 auto ptr =
static_cast<char*
>(std::malloc(size));
220 throw std::runtime_error(std::strerror(errno));
222 std::memcpy(ptr, data.data(), size);
224 auto src = zip_source_buffer(
archive, ptr, size, 1);
226 if (src ==
nullptr) {
228 throw std::runtime_error(zip_strerror(
archive));
245 return [path = std::move(path), start, length] (
struct zip*
archive) ->
struct zip_source* {
246 auto src = zip_source_file(
archive, path.c_str(), start, length);
249 throw std::runtime_error(zip_strerror(
archive));
319 std::unique_ptr<
struct zip, int (*)(
struct zip *)> handle_;
328 class iterator :
public std::iterator<std::random_access_iterator_tag, libzip::stat> {
356 return archive_->
stat(index_);
431 return iterator(archive_, index_ + inc);
442 return iterator(archive_, index_ - dec);
453 return index_ == other.index_;
464 return index_ != other.index_;
479 return archive_->
stat(index_ + index);
523 : handle_(nullptr, nullptr)
526 struct zip*
archive = zip_open(path.c_str(), flags, &error);
531 zip_error_to_str(buf,
sizeof (buf), error, errno);
533 throw std::runtime_error(buf);
536 handle_ = {
archive, zip_close };
624 auto size = text.size();
625 auto cstr = (size == 0) ?
nullptr : text.c_str();
627 if (zip_file_set_comment(handle_.get(), index, cstr, size, flags) < 0)
628 throw std::runtime_error(zip_strerror(handle_.get()));
641 zip_uint32_t length = 0;
642 auto text = zip_file_get_comment(handle_.get(), index, &length, flags);
645 throw std::runtime_error(zip_strerror(handle_.get()));
647 return std::string(text, length);
658 if (zip_set_archive_comment(handle_.get(),
comment.c_str(),
comment.size()) < 0)
659 throw std::runtime_error(zip_strerror(handle_.get()));
672 auto text = zip_get_archive_comment(handle_.get(), &length, flags);
675 throw std::runtime_error(zip_strerror(handle_.get()));
677 return std::string(text,
static_cast<std::size_t
>(length));
689 return zip_name_locate(handle_.get(), name.c_str(), flags) >= 0;
702 auto index = zip_name_locate(handle_.get(), name.c_str(), flags);
705 throw std::runtime_error(zip_strerror(handle_.get()));
722 if (zip_stat(handle_.get(), name.c_str(), flags, &st) < 0)
723 throw std::runtime_error(zip_strerror(handle_.get()));
740 if (zip_stat_index(handle_.get(), index, flags, &st) < 0)
741 throw std::runtime_error(zip_strerror(handle_.get()));
759 auto src =
source(handle_.get());
760 auto ret = zip_file_add(handle_.get(), name.c_str(), src, flags);
763 zip_source_free(src);
764 throw std::runtime_error(zip_strerror(handle_.get()));
780 auto ret = zip_dir_add(handle_.get(), directory.c_str(), flags);
783 throw std::runtime_error(zip_strerror(handle_.get()));
798 auto src =
source(handle_.get());
800 if (zip_file_replace(handle_.get(), index, src, flags) < 0) {
801 zip_source_free(src);
802 throw std::runtime_error(zip_strerror(handle_.get()));
815 file open(
const std::string& name,
flags_t flags = 0,
const std::string& password =
"")
817 struct zip_file*
file;
819 if (password.size() > 0)
820 file = zip_fopen_encrypted(handle_.get(), name.c_str(), flags, password.c_str());
822 file = zip_fopen(handle_.get(), name.c_str(), flags);
825 throw std::runtime_error(zip_strerror(handle_.get()));
841 struct zip_file*
file;
843 if (password.size() > 0)
844 file = zip_fopen_index_encrypted(handle_.get(), index, flags, password.c_str());
846 file = zip_fopen_index(handle_.get(), index, flags);
849 throw std::runtime_error(zip_strerror(handle_.get()));
864 if (zip_file_rename(handle_.get(), index, name.c_str(), flags) < 0)
865 throw std::runtime_error(zip_strerror(handle_.get()));
878 if (zip_set_file_compression(handle_.get(), index, comp, flags) < 0)
879 throw std::runtime_error(zip_strerror(handle_.get()));
890 if (zip_delete(handle_.get(), index) < 0)
891 throw std::runtime_error(zip_strerror(handle_.get()));
902 return zip_get_num_entries(handle_.get(), flags);
913 if (zip_unchange(handle_.get(), index) < 0)
914 throw std::runtime_error(zip_strerror(handle_.get()));
924 if (zip_unchange_all(handle_.get()) < 0)
925 throw std::runtime_error(zip_strerror(handle_.get()));
935 if (zip_unchange_archive(handle_.get()) < 0)
936 throw std::runtime_error(zip_strerror(handle_.get()));
947 auto cstr = (password.size() > 0) ? password.c_str() :
nullptr;
949 if (zip_set_default_password(handle_.get(), cstr) < 0)
950 throw std::runtime_error(zip_strerror(handle_.get()));
962 if (zip_set_archive_flag(handle_.get(),
flag, value) < 0)
963 throw std::runtime_error(zip_strerror(handle_.get()));
976 auto ret = zip_get_archive_flag(handle_.get(), which, flags);
979 throw std::runtime_error(zip_strerror(handle_.get()));
archive(archive &&other) noexcept=default
Move constructor defaulted.
void set_file_compression(uint64_t index, int32_t comp, uint32_t flags=0)
Set file compression.
Definition: zip.hpp:876
void set_flag(flags_t flag, int value)
Set an archive flag.
Definition: zip.hpp:960
iterator operator++(int) noexcept
Pre increment.
Definition: zip.hpp:388
int64_t add(const source &source, const std::string &name, flags_t flags=0)
Add a file to the archive.
Definition: zip.hpp:757
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
int64_t find(const std::string &name, flags_t flags=0) const
Locate a file on the archive.
Definition: zip.hpp:700
iterator() noexcept=default
Default iterator.
void set_comment(const std::string &comment)
Set the archive comment.
Definition: zip.hpp:656
file(file &&other) noexcept=default
Move constructor defaulted.
iterator operator+(int inc) const noexcept
Increment.
Definition: zip.hpp:429
iterator end() noexcept
Get an iterator to the end.
Definition: zip.hpp:589
std::string read(uint64_t length)
Optimized function for reading all characters with only one allocation.
Definition: zip.hpp:191
const stat * operator->() const noexcept
Access the object.
Definition: zip.hpp:308
iterator operator-(int dec) const noexcept
Decrement.
Definition: zip.hpp:440
void replace(const source &source, uint64_t index, flags_t flags=0)
Replace an existing file in the archive.
Definition: zip.hpp:796
enum zip_source_cmd source_cmd
zip_source_cmd typedef.
Definition: zip.hpp:58
zip_uint32_t uint32_t
zip_uint32_t typedef.
Definition: zip.hpp:98
const_iterator cend() const noexcept
Overloaded function.
Definition: zip.hpp:609
Base iterator class.
Definition: zip.hpp:328
void unchange_all()
Revert all changes.
Definition: zip.hpp:922
zip_int32_t int32_t
zip_int32_t typedef.
Definition: zip.hpp:93
iterator const_iterator
Const random access iterator.
Definition: zip.hpp:512
file(struct zip_file *file) noexcept
Create a File with a zip_file structure.
Definition: zip.hpp:140
File for reading.
Definition: zip.hpp:127
struct zip_stat stat
zip_stat typedef.
Definition: zip.hpp:53
std::string comment(flags_t flags=0) const
Get the archive comment.
Definition: zip.hpp:669
void remove(uint64_t index)
Delete a file from the archive.
Definition: zip.hpp:888
bool operator==(const iterator &other) const noexcept
Compare equality.
Definition: zip.hpp:451
libzip::stat value_type
Iterator conversion to Stat.
Definition: zip.hpp:487
int read(char(&data)[Size]) noexcept
Read some data to a fixed size array.
Definition: zip.hpp:178
void unchange(uint64_t index)
Revert changes on the file.
Definition: zip.hpp:911
libzip::stat stat(uint64_t index, flags_t flags=0) const
Get information about a file.
Definition: zip.hpp:736
Safe wrapper on the struct zip structure.
Definition: zip.hpp:317
source source_buffer(std::string data) noexcept
Add a file to the archive using a binary buffer.
Definition: zip.hpp:213
std::string file_comment(uint64_t index, flags_t flags=0) const
Get a comment from a file.
Definition: zip.hpp:639
void set_default_password(const std::string &password="")
Set the defaut password.
Definition: zip.hpp:945
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
void set_file_comment(uint64_t index, const std::string &text="", flags_t flags=0)
Set a comment on a file.
Definition: zip.hpp:622
bool operator!=(const iterator &other) const noexcept
Compare equality.
Definition: zip.hpp:462
file & operator=(file &&) noexcept=default
Move operator defaulted.
libzip::stat const_reference
Const reference is a copy of Stat.
Definition: zip.hpp:497
archive & operator=(archive &&other) noexcept=default
Move operator defaulted.
file open(uint64_t index, flags_t flags=0, const std::string &password="")
Open a file in the archive.
Definition: zip.hpp:839
bool exists(const std::string &name, flags_t flags=0) const noexcept
Check if a file exists on the archive.
Definition: zip.hpp:687
const_iterator end() const noexcept
Overloaded function.
Definition: zip.hpp:599
void unchange_archive()
Revert changes to archive.
Definition: zip.hpp:933
int64_t num_entries(flags_t flags=0) const noexcept
Get the number of entries in the archive.
Definition: zip.hpp:900
stat_ptr operator->() const
Dereference the iterator.
Definition: zip.hpp:364
std::function< struct zip_source *(struct zip *)> source
Source creation for adding files.
Definition: zip.hpp:122
iterator & operator--() noexcept
Post decrement.
Definition: zip.hpp:402
libzip::stat operator[](int index)
Access a stat information at the specified index.
Definition: zip.hpp:475
libzip::stat reference
Reference is a copy of Stat.
Definition: zip.hpp:492
zip_int8_t int8_t
zip_int8_t typedef.
Definition: zip.hpp:73
archive(const std::string &path, flags_t flags=0)
Open an archive on the disk.
Definition: zip.hpp:522
zip_source_callback source_callback
zip_source_cmd typedef.
Definition: zip.hpp:63
unsigned size_type
Type of difference.
Definition: zip.hpp:507
const_iterator begin() const noexcept
Overloaded function.
Definition: zip.hpp:569
iterator & operator++() noexcept
Post increment.
Definition: zip.hpp:376
iterator operator--(int) noexcept
Pre decrement.
Definition: zip.hpp:414
zip_int16_t int16_t
zip_int16_t typedef.
Definition: zip.hpp:83
stat_ptr(stat stat) noexcept
Constructor.
Definition: zip.hpp:268
const stat & operator*() const noexcept
Get the reference.
Definition: zip.hpp:288
int64_t mkdir(const std::string &directory, flags_t flags=0)
Create a directory in the archive.
Definition: zip.hpp:778
The libzip namespace.
Definition: zip.hpp:48
source source_file(std::string path, uint64_t start=0, int64_t length=-1) noexcept
Add a file to the archive from the disk.
Definition: zip.hpp:243
file open(const std::string &name, flags_t flags=0, const std::string &password="")
Open a file in the archive.
Definition: zip.hpp:815
int read(void *data, uint64_t length) noexcept
Read some data.
Definition: zip.hpp:166
const_iterator cbegin() const noexcept
Overloaded function.
Definition: zip.hpp:579
libzip::stat stat(const std::string &name, flags_t flags=0) const
Get information about a file.
Definition: zip.hpp:718
zip_flags_t flags_t
zip_flags_t typedef.
Definition: zip.hpp:68
zip_uint16_t uint16_t
zip_uint16_t typedef.
Definition: zip.hpp:88
void rename(uint64_t index, const std::string &name, flags_t flags=0)
Rename an existing entry in the archive.
Definition: zip.hpp:862
Wrapper for stat as pointer.
Definition: zip.hpp:258
iterator begin() noexcept
Get an iterator to the beginning.
Definition: zip.hpp:559
stat & operator*() noexcept
Get the reference.
Definition: zip.hpp:278
zip_uint64_t uint64_t
zip_uint64_t_t typedef.
Definition: zip.hpp:108
int flag(flags_t which, flags_t flags=0) const
Get an archive flag.
Definition: zip.hpp:974
stat * operator->() noexcept
Access the object.
Definition: zip.hpp:298