Mercurial > code
changeset 345:c293dbe181c0
Zip: add exists() function
Submitted by: Elias Pipping
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 31 Mar 2015 09:57:37 +0200 |
parents | 5c74a623888c |
children | d235553e47a9 |
files | C++/modules/Zip/ZipArchive.cpp C++/modules/Zip/ZipArchive.h C++/tests/Zip/main.cpp |
diffstat | 3 files changed, 40 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Zip/ZipArchive.cpp Tue Mar 31 08:49:59 2015 +0200 +++ b/C++/modules/Zip/ZipArchive.cpp Tue Mar 31 09:57:37 2015 +0200 @@ -110,7 +110,7 @@ std::string ZipArchive::getFileComment(ZipUint64 index, ZipFlags flags) const { - zip_uint32_t length{}; + zip_uint32_t length = 0; auto text = zip_file_get_comment(m_handle.get(), index, &length, flags); if (text == nullptr) @@ -127,7 +127,7 @@ std::string ZipArchive::getComment(ZipFlags flags) const { - int length{}; + int length = 0; auto text = zip_get_archive_comment(m_handle.get(), &length, flags); if (text == nullptr) @@ -136,7 +136,12 @@ return { text, static_cast<size_t>(length) }; } -ZipInt64 ZipArchive::find(const std::string &name, ZipFlags flags) +bool ZipArchive::exists(const std::string &name, ZipFlags flags) const noexcept +{ + return zip_name_locate(m_handle.get(), name.c_str(), flags) >= 0; +} + +ZipInt64 ZipArchive::find(const std::string &name, ZipFlags flags) const { auto index = zip_name_locate(m_handle.get(), name.c_str(), flags);
--- a/C++/modules/Zip/ZipArchive.h Tue Mar 31 08:49:59 2015 +0200 +++ b/C++/modules/Zip/ZipArchive.h Tue Mar 31 09:57:37 2015 +0200 @@ -554,6 +554,15 @@ std::string getComment(ZipFlags flags = 0) const; /** + * Check if a file exists on the archive. + * + * @param name the name + * @param flags the optional flags + * @return if the file exists + */ + bool exists(const std::string &name, ZipFlags flags = 0) const noexcept; + + /** * Locate a file on the archive. * * @param name the name @@ -561,7 +570,7 @@ * @return the index * @throw std::runtime_error on errors */ - ZipInt64 find(const std::string &name, ZipFlags flags = 0); + ZipInt64 find(const std::string &name, ZipFlags flags = 0) const; /** * Get information about a file.
--- a/C++/tests/Zip/main.cpp Tue Mar 31 08:49:59 2015 +0200 +++ b/C++/tests/Zip/main.cpp Tue Mar 31 09:57:37 2015 +0200 @@ -107,6 +107,23 @@ } } +TEST(Write, notexist) +{ + remove("output.zip"); + + try { + ZipArchive archive("output.zip", ZIP_CREATE); + + /* + * According to the libzip, adding a file that does not exists + * on the disk is not an error. + */ + archive.add(File("file_not_exist"), "FILE"); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + /* -------------------------------------------------------- * Reading * -------------------------------------------------------- */ @@ -139,6 +156,11 @@ } } +TEST_F(ReadingTest, exists) +{ + ASSERT_TRUE(m_archive.exists("README")); +} + TEST_F(ReadingTest, read) { try {