Mercurial > hg > orthanc
view Core/Compression/ZipWriter.cpp @ 543:fe796b053863 laaw
conditional
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 13 Sep 2013 11:10:58 +0200 |
parents | bdd72233b105 |
children | 3f27814104f7 |
line wrap: on
line source
/** * Orthanc - A Lightweight, RESTful DICOM Store * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, * Belgium * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * In addition, as a special exception, the copyright holders of this * program give permission to link the code of its release with the * OpenSSL project's "OpenSSL" library (or with modified versions of it * that use the same license as the "OpenSSL" library), and distribute * the linked executables. You must obey the GNU General Public License * in all respects for all of the code used other than "OpenSSL". If you * modify file(s) with this exception, you may extend this exception to * your version of the file(s), but you are not obligated to do so. If * you do not wish to do so, delete this exception statement from your * version. If you delete this exception statement from all source files * in the program, then also delete it here. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. **/ #include "ZipWriter.h" #include "../../Resources/minizip/zip.h" #include <boost/date_time/posix_time/posix_time.hpp> #include "../OrthancException.h" static void PrepareFileInfo(zip_fileinfo& zfi) { memset(&zfi, 0, sizeof(zfi)); using namespace boost::posix_time; ptime now = second_clock::local_time(); boost::gregorian::date today = now.date(); ptime midnight(today); time_duration sinceMidnight = now - midnight; zfi.tmz_date.tm_sec = sinceMidnight.seconds(); // seconds after the minute - [0,59] zfi.tmz_date.tm_min = sinceMidnight.minutes(); // minutes after the hour - [0,59] zfi.tmz_date.tm_hour = sinceMidnight.hours(); // hours since midnight - [0,23] // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_day.html zfi.tmz_date.tm_mday = today.day(); // day of the month - [1,31] // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_month.html zfi.tmz_date.tm_mon = today.month() - 1; // months since January - [0,11] // http://www.boost.org/doc/libs/1_35_0/doc/html/boost/gregorian/greg_year.html zfi.tmz_date.tm_year = today.year(); // years - [1980..2044] } namespace Orthanc { struct ZipWriter::PImpl { zipFile file_; }; ZipWriter::ZipWriter() : pimpl_(new PImpl) { compressionLevel_ = 6; hasFileInZip_ = false; pimpl_->file_ = NULL; } ZipWriter::~ZipWriter() { Close(); } void ZipWriter::Close() { if (IsOpen()) { zipClose(pimpl_->file_, "Created by Orthanc"); pimpl_->file_ = NULL; hasFileInZip_ = false; } } bool ZipWriter::IsOpen() const { return pimpl_->file_ != NULL; } void ZipWriter::Open() { if (IsOpen()) { return; } if (path_.size() == 0) { throw OrthancException("Please call SetOutputPath() before creating the file"); } hasFileInZip_ = false; pimpl_->file_ = zipOpen64(path_.c_str(), APPEND_STATUS_CREATE); if (!pimpl_->file_) { throw OrthancException(ErrorCode_CannotWriteFile); } } void ZipWriter::SetOutputPath(const char* path) { Close(); path_ = path; } void ZipWriter::SetCompressionLevel(uint8_t level) { if (level >= 10) { throw OrthancException("ZIP compression level must be between 0 (no compression) and 9 (highest compression"); } compressionLevel_ = level; } void ZipWriter::OpenFile(const char* path) { Open(); zip_fileinfo zfi; PrepareFileInfo(zfi); if (zipOpenNewFileInZip64(pimpl_->file_, path, &zfi, NULL, 0, NULL, 0, "", // Comment Z_DEFLATED, compressionLevel_, 1) != 0) { throw OrthancException(ErrorCode_CannotWriteFile); } hasFileInZip_ = true; } void ZipWriter::Write(const std::string& data) { if (data.size()) { Write(&data[0], data.size()); } } void ZipWriter::Write(const char* data, size_t length) { if (!hasFileInZip_) { throw OrthancException("Call first OpenFile()"); } const size_t maxBytesInAStep = std::numeric_limits<int32_t>::max(); while (length > 0) { int bytes = static_cast<int32_t>(length <= maxBytesInAStep ? length : maxBytesInAStep); if (zipWriteInFileInZip(pimpl_->file_, data, bytes)) { throw OrthancException(ErrorCode_CannotWriteFile); } data += bytes; length -= bytes; } } }