FFSM++  1.1.0
French Forest Sector Model ++
zipExampleUsage.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Filename: main.cpp
3 ** Last updated [dd/mm/yyyy]: 01/02/2007
4 **
5 ** Test routine for the Zip and UnZip classed.
6 **
7 ** Copyright (C) 2007 Angius Fabrizio. All rights reserved.
8 **
9 ** This file is part of the OSDaB project (http://osdab.sourceforge.net/).
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.GPL included in the
14 ** packaging of this file.
15 **
16 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
17 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 **
19 ** See the file LICENSE.GPL that came with this software distribution or
20 ** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
21 **
22 **********************************************************************/
23 
24 #include "zip.h"
25 #include "unzip.h"
26 
27 #include <QFile>
28 #include <QFileInfo>
29 
30 #include <QString>
31 #include <QStringList>
32 #include <QList>
33 
34 #include <iostream>
35 #include <iomanip>
36 
37 void invalidCMD();
38 bool decompress(const QString& file, const QString& out, const QString& pwd);
39 bool compress(const QString& zip, const QString& dir, const QString& pwd);
40 bool listFiles(const QString& file, const QString& pwd);
41 
42 using namespace std;
43 
44 int main(int argc, char** argv)
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 }
139 
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 }
148 
149 bool decompress(const QString& file, const QString& out, const QString& pwd)
150 {
151 
152  if (!QFile::exists(file))
153  {
154  cout << "File does not exist." << endl << endl;
155  return false;
156  }
157 
158  UnZip::ErrorCode ec;
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 }
181 
182 bool compress(const QString& zip, const QString& dir, const QString& pwd)
183 {
184  QFileInfo fi(dir);
185  if (!fi.isDir())
186  {
187  cout << "Directory does not exist." << endl << endl;
188  return false;
189  }
190 
191  Zip::ErrorCode ec;
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 }
217 
218 bool listFiles(const QString& file, const QString& pwd)
219 {
220  if (!QFile::exists(file))
221  {
222  cout << "File does not exist." << endl << endl;
223  return false;
224  }
225 
226  UnZip::ErrorCode ec;
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 }
Zip file compression.
Definition: zip.h:45
Definition: zip.h:50
ErrorCode createArchive(const QString &file, bool overwrite=true)
Definition: zip.cpp:268
ErrorCode extractAll(const QString &dirname, ExtractionOptions options=ExtractPaths)
Definition: unzip.cpp:325
ErrorCode
Definition: unzip.h:48
QString filename
Definition: unzip.h:93
void setPassword(const QString &pwd)
Definition: zip.cpp:246
void closeArchive()
Definition: unzip.cpp:226
void setArchiveComment(const QString &comment)
Definition: zip.cpp:317
QList< ZipEntry > entryList() const
Definition: unzip.cpp:289
STL namespace.
quint32 crc32
Definition: unzip.h:98
PKZip 2.0 file decompression. Compatibility with later versions is not ensured as they may use unsupp...
Definition: unzip.h:45
ErrorCode closeArchive()
Definition: zip.cpp:465
bool compress(const QString &zip, const QString &dir, const QString &pwd)
quint32 uncompressedSize
Definition: unzip.h:97
ErrorCode
Definition: zip.h:48
bool encrypted
Definition: unzip.h:105
QString archiveComment() const
Definition: unzip.cpp:231
QString formatError(UnZip::ErrorCode c) const
Definition: unzip.cpp:241
quint32 compressedSize
Definition: unzip.h:96
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
Definition: zip.cpp:333
bool decompress(const QString &file, const QString &out, const QString &pwd)
ErrorCode openArchive(const QString &filename)
Definition: unzip.cpp:190
int main(int argc, char **argv)
bool listFiles(const QString &file, const QString &pwd)
void invalidCMD()
void setPassword(const QString &pwd)
Definition: unzip.cpp:463
QString formatError(ErrorCode c) const
Definition: zip.cpp:475