12 #include <boost/iostreams/stream.hpp> 13 #include <boost/iostreams/stream_buffer.hpp> 14 #include <boost/iostreams/categories.hpp> 16 #include <boost/filesystem/path.hpp> 17 #include <boost/filesystem/operations.hpp> 19 #include <boost/date_time/posix_time/posix_time_types.hpp> 20 #include <boost/date_time/posix_time/conversion.hpp> 22 #include <fwCore/exceptionmacros.hpp> 24 #include "minizip/zip.h" 25 #include "fwZip/WriteZipArchive.hpp" 26 #include "fwZip/exception/Write.hpp" 34 zipFile openWriteZipArchive( const ::boost::filesystem::path &archive )
36 int append = (::boost::filesystem::exists(archive)) ? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE;
37 zipFile zip = zipOpen(archive.string().c_str(), append);
39 FW_RAISE_EXCEPTION_IF(
53 std::streamsize openFile(zipFile zipDescriptor, const ::boost::filesystem::path &path)
55 const std::string extension = path.extension().string();
56 int compressLevel = Z_DEFAULT_COMPRESSION;
57 if(extension ==
".raw")
59 compressLevel = Z_BEST_SPEED;
61 else if(extension ==
".raw.gz")
63 compressLevel = Z_NO_COMPRESSION;
69 ::boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
70 std::tm ptm = ::boost::posix_time::to_tm(now);
72 zfi.tmz_date.tm_year = ptm.tm_year;
73 zfi.tmz_date.tm_mon = ptm.tm_mon;
74 zfi.tmz_date.tm_mday = ptm.tm_mday;
75 zfi.tmz_date.tm_hour = ptm.tm_hour;
76 zfi.tmz_date.tm_min = ptm.tm_min;
77 zfi.tmz_date.tm_sec = ptm.tm_sec;
79 std::streamsize nRet = zipOpenNewFileInZip(zipDescriptor,
80 path.generic_string().c_str(),
98 typedef char char_type;
99 typedef ::boost::iostreams::sink_tag category;
101 ZipSink( const ::boost::filesystem::path &archive, const ::boost::filesystem::path &path,
102 const std::string comment ) :
104 openWriteZipArchive(archive),
105 [comment](zipFile zipDescriptor)
107 zipClose(zipDescriptor, comment.c_str());
112 std::streamsize nRet = openFile(m_zipDescriptor.get(), m_path);
113 FW_RAISE_EXCEPTION_IF(
115 "' in archive '"+ archive.string() +
"'."),
119 std::streamsize write(
const char* s, std::streamsize n)
121 std::streamsize nRet = zipWriteInFileInZip(m_zipDescriptor.get(), s,
static_cast< unsigned int >(n));
122 FW_RAISE_EXCEPTION_IF(
124 +
":" + m_path.string() +
"'."),
130 SPTR(
void) m_zipDescriptor;
131 ::boost::filesystem::path m_archive;
132 ::boost::filesystem::path m_path;
153 WriteZipArchive::~WriteZipArchive()
161 SPTR(::boost::iostreams::stream<ZipSink>) os
162 = std::make_shared< ::boost::iostreams::stream<ZipSink> >(m_archive, path, m_comment);
170 std::ifstream is(sourceFile.string().c_str(), std::ios::binary);
171 FW_RAISE_EXCEPTION_IF(::
fwZip::exception::Write(
"Source file '" + sourceFile.string() +
"' cannot be opened."),
185 zipFile zipDescriptor = openWriteZipArchive(m_archive);
186 const std::streamsize nRet = openFile(zipDescriptor, path);
188 zipCloseFileInZip(zipDescriptor);
189 zipClose(zipDescriptor, m_comment.c_str());
191 return nRet == ZIP_OK;
FWZIP_API bool createDir(const ::boost::filesystem::path &path) override
Creates a folder in archive.
FWZIP_API WriteZipArchive(const ::boost::filesystem::path &archive)
Constructors. Initializes archive path.
FWZIP_APIconst::boost::filesystem::path getArchivePath() const override
Returns archive path.
This class defines functions to write a file in a zip archive.
FWZIP_API std::shared_ptr< std::ostream > createFile(const ::boost::filesystem::path &path) override
Creates a new file entry in archive and returns output stream for this file.
FWZIP_API void putFile(const ::boost::filesystem::path &sourceFile, const ::boost::filesystem::path &path) override
Writes source file in archive.
The namespace fwZip provides IO for compress/uncompress .zip files using zlib .