Subversion Repositories Scribus

Rev

Rev 415 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
411 Franz 1
/***************************************************************************
2
 *   Copyright (C) 2004 by Riku Leino                                      *
3
 *   tsoots@welho.com                                                      *
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 2 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, write to the                         *
17
 *   Free Software Foundation, Inc.,                                       *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 ***************************************************************************/
20
 
21
#include "sxwunzip.h"
22
 
23
#ifdef HAVE_XML
24
 
25
#include <stdlib.h>
26
#include <string.h>
27
#include <errno.h>
28
#include <fcntl.h>
29
 
30
#ifdef unix
31
# include <unistd.h>
32
# include <utime.h>
33
#else
34
# include <direct.h>
35
# include <io.h>
36
#endif
37
 
38
#include <iostream>
39
#include "unzip.h"
40
#include <qdir.h>
41
 
42
#define CASESENSITIVITY (0)
43
#define WRITEBUFFERSIZE (8192)
44
#define MAXFILENAME (256)
45
 
46
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password);
47
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password);
48
int mymkdir(const char* dirname);
49
int makedir (char *newdir);
50
void change_file_date(const char* filename, uLong dosdate, tm_unz tmu_date);
51
 
52
SxwUnzip::SxwUnzip(QString zipFilePath)
53
{
54
        zipFile = zipFilePath;
55
}
56
 
57
QString SxwUnzip::getFile(QString name)
58
{
59
        QString pwd = QDir::currentDirPath();
60
        QString outDir = zipFile.left(zipFile.findRev("/") + 1);
61
        QFile f(outDir);
62
        QFileInfo fi(f);
63
        if (!fi.isWritable())
64
                outDir = QDir::homeDirPath() + "/.scribus/";
65
        QDir::setCurrent(outDir);
66
        const char *zipfilename = zipFile.ascii();
67
        const char *filename_to_extract = name.ascii();
68
        unzFile uf = unzOpen(zipfilename);;
69
        int ret = do_extract_onefile(uf,filename_to_extract, 0, 1, NULL);
70
        unzCloseCurrentFile(uf);
71
        QDir::setCurrent(pwd);
72
        if (ret != 0)
73
                return NULL;
74
        else
75
                return outDir + name;
76
}
77
 
78
SxwUnzip::~SxwUnzip()
79
{
80
 
81
}
82
 
83
 
84
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password)
85
{
86
    if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
87
        return 2;
88
 
89
    if (do_extract_currentfile(uf,&opt_extract_without_path,
90
                                      &opt_overwrite,
91
                                      password) == UNZ_OK)
92
        return 0;
93
    else
94
        return 1;
95
}
96
 
97
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password)
98
{
99
    char filename_inzip[256];
100
    char* filename_withoutpath;
101
    char* p;
102
    int err=UNZ_OK;
103
    FILE *fout=NULL;
104
    void* buf;
105
    uInt size_buf;
106
 
107
    unz_file_info file_info;
108
//    uLong ratio=0;
109
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
110
 
111
    if (err!=UNZ_OK)
112
        return err;
113
 
114
    size_buf = WRITEBUFFERSIZE;
115
    buf = (void*)malloc(size_buf);
116
    if (buf==NULL)
117
        return UNZ_INTERNALERROR;
118
 
119
    p = filename_withoutpath = filename_inzip;
120
    while ((*p) != '\0')
121
    {
122
        if (((*p)=='/') || ((*p)=='\\'))
123
            filename_withoutpath = p+1;
124
        p++;
125
    }
126
 
127
    if ((*filename_withoutpath)=='\0')
128
    {
129
        if ((*popt_extract_without_path)==0)
130
            mymkdir(filename_inzip);
131
    }
132
    else
133
    {
134
        char* write_filename;
135
        int skip=0;
136
 
137
        if ((*popt_extract_without_path)==0)
138
            write_filename = filename_inzip;
139
        else
140
            write_filename = filename_withoutpath;
141
 
142
        err = unzOpenCurrentFilePassword(uf,password);
143
 
144
        if ((skip==0) && (err==UNZ_OK))
145
        {
146
            fout=fopen(write_filename,"wb");
147
 
148
            /* some zipfile don't contain directory alone before file */
149
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
150
                                (filename_withoutpath!=(char*)filename_inzip))
151
            {
152
                char c=*(filename_withoutpath-1);
153
                *(filename_withoutpath-1)='\0';
154
                makedir(write_filename);
155
                *(filename_withoutpath-1)=c;
156
                fout=fopen(write_filename,"wb");
157
            }
158
        }
159
 
160
        if (fout!=NULL)
161
        {
162
 
163
            do
164
            {
165
                err = unzReadCurrentFile(uf,buf,size_buf);
166
                if (err<0)
167
                    break;
168
                if (err>0)
169
                    if (fwrite(buf,err,1,fout)!=1)
170
                    {
171
                        err=UNZ_ERRNO;
172
                        break;
173
                    }
174
            }
175
            while (err>0);
176
            if (fout)
177
                    fclose(fout);
178
 
179
            if (err==0)
180
                change_file_date(write_filename,file_info.dosDate,
181
                                 file_info.tmu_date);
182
        }
183
 
184
        if (err==UNZ_OK)
185
            err = unzCloseCurrentFile (uf);
186
        else
187
            unzCloseCurrentFile(uf); /* don't lose the error */
188
    }
