NRP Core  1.4.1
zip_container.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef ZIP_CONTAINER_H
23 #define ZIP_CONTAINER_H
24 
25 #include <filesystem>
26 #include <string>
27 #include <vector>
28 #include <zip.h>
29 
34 {
39  struct ZipErrorT : public zip_error_t
40  {
44  ZipErrorT();
45 
50  ZipErrorT(int ze);
51 
55  ~ZipErrorT();
56  };
57 
61  struct ZipFileWrapper
62  {
63  ZipFileWrapper(zip_file_t *zFile);
64  ~ZipFileWrapper();
65 
66  operator zip_file_t*();
67 
68  private:
69  zip_file_t *_zFile = nullptr;
70  };
71 
75  static constexpr zip_uint64_t BuffCopySize = 1024;
76 
77  public:
83  ZipContainer(std::string &&data);
84 
90  ZipContainer(std::vector<uint8_t> &&data);
91 
98  ZipContainer(const std::string &path, bool readOnly, bool saveOnDestruct);
99 
103  ~ZipContainer() noexcept;
104 
113  static ZipContainer compressPath(const std::filesystem::path &path, bool keepRelDirStruct = false);
114 
119  std::vector<uint8_t> getCompressedData() const;
120 
126  void extractZipFiles(std::string path) const;
127 
133  void saveToDestination(const std::string &dest) const;
134 
135  private:
139  zip_t *_data;
140 
144  bool _saveOnDesctruct = false;
145 
153  static zip_t *createZip(const void *data, zip_uint64_t length);
154 
162  static zip_t *openZipArchive(const std::string &path, bool readOnly);
163 
170  static void addZipToZip(zip_t *dest, zip_t *src);
171 
172  ZipContainer(zip_t *data, bool saveOnDestruct);
173 };
174 
175 #endif // ZIP_CONTAINER_H
ZipContainer::saveToDestination
void saveToDestination(const std::string &dest) const
Save Archive to storage.
Definition: zip_container.cpp:276
ZipContainer::getCompressedData
std::vector< uint8_t > getCompressedData() const
Get zip archive's compressed data.
Definition: zip_container.cpp:186
ZipContainer::extractZipFiles
void extractZipFiles(std::string path) const
Extract Zip Files and store them under path.
Definition: zip_container.cpp:217
ZipContainer::ZipContainer
ZipContainer(std::string &&data)
Constructor. Takes a string argument. This is mainly used for Pistache data receiving.
Definition: zip_container.cpp:115
ZipContainer::compressPath
static ZipContainer compressPath(const std::filesystem::path &path, bool keepRelDirStruct=false)
Compress files and directories under path.
Definition: zip_container.cpp:151
ZipContainer::~ZipContainer
~ZipContainer() noexcept
Destructor. Will save zip archive if requested.
Definition: zip_container.cpp:128
ZipContainer
Zip Container Structure. Based on libzip.
Definition: zip_container.h:33