-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqunzip.cpp
More file actions
140 lines (116 loc) · 3.97 KB
/
qunzip.cpp
File metadata and controls
140 lines (116 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// we really need this ?
// yes, it's always included
// but on windows we haven't this :)
// ok, we'll think about it later, but for linux it's a good way :)
#include <QtCore>
#if defined(Q_OS_LINUX)
#include <linux/limits.h>
#endif
#include <QDir>
#include <QFile>
#include <QDebug>
#include "unzip/unzip.h"
#include "qunzip.h"
#define UNZ_ERR ( -120 )
// for original do_extract_currentfile see: miniunz.c
static int do_extract_currentfile( unzFile uf, const QString &targetDir )
{
char zip_file_path[PATH_MAX];
int err=UNZ_OK;
char buf[4096]; // file read buffer
unz_file_info zip_file_info;
err = unzGetCurrentFileInfo( uf, &zip_file_info, zip_file_path, sizeof( zip_file_path ), NULL, 0, NULL, 0 );
if (err!=UNZ_OK) {
qCritical() << "unzGetCurrentFileInfo returned: %d" << err;
return err;
}
QString zipFilePath = QString::fromLocal8Bit( zip_file_path );
QString targetPath = targetDir + zipFilePath;
QString fileName = zipFilePath.split( "/" ).last();
if ( fileName.isEmpty() ) // is a directory
{
qDebug() << "creating directory:" << targetPath;
qDebug("creating directory: %s", zip_file_path );
QDir().mkpath( targetPath );
return UNZ_OK;
}
err = unzOpenCurrentFile( uf );
if ( err != UNZ_OK ) {
qCritical() << "unzOpenCurrentFile returned:" << err;
return err;
}
QFile fo( targetPath );
// check target path directory existance
if ( !QDir().mkpath( QFileInfo( targetPath ).path() ) ) {
qCritical() << "can't create target dir:" << targetDir;
}
if ( !fo.open( QIODevice::WriteOnly ) || !fo.isWritable() ) {
qCritical() << "can't open target file:" << targetPath;
return UNZ_ERR;
}
/* some zipfile don't contain directory alone before file */
//TODO: handle it ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// if ((fout==NULL) && filename_withoutpath!=filename)
// {
// char c=*(filename_withoutpath-1);
// *(filename_withoutpath-1)='\0';
// makedir(write_filename);
// *(filename_withoutpath-1)=c;
// fout=fopen(write_filename,"wb");
// }
qDebug() << "extracting:" << zipFilePath;
do {
err = unzReadCurrentFile( uf, buf, sizeof( buf ) );
if ( err < 0 ) {
qCritical() << "unzReadCurrentFile returned:" << err;
break;
}
if ( err > 0 && fo.write( buf, err ) != err ) {
qCritical() << "error in writing extracted file";
err = UNZ_ERRNO;
break;
}
//TODO: change extracted file's modification date (do we need it?)
// if (err==0)
// change_file_date(write_filename,file_info.dosDate,
// file_info.tmu_date);
} while( err > 0 );
fo.close();
err = unzCloseCurrentFile( uf );
if ( err != UNZ_OK ) {
qCritical() << "unzCloseCurrentFile returned %d" << err;
return err;
}
return UNZ_OK;
}
bool qUnzip( const QString &archPath, const QString &targetDir )
{
QString cwd = QDir::currentPath ();
QFileInfo fi = QFileInfo(archPath);
if (!QDir::setCurrent(fi.dir().path())) {
qCritical() << "can't chdir:" << fi.dir().path();
}
unzFile uf = unzOpen( fi.fileName().toLocal8Bit().data() );
QDir::setCurrent(cwd);
if( uf == NULL ) {
qCritical() << "can't open archive:" << QFileInfo(archPath).fileName();
return false;
}
if( unzOpenCurrentFile( uf ) != UNZ_OK ) {
qCritical() << "can't get the first file in archive";
}
unz_global_info gi;
if( unzGetGlobalInfo( uf, &gi ) != UNZ_OK ) {
qCritical() << "can't get global info";
}
int err = unzGoToFirstFile( uf );
while( err == UNZ_OK ) {
if ( do_extract_currentfile( uf, targetDir ) != UNZ_OK ) {
unzClose( uf );
return false;
}
err = unzGoToNextFile( uf );
}
unzClose( uf );
return true;
}