crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Zip.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Zip.hpp
24  *
25  * Namespace for functions for zip compression.
26  *
27  * Created on: Jan 4, 2021
28  * Author: ans
29  */
30 
31 #ifndef DATA_ZIP_HPP_
32 #define DATA_ZIP_HPP_
33 
34 #include "../../Main/Exception.hpp"
35 #include "../../Wrapper/ZipArchive.hpp"
36 
37 #include <zip.h>
38 
39 #include <string> // std::string
40 #include <utility> // std::pair
41 #include <vector> // std::vector
42 
45 
46  /*
47  * DECLARATION
48  */
49 
51  using StringString = std::pair<std::string, std::string>;
52 
55 
56  std::string compress(const std::vector<StringString>& fileContents);
57  std::vector<StringString> decompress(const std::string& compressedContent);
58 
59  /*
60  * CLASS FOR ZIP EXCEPTIONS
61  */
62 
64 
71 
72  /*
73  * IMPLEMENTATION
74  */
75 
77 
88  inline std::string compress(const std::vector<StringString>& fileContents) {
89  // create ZIP archive
90  Wrapper::ZipArchive archive;
91 
92  if(!archive.valid()) {
93  throw Exception("Zip::compress(): " + archive.getError());
94  }
95 
96  // add files
97  for(const auto& fileContent : fileContents) {
98  if(!archive.addFile(fileContent.first, fileContent.second, false)) {
99  throw Exception("Zip::compress(): " + archive.getError());
100  }
101  }
102 
103  // close archive and dump compressed content
104  std::string result;
105 
106  archive.close(result);
107 
108  return result;
109  }
110 
112 
126  inline std::vector<StringString> decompress(const std::string& compressed) {
127  if(compressed.empty()) {
128  return {};
129  }
130 
131  //TODO Not implemented yet.
132 
133  return {};
134  }
135 
136 } /* namespace crawlservpp::Data::Compression::Zip */
137 
138 #endif /* DATA_ZIP_HPP_ */
std::vector< StringString > decompress(const std::string &compressedContent)
Decompresses zip-compressed content.
Definition: Zip.hpp:126
Class for zip exceptions.
Definition: Zip.hpp:70
bool valid() const noexcept
Checks whether the underlying archive is valid.
Definition: ZipArchive.hpp:194
Namespace for compressing and decompressing zip.
Definition: Zip.hpp:44
bool addFile(const std::string &fileName, const std::string &content, bool overwrite)
Adds a file to the archive.
Definition: ZipArchive.hpp:270
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
std::string compress(const std::vector< StringString > &fileContents)
Compresses files using zip.
Definition: Zip.hpp:88
RAII wrapper for ZIP archives used by libzip.
Definition: ZipArchive.hpp:65
std::pair< std::string, std::string > StringString
A pair of strings.
Definition: Zip.hpp:51
std::string getError()
Get the last occurred error as string.
Definition: ZipArchive.hpp:205
void close() noexcept
Closes the underlying archive if necessary.
Definition: ZipArchive.hpp:319