coral
A C++ library for distributed co-simulation
zip.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_UTIL_ZIP_HPP
11 #define CORAL_UTIL_ZIP_HPP
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <stdexcept>
16 #include <string>
17 
18 #include "boost/filesystem/path.hpp"
19 #include "boost/optional.hpp"
20 
21 #include "coral/config.h"
22 
23 
24 // Forward declarations to avoid dependency on zip.h
25 struct zip;
26 struct zip_file;
27 
28 
29 namespace coral
30 {
31 namespace util
32 {
33 
35 namespace zip
36 {
37 
44 
45 
50 const EntryIndex INVALID_ENTRY_INDEX = 0xFFFFFFFFFFFFFFFFull;
51 
52 
72 class Archive
73 {
74 public:
76  Archive() CORAL_NOEXCEPT;
77 
89  Archive(const boost::filesystem::path& path);
90 
91  // Disable copying.
92  Archive(const Archive&) = delete;
93  Archive& operator=(const Archive&) = delete;
94 
96  Archive(Archive&&) CORAL_NOEXCEPT;
98  Archive& operator=(Archive&&) CORAL_NOEXCEPT;
99 
101  ~Archive() CORAL_NOEXCEPT;
102 
113  void Open(const boost::filesystem::path& path);
114 
120  void Discard() CORAL_NOEXCEPT;
121 
123  bool IsOpen() const CORAL_NOEXCEPT;
124 
132  std::uint64_t EntryCount() const;
133 
148  EntryIndex FindEntry(const std::string& name) const;
149 
162  std::string EntryName(EntryIndex index) const;
163 
179  bool IsDirEntry(EntryIndex index) const;
180 
196  void ExtractAll(
197  const boost::filesystem::path& targetDir) const;
198 
221  boost::filesystem::path ExtractFileTo(
222  EntryIndex index,
223  const boost::filesystem::path& targetDir) const;
224 
225 private:
226  ::zip* m_archive;
227 };
228 
229 
232 {
233 public:
234  // Creates an exception with the given message
235  Exception(const std::string& msg) CORAL_NOEXCEPT;
236 
237  // Creates an exception for the last error for the given archive
238  Exception(::zip* archive) CORAL_NOEXCEPT;
239 
240  // Creates an exception for the last error for the given file
241  Exception(zip_file* file) CORAL_NOEXCEPT;
242 };
243 
244 
245 }}} // namespace
246 #endif // header guard
A class for reading ZIP archives.
Definition: zip.hpp:72
const EntryIndex INVALID_ENTRY_INDEX
An index value that represents an invalid/unknown zip entry.
Definition: zip.hpp:50
bool IsOpen() const CORAL_NOEXCEPT
Returns whether this object refers to an open ZIP archive.
std::string EntryName(EntryIndex index) const
Returns the name of an archive entry.
Exception class for errors that occur while dealing with ZIP files.
Definition: zip.hpp:231
Archive() CORAL_NOEXCEPT
Default constructor; does not associate the object with an archive file.
STL class.
boost::filesystem::path ExtractFileTo(EntryIndex index, const boost::filesystem::path &targetDir) const
Extracts a single file from the archive, placing it in a specific target directory.
void Open(const boost::filesystem::path &path)
Opens a ZIP archive.
~Archive() CORAL_NOEXCEPT
Destructor; calls Discard().
void ExtractAll(const boost::filesystem::path &targetDir) const
Extracts the entire contents of the archive.
Definition: variable_io.hpp:28
void Discard() CORAL_NOEXCEPT
Closes the archive.
std::uint64_t EntryIndex
A type for numeric zip entry indices.
Definition: zip.hpp:43
bool IsDirEntry(EntryIndex index) const
Returns whether an archive entry is a directory.
EntryIndex FindEntry(const std::string &name) const
Finds an entry by name.
std::uint64_t EntryCount() const
Returns the number of entries in the archive.