NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
zip++.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2015 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20
21#include "zip++.hpp"
22
24{
25 hZip = NULL;
26 bIsOpen = false;
27}
28
30{
31 if (bIsOpen && hZip != NULL)
32 {
34 hZip = NULL;
35 }
36}
37
38bool Zipfile::open(const std::string& sFilename, int nOpenmode)
39{
40 if (bIsOpen && hZip != NULL)
41 {
43 hZip = NULL;
44 }
45 if (!nOpenmode)
46 hZip = OpenZip(sFilename.c_str(), 0);
47 else
48 hZip = CreateZip(sFilename.c_str(), 0);
49 if (hZip != NULL)
50 {
51 bIsOpen = true;
52 }
53 else
54 bIsOpen = false;
55
56 return bIsOpen;
57}
58
60{
61 if (bIsOpen && hZip != NULL)
62 {
64 hZip = NULL;
65 bIsOpen = false;
66 }
67 return !bIsOpen;
68}
69
70bool Zipfile::addFile(const std::string& sFilename)
71{
72 if (bIsOpen && hZip != NULL)
73 {
74 return !(bool)ZipAdd(hZip, sFilename.c_str(), sFilename.c_str());
75 }
76 return false;
77}
78
80{
81 if (bIsOpen && hZip != NULL)
82 {
83 std::string sZipfilecontent = "";
84 char cBuffer[1024];
85 int i = 0;
86 unsigned long int totalsize = 0;
88 ZRESULT zRes = ZR_MORE;
89
90 while (zRes == ZR_MORE)
91 {
92 zRes = UnzipItem(hZip, i, cBuffer, 1024);
93 unsigned long int bufsize = 1024;
94 if (zRes == ZR_OK)
95 bufsize = zEntry.unc_size-totalsize;
96 totalsize += bufsize;
97 sZipfilecontent.append(cBuffer, bufsize);
98 }
99
100 return sZipfilecontent;
101 }
102 return "";
103}
104
105
106std::string Zipfile::getZipItem(const std::string& sFilename)
107{
108 if (bIsOpen && hZip != NULL)
109 {
110 std::string sFilecontent = "";
111 //wstring wFilecontent = L"";
112 //wchar_t* wBuffer = 0;
113 char* cBuffer = 0;
114 int nIndex = 0;
115 if (FindZipItem(hZip, sFilename.c_str(), true, &nIndex, &zEntry) != ZR_OK)
116 return sFilecontent;
117 cBuffer = new char[zEntry.unc_size];
118 UnzipItem(hZip, nIndex, cBuffer, zEntry.unc_size);
119
120 sFilecontent.append(cBuffer, zEntry.unc_size);
121 delete[] cBuffer;
122
123 //sFilecontent = wstring_convert<codecvt_utf8<wchar_t> >().to_bytes(wFilecontent);
124 return sFilecontent;
125 }
126 return "";
127}
128
bool open(const std::string &sZipFilename, int nOpenmode=0)
Definition: zip++.cpp:38
Zipfile()
Definition: zip++.cpp:23
std::string getZipContent()
Definition: zip++.cpp:79
bool addFile(const std::string &sFilename)
Definition: zip++.cpp:70
bool close()
Definition: zip++.cpp:59
~Zipfile()
Definition: zip++.cpp:29
bool bIsOpen
Definition: zip++.hpp:39
HZIP hZip
Definition: zip++.hpp:37
ZIPENTRY zEntry
Definition: zip++.hpp:38
std::string getZipItem(const std::string &sFilename)
Definition: zip++.cpp:106
long unc_size
Definition: unzip.h:26
ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY *ze)
Definition: unzip.cpp:4664
ZRESULT FindZipItem(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
Definition: unzip.cpp:4685
ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn)
Definition: unzip.cpp:4724
HZIP OpenZip(const TCHAR *fn, const char *password)
Definition: unzip.cpp:4654
#define CloseZip
Definition: unzip.h:208
#define ZR_OK
Definition: unzip.h:103
DWORD ZRESULT
Definition: unzip.h:17
#define ZR_MORE
Definition: unzip.h:112
ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, const TCHAR *fn)
Definition: zip.cpp:3280
HZIP CreateZip(const TCHAR *fn, const char *password)
Definition: zip.cpp:3253