fw4spl
WriteDirArchive.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwZip/exception/Write.hpp"
8 
9 #include "minizip/zip.h"
10 #include "fwZip/WriteDirArchive.hpp"
11 
12 #include <fwCore/exceptionmacros.hpp>
13 
14 #include <fstream>
15 
16 
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/operations.hpp>
19 
20 #include <boost/iostreams/stream_buffer.hpp>
21 
22 
23 namespace fwZip
24 {
25 
26 //-----------------------------------------------------------------------------
27 
28 WriteDirArchive::WriteDirArchive( const ::boost::filesystem::path &archive ) :
29  m_archive(archive)
30 {
31  if(!::boost::filesystem::exists(m_archive))
32  {
33  ::boost::filesystem::create_directories(m_archive);
34  }
35 }
36 
37 //-----------------------------------------------------------------------------
38 
40 {
41 }
42 
43 //-----------------------------------------------------------------------------
44 
45 SPTR(std::ostream) WriteDirArchive::createFile(const ::boost::filesystem::path &path)
46 {
47  const ::boost::filesystem::path file = m_archive / path;
48  const ::boost::filesystem::path parentFile = file.parent_path();
49  if(!::boost::filesystem::exists(parentFile))
50  {
51  ::boost::filesystem::create_directories(parentFile);
52  }
53 
54  SPTR(std::ofstream) os = std::make_shared< std::ofstream >();
55  os->open(file.string().c_str(), std::fstream::binary | std::fstream::out | std::fstream::trunc);
56  return os;
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 void WriteDirArchive::putFile(const ::boost::filesystem::path &sourceFile,
62  const ::boost::filesystem::path &destinationFile)
63 {
64  const ::boost::filesystem::path fileDest = m_archive / destinationFile;
65  if (!::boost::filesystem::exists(fileDest))
66  {
67  const ::boost::filesystem::path parentFile = fileDest.parent_path();
68  if(!::boost::filesystem::exists(parentFile))
69  {
70  ::boost::filesystem::create_directories(parentFile);
71  }
72  ::boost::system::error_code err;
73  ::boost::filesystem::create_hard_link( sourceFile, fileDest, err );
74  if (err.value() != 0)
75  {
76  // Use std stream instead of boost:::filesystem::copy_file
77  // because fwZip is build using std=c++11 and using copy_file also requires boost built
78  // with std=c++11 (for now in c++0x).
79  std::string strSource = sourceFile.string();
80  std::string strDest = fileDest.string();
81  std::ifstream src(strSource.c_str(), std::ios::binary);
82  std::ofstream dst(strDest.c_str(), std::ios::binary);
83 
84  dst << src.rdbuf();
85 
86  }
87  }
88 }
89 
90 //-----------------------------------------------------------------------------
91 
92 bool WriteDirArchive::createDir(const ::boost::filesystem::path &path)
93 {
94  return ::boost::filesystem::create_directories(m_archive/path);
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 const ::boost::filesystem::path WriteDirArchive::getArchivePath() const
100 {
101  return m_archive;
102 }
103 
104 
105 }
106 
#define SPTR(_cls_)
This class defines functions to write a file in a file system archive.
FWZIP_API bool createDir(const ::boost::filesystem::path &path) override
Creates a folder in 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 &destinationFile) override
Writes source file in archive. If possible, creates hard link otherwise copy source file in archive...
FWZIP_APIconst::boost::filesystem::path getArchivePath() const override
Returns archive path.
FWZIP_API ~WriteDirArchive()
Destructor. Flush and close last output file stream.
The namespace fwZip provides IO for compress/uncompress .zip files using zlib .
Definition: Read.hpp:14
FWZIP_API WriteDirArchive(const ::boost::filesystem::path &archive)
Constructors. Initializes archive path and creates archive directories if doesn&#39;t exist...