31 #include <QStringList> 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);
44 int main(
int argc,
char** argv)
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;
62 if (strlen(argv[1]) == 2 && argv[1][0] ==
'-')
70 if (strcmp(argv[2],
"-p") == 0)
72 pwd = QString(argv[3]);
73 fname = QString(argv[4]);
74 dname = QString(argv[5]);
80 fname = QString(argv[2]);
81 dname = QString(argv[3]);
92 if (strcmp(argv[2],
"-p") == 0)
94 pwd = QString(argv[3]);
95 fname = QString(argv[4]);
101 fname = QString(argv[2]);
112 pwd = QString(argv[2]);
113 fname = QString(argv[3]);
114 dname = QString(argv[4]);
118 resOK =
compress(fname, dname, pwd);
127 resOK =
compress(QString(argv[1]), QString(argv[2]), 0);
133 cout <<
"Sorry, some error occurred!" << endl;
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;
149 bool decompress(
const QString& file,
const QString& out,
const QString& pwd)
152 if (!QFile::exists(file))
154 cout <<
"File does not exist." << endl << endl;
167 cout <<
"Failed to open archive: " << uz.
formatError(ec).toAscii().data() << endl << endl;
174 cout <<
"Extraction failed: " << uz.
formatError(ec).toAscii().data() << endl << endl;
182 bool compress(
const QString& zip,
const QString& dir,
const QString& pwd)
187 cout <<
"Directory does not exist." << endl << endl;
197 cout <<
"Unable to create archive: " << uz.
formatError(ec).toAscii().data() << endl << endl;
205 cout <<
"Unable to add directory: " << uz.
formatError(ec).toAscii().data() << endl << endl;
208 uz.
setArchiveComment(
"This archive has been created using OSDaB Zip (http://osdab.sourceforge.net/).");
212 cout <<
"Unable to close the archive: " << uz.
formatError(ec).toAscii().data() << endl << endl;
220 if (!QFile::exists(file))
222 cout <<
"File does not exist." << endl << endl;
235 cout <<
"Unable to open archive: " << uz.
formatError(ec).toAscii().data() << endl << endl;
240 if (!comment.isEmpty())
241 cout <<
"Archive comment: " << comment.toAscii().data() << endl << endl;
243 QList<UnZip::ZipEntry> list = uz.
entryList();
246 cout <<
"Empty archive.";
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;
259 for (
int i = 0; i < list.size(); ++i)
265 QString ratioS = QString::number(ratio,
'f', 2).append(
"%");
267 crc = crc.sprintf(
"%X", entry.
crc32).rightJustified(8,
'0');
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);
277 cout << setw(40) << file.toAscii().data() << setw(10) << entry.
uncompressedSize << setw(10) << ratioS.toAscii().data() << setw(10) << crc.toAscii().data() << endl;
ErrorCode createArchive(const QString &file, bool overwrite=true)
ErrorCode extractAll(const QString &dirname, ExtractionOptions options=ExtractPaths)
void setPassword(const QString &pwd)
void setArchiveComment(const QString &comment)
QList< ZipEntry > entryList() const
PKZip 2.0 file decompression. Compatibility with later versions is not ensured as they may use unsupp...
bool compress(const QString &zip, const QString &dir, const QString &pwd)
QString archiveComment() const
QString formatError(UnZip::ErrorCode c) const
ErrorCode addDirectory(const QString &path, CompressionOptions options=RelativePaths, CompressionLevel level=AutoFull)
bool decompress(const QString &file, const QString &out, const QString &pwd)
ErrorCode openArchive(const QString &filename)
int main(int argc, char **argv)
bool listFiles(const QString &file, const QString &pwd)
void setPassword(const QString &pwd)
QString formatError(ErrorCode c) const