FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
zipExampleUsage.cpp File Reference
#include "zip.h"
#include "unzip.h"
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QStringList>
#include <QList>
#include <iostream>
#include <iomanip>
Include dependency graph for zipExampleUsage.cpp:

Go to the source code of this file.

Functions

void invalidCMD ()
 
bool decompress (const QString &file, const QString &out, const QString &pwd)
 
bool compress (const QString &zip, const QString &dir, const QString &pwd)
 
bool listFiles (const QString &file, const QString &pwd)
 
int main (int argc, char **argv)
 

Function Documentation

◆ compress()

bool compress ( const QString &  zip,
const QString &  dir,
const QString &  pwd 
)

Definition at line 182 of file zipExampleUsage.cpp.

183{
184 QFileInfo fi(dir);
185 if (!fi.isDir())
186 {
187 cout << "Directory does not exist." << endl << endl;
188 return false;
189 }
190
192 Zip uz;
193
194 ec = uz.createArchive(zip);
195 if (ec != Zip::Ok)
196 {
197 cout << "Unable to create archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
198 return false;
199 }
200
201 uz.setPassword(pwd);
202 ec = uz.addDirectory(dir);
203 if (ec != Zip::Ok)
204 {
205 cout << "Unable to add directory: " << uz.formatError(ec).toAscii().data() << endl << endl;
206 }
207
208 uz.setArchiveComment("This archive has been created using OSDaB Zip (http://osdab.sourceforge.net/).");
209
210 if (uz.closeArchive() != Zip::Ok)
211 {
212 cout << "Unable to close the archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
213 }
214
215 return ec == Zip::Ok;
216}
Zip file compression.
Definition zip.h:46
void setPassword(const QString &pwd)
Definition zip.cpp:246
ErrorCode createArchive(const QString &file, bool overwrite=true)
Definition zip.cpp:268
ErrorCode
Definition zip.h:49
@ Ok
Definition zip.h:50
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition zip.cpp:333
QString formatError(ErrorCode c) const
Definition zip.cpp:475
void setArchiveComment(const QString &comment)
Definition zip.cpp:317
ErrorCode closeArchive()
Definition zip.cpp:465

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decompress()

bool decompress ( const QString &  file,
const QString &  out,
const QString &  pwd 
)

Definition at line 149 of file zipExampleUsage.cpp.

150{
151
152 if (!QFile::exists(file))
153 {
154 cout << "File does not exist." << endl << endl;
155 return false;
156 }
157
159 UnZip uz;
160
161 if (!pwd.isEmpty())
162 uz.setPassword(pwd);
163
164 ec = uz.openArchive(file);
165 if (ec != UnZip::Ok)
166 {
167 cout << "Failed to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
168 return false;
169 }
170
171 ec = uz.extractAll(out);
172 if (ec != UnZip::Ok)
173 {
174 cout << "Extraction failed: " << uz.formatError(ec).toAscii().data() << endl << endl;
175 uz.closeArchive();
176 return false;
177 }
178
179 return true;
180}
PKZip 2.0 file decompression. Compatibility with later versions is not ensured as they may use unsupp...
Definition unzip.h:46
void setPassword(const QString &pwd)
Definition unzip.cpp:463
void closeArchive()
Definition unzip.cpp:226
ErrorCode openArchive(const QString &filename)
Definition unzip.cpp:190
QString formatError(UnZip::ErrorCode c) const
Definition unzip.cpp:241
ErrorCode
Definition unzip.h:49
@ Ok
Definition unzip.h:50
ErrorCode extractAll(const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition unzip.cpp:325

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ invalidCMD()

void invalidCMD ( )

Definition at line 140 of file zipExampleUsage.cpp.

141{
142 cout << "Invalid command line. Usage:" << endl;
143 cout << "Compression: zip [-p PWD] DIRECTORY" << endl;
144 cout << "List files: zip -l [-p PWD] ZIPFILE" << endl;
145 cout << "Decompression: zip -d [-p PWD] ZIPFILE OUTPUT_DIR" << endl << endl;
146 exit(-1);
147}

Referenced by main().

Here is the caller graph for this function:

◆ listFiles()

bool listFiles ( const QString &  file,
const QString &  pwd 
)

Definition at line 218 of file zipExampleUsage.cpp.

219{
220 if (!QFile::exists(file))
221 {
222 cout << "File does not exist." << endl << endl;
223 return false;
224 }
225
227 UnZip uz;
228
229 if (!pwd.isEmpty())
230 uz.setPassword(pwd);
231
232 ec = uz.openArchive(file);
233 if (ec != UnZip::Ok)
234 {
235 cout << "Unable to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
236 return false;
237 }
238
239 QString comment = uz.archiveComment();
240 if (!comment.isEmpty())
241 cout << "Archive comment: " << comment.toAscii().data() << endl << endl;
242
243 QList<UnZip::ZipEntry> list = uz.entryList();
244 if (list.isEmpty())
245 {
246 cout << "Empty archive.";
247 }
248 else
249 {
250 cout.setf(ios::left);
251 cout << setw(40) << "Filename";
252 cout.unsetf(ios::left);
253 cout << setw(10) << "Size" << setw(10) << "Ratio" << setw(10) << "CRC32" << endl;
254 cout.setf(ios::left);
255 cout << setw(40) << "--------";
256 cout.unsetf(ios::left);
257 cout << setw(10) << "----" << setw(10) << "-----" << setw(10) << "-----" << endl;
258
259 for (int i = 0; i < list.size(); ++i)
260 {
261 const UnZip::ZipEntry& entry = list.at(i);
262
263 double ratio = entry.uncompressedSize == 0 ? 0 : 100 - (double) entry.compressedSize * 100 / (double) entry.uncompressedSize;
264
265 QString ratioS = QString::number(ratio, 'f', 2).append("%");
266 QString crc;
267 crc = crc.sprintf("%X", entry.crc32).rightJustified(8, '0');
268 QString file = entry.filename;
269 int idx = file.lastIndexOf("/");
270 if (idx >= 0 && idx != file.length()-1)
271 file = file.right(file.length() - idx - 1);
272 file = file.leftJustified(40, ' ', true);
273
274 if (entry.encrypted)
275 file.append("*");
276
277 cout << setw(40) << file.toAscii().data() << setw(10) << entry.uncompressedSize << setw(10) << ratioS.toAscii().data() << setw(10) << crc.toAscii().data() << endl;
278 }
279 }
280
281 uz.closeArchive();
282 return true;
283}
QList< ZipEntry > entryList() const
Definition unzip.cpp:289
QString archiveComment() const
Definition unzip.cpp:231
quint32 uncompressedSize
Definition unzip.h:97
quint32 crc32
Definition unzip.h:98
bool encrypted
Definition unzip.h:105
quint32 compressedSize
Definition unzip.h:96
QString filename
Definition unzip.h:93

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 44 of file zipExampleUsage.cpp.

45{
46 if (argc < 3)
47 {
48 cout << "Test routine for the OSDaB Project Zip/UnZip classes" << endl << endl;
49 cout << "Compression: zip [-p PWD] ZIPFILE DIRECTORY" << endl;
50 cout << "List files: zip -l [-p PWD] ZIPFILE" << endl;
51 cout << "Decompression: zip -d [-p PWD] ZIPFILE OUTPUT_DIR" << endl << endl;
52 cout << "(C) 2007 Angius Fabrizio\nLicensed under the terms of the GNU GPL Version 2 or later" << endl;
53 return -1;
54 }
55
56 QString fname;
57 QString dname;
58 QString pwd;
59
60 bool resOK = true;
61
62 if (strlen(argv[1]) == 2 && argv[1][0] == '-')
63 {
64 switch (argv[1][1])
65 {
66 case 'd':
67 {
68 if (argc >= 6)
69 {
70 if (strcmp(argv[2], "-p") == 0)
71 {
72 pwd = QString(argv[3]);
73 fname = QString(argv[4]);
74 dname = QString(argv[5]);
75 }
76 else invalidCMD();
77 }
78 else if (argc >= 4)
79 {
80 fname = QString(argv[2]);
81 dname = QString(argv[3]);
82 }
83 else invalidCMD();
84
85 resOK = decompress(fname, dname, pwd);
86 }
87 break;
88 case 'l':
89 {
90 if (argc >= 5)
91 {
92 if (strcmp(argv[2], "-p") == 0)
93 {
94 pwd = QString(argv[3]);
95 fname = QString(argv[4]);
96 }
97 else invalidCMD();
98 }
99 else if (argc >= 3)
100 {
101 fname = QString(argv[2]);
102 }
103 else invalidCMD();
104
105 resOK = listFiles(fname, pwd);
106 }
107 break;
108 case 'p':
109 {
110 if (argc >= 5)
111 {
112 pwd = QString(argv[2]);
113 fname = QString(argv[3]);
114 dname = QString(argv[4]);
115 }
116 else invalidCMD();
117
118 resOK = compress(fname, dname, pwd);
119 }
120 break;
121 default: invalidCMD();
122 }
123 }
124 else
125 {
126 // no parameters -- compress directly
127 resOK = compress(QString(argv[1]), QString(argv[2]), 0);
128 }
129
130
131 if (!resOK)
132 {
133 cout << "Sorry, some error occurred!" << endl;
134 return -1;
135 }
136
137 return 0;
138}
bool listFiles(const QString &file, const QString &pwd)
bool decompress(const QString &file, const QString &out, const QString &pwd)
void invalidCMD()
bool compress(const QString &zip, const QString &dir, const QString &pwd)
Here is the call graph for this function: