2019-03-14 18:07:16 +00:00
|
|
|
#ifndef ZIPPER_HPP
|
|
|
|
#define ZIPPER_HPP
|
|
|
|
|
2019-06-04 07:26:33 +00:00
|
|
|
#include <cstdint>
|
2019-03-14 18:07:16 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
// Class for creating zip archives.
|
2019-03-14 18:07:16 +00:00
|
|
|
class Zipper {
|
|
|
|
public:
|
2019-03-15 10:23:27 +00:00
|
|
|
// Three compression levels supported
|
2019-03-14 18:07:16 +00:00
|
|
|
enum e_compression {
|
|
|
|
NO_COMPRESSION,
|
|
|
|
FAST_COMPRESSION,
|
|
|
|
TIGHT_COMPRESSION
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
std::unique_ptr<Impl> m_impl;
|
|
|
|
std::string m_data;
|
|
|
|
std::string m_entry;
|
|
|
|
e_compression m_compression;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// Will blow up in a runtime exception if the file cannot be created.
|
|
|
|
explicit Zipper(const std::string& zipfname,
|
2020-04-23 17:12:07 +00:00
|
|
|
e_compression level = FAST_COMPRESSION);
|
2019-03-14 18:07:16 +00:00
|
|
|
~Zipper();
|
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
// No copies allwed, this is a file resource...
|
2019-03-14 18:07:16 +00:00
|
|
|
Zipper(const Zipper&) = delete;
|
|
|
|
Zipper& operator=(const Zipper&) = delete;
|
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
// Moving is fine.
|
2019-03-15 11:16:05 +00:00
|
|
|
// Zipper(Zipper&&) = default;
|
|
|
|
// Zipper& operator=(Zipper&&) = default;
|
|
|
|
// All becouse of VS2013:
|
|
|
|
Zipper(Zipper &&m);
|
|
|
|
Zipper& operator=(Zipper &&m);
|
2019-03-14 18:07:16 +00:00
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
/// Adding an entry means a file inside the new archive. Name param is the
|
|
|
|
/// name of the new file. To create directories, append a forward slash.
|
2019-03-15 11:16:05 +00:00
|
|
|
/// The previous entry is finished (see finish_entry)
|
2019-03-14 18:07:16 +00:00
|
|
|
void add_entry(const std::string& name);
|
|
|
|
|
2019-03-18 17:02:50 +00:00
|
|
|
/// Add a new binary file entry with an instantly given byte buffer.
|
2019-03-28 15:19:30 +00:00
|
|
|
/// This method throws exactly like finish_entry() does.
|
2020-04-13 10:31:37 +00:00
|
|
|
void add_entry(const std::string& name, const void* data, size_t bytes);
|
2019-03-18 17:02:50 +00:00
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
// Writing data to the archive works like with standard streams. The target
|
|
|
|
// within the zip file is the entry created with the add_entry method.
|
|
|
|
|
|
|
|
// Template taking only arithmetic values, that std::to_string can handle.
|
|
|
|
template<class T> inline
|
|
|
|
typename std::enable_if<std::is_arithmetic<T>::value, Zipper&>::type
|
|
|
|
operator<<(T &&val) {
|
|
|
|
return this->operator<<(std::to_string(std::forward<T>(val)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template applied only for types that std::string can handle for append
|
|
|
|
// and copy. This includes c style strings...
|
|
|
|
template<class T> inline
|
|
|
|
typename std::enable_if<!std::is_arithmetic<T>::value, Zipper&>::type
|
|
|
|
operator<<(T &&val) {
|
|
|
|
if(m_data.empty()) m_data = std::forward<T>(val);
|
|
|
|
else m_data.append(val);
|
2019-03-14 18:07:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
/// Finishing an entry means that subsequent writes will no longer be
|
2019-03-15 11:16:05 +00:00
|
|
|
/// appended to the previous entry. They will be written into the internal
|
|
|
|
/// buffer and ones an entry is added, the buffer will bind to the new entry
|
|
|
|
/// If the buffer was written, but no entry was added, the buffer will be
|
|
|
|
/// cleared after this call.
|
2019-03-28 15:19:30 +00:00
|
|
|
///
|
|
|
|
/// This method will throw a runtime exception if an error occures. The
|
|
|
|
/// entry will still be open (with the data intact) but the state of the
|
|
|
|
/// file is up to minz after the erroneous write.
|
2019-03-15 10:23:27 +00:00
|
|
|
void finish_entry();
|
|
|
|
|
2019-03-28 15:19:30 +00:00
|
|
|
void finalize();
|
2019-10-23 15:10:14 +00:00
|
|
|
|
|
|
|
const std::string & get_filename() const;
|
2019-03-14 18:07:16 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 10:23:27 +00:00
|
|
|
|
2019-03-14 18:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ZIPPER_HPP
|