189
 
190
    free(buf);
191
    return err;
192
}
193
 
194
int mymkdir(char* dirname)
195
{
196
    int ret=0;
197
#ifdef WIN32
198
    ret = mkdir(dirname);
199
#else
200
#ifdef unix
201
    ret = mkdir (dirname,0775);
202
#endif
203
#endif
204
    return ret;
205
}
206
 
207
int makedir (const char *newdir)
208
{
209
  char *buffer ;
210
  char *p;
211
  int  len = (int)strlen(newdir);
212
 
213
  if (len <= 0)
214
    return 0;
215
 
216
  buffer = (char*)malloc(len+1);
217
  strcpy(buffer,newdir);
218
 
219
  if (buffer[len-1] == '/') {
220
    buffer[len-1] = '\0';
221
  }
222
  if (mymkdir(buffer) == 0)
223
    {
224
      free(buffer);
225
      return 1;
226
    }
227
 
228
  p = buffer+1;
229
  while (1)
230
    {
231
      char hold;
232
 
233
      while(*p && *p != '\\' && *p != '/')
234
        p++;
235
      hold = *p;
236
      *p = 0;
237
      if ((mymkdir(buffer) == -1) && (errno == ENOENT))
238
        {
239
          free(buffer);
240
          return 0;
241
        }
242
      if (hold == 0)
243
        break;
244
      *p++ = hold;
245
    }
246
  free(buffer);
247
  return 1;
248
}
249
 
250
/* change_file_date : change the date/time of a file
251
    filename : the filename of the file where date/time must be modified
252
    dosdate : the new date at the MSDos format (4 bytes)
253
    tmu_date : the SAME new date at the tm_unz format */
254
void change_file_date(const char* filename, uLong dosdate, tm_unz tmu_date)
255
{
256
#ifdef WIN32
257
  HANDLE hFile;
258
  FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
259
 
260
  hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
261
                      0,NULL,OPEN_EXISTING,0,NULL);
262
  GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
263
  DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
264
  LocalFileTimeToFileTime(&ftLocal,&ftm);
265
  SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
266
  CloseHandle(hFile);
267
#else
268
#ifdef unix
269
  struct utimbuf ut;
270
  struct tm newdate;
271
  newdate.tm_sec = tmu_date.tm_sec;
272
  newdate.tm_min=tmu_date.tm_min;
273
  newdate.tm_hour=tmu_date.tm_hour;
274
  newdate.tm_mday=tmu_date.tm_mday;
275
  newdate.tm_mon=tmu_date.tm_mon;
276
  if (tmu_date.tm_year > 1900)
277
      newdate.tm_year=tmu_date.tm_year - 1900;
278
  else
279
      newdate.tm_year=tmu_date.tm_year ;
280
  newdate.tm_isdst=-1;
281
 
282
  ut.actime=ut.modtime=mktime(&newdate);
283
  utime(filename,&ut);
284
#endif
285
#endif
286
}
287
 
288
#endif // HAVE_XML
289