Subversion Repositories Scribus

Rev

Rev 4430 | Rev 6382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
1576 tsoots 7
/***************************************************************************
8
 *   Copyright (C) 2004 by Riku Leino                                      *
9
 *   tsoots@gmail.com                                                      *
10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 *   This program is distributed in the hope that it will be useful,       *
17
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
 *   GNU General Public License for more details.                          *
20
 *                                                                         *
21
 *   You should have received a copy of the GNU General Public License     *
22
 *   along with this program; if not, write to the                         *
23
 *   Free Software Foundation, Inc.,                                       *
24
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25
 ***************************************************************************/
26
 
27
#include "fileunzip.h"
28
 
29
#include <stdlib.h>
30
#include <string.h>
31
#include <errno.h>
32
#include <fcntl.h>
2632 craig 33
 
34
#ifdef HAVE_SYS_TYPES_H
35
#include <sys/types.h>
36
#endif
37
#ifdef HAVE_SYS_STAT_H
1620 tsoots 38
#include <sys/stat.h>
2632 craig 39
#endif
1576 tsoots 40
 
2702 craig 41
#ifdef HAVE_UNISTD_H
1576 tsoots 42
# include <unistd.h>
2702 craig 43
#endif
44
 
2730 cbradney 45
#ifndef _MSC_VER
46
#include <utime.h>
47
#else
48
#include <direct.h>
49
#include <time.h>
50
#include <sys/utime.h>
51
#endif
1576 tsoots 52
 
53
#include <iostream>
54
#include "unzip.h"
55
#include <qdir.h>
56
 
5917 jghali 57
#include "scpaths.h"
58
 
1576 tsoots 59
#define CASESENSITIVITY (0)
60
#define WRITEBUFFERSIZE (8192)
61
#define MAXFILENAME (256)
62
 
63
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password);
64
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password);
65
int mymkdir(const char* dirname);
66
int makedir (char *newdir);
67
void change_file_date(const char* filename, uLong dosdate, tm_unz tmu_date);
68
 
69
FileUnzip::FileUnzip(QString zipFilePath)
70
{
71
        zipFile = zipFilePath;
72
}
73
 
74
QString FileUnzip::getFile(QString name)
75
{
76
        QString pwd = QDir::currentDirPath();
5917 jghali 77
        QString outDir = ScPaths::getTempFileDir();
1576 tsoots 78
        QFile f(outDir);
79
        QFileInfo fi(f);
80
        if (!fi.isWritable())
5917 jghali 81
                outDir = ScPaths::getApplicationDataDir();
1576 tsoots 82
        QDir::setCurrent(outDir);
83
        const char *zipfilename = zipFile.ascii();
84
        const char *filename_to_extract = name.ascii();
85
        unzFile uf = unzOpen(zipfilename);;
86
        int ret = do_extract_onefile(uf,filename_to_extract, 0, 1, NULL);
87
        unzCloseCurrentFile(uf);
88
        QDir::setCurrent(pwd);
89
        if (ret != 0)
90
                return NULL;
91
        else
92
                return outDir + name;
93
}
94
 
95
FileUnzip::~FileUnzip()
96
{
97
 
98
}
99
 
100
 
101
int mymkdir(char* dirname)
102
{
103
    int ret=0;
2730 cbradney 104
#ifndef _MSC_VER
1576 tsoots 105
    ret = mkdir (dirname,0775);
2730 cbradney 106
#else
107
        ret = _mkdir(dirname);
108
#endif
1576 tsoots 109
    return ret;
110
}
111
 
112
int makedir (char *newdir)
113
{
114
  char *buffer ;
115
  char *p;
116
  int  len = (int)strlen(newdir);
117
 
118
  if (len <= 0)
119
    return 0;
120
 
121
  buffer = (char*)malloc(len+1);
122
  strcpy(buffer,newdir);
123
 
124
  if (buffer[len-1] == '/') {
125
    buffer[len-1] = '\0';
126
  }
127
  if (mymkdir(buffer) == 0)
128
    {
129
      free(buffer);
130
      return 1;
131
    }
132
 
133
  p = buffer+1;
134
  while (1)
135
    {
136
      char hold;
137
 
138
      while(*p && *p != '\\' && *p != '/')
139
        p++;
140
      hold = *p;
141
      *p = 0;
142
      if ((mymkdir(buffer) == -1) && (errno == ENOENT))
143
        {
144
          free(buffer);
145
          return 0;
146
        }
147
      if (hold == 0)
148
        break;
149
      *p++ = hold;
150
    }
151
  free(buffer);
152
  return 1;
153
}
154
 
155
 
156
 
157
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password)
158
{
159
//    int err = UNZ_OK;
160
    if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
161
        return 2;
162
 
163
    if (do_extract_currentfile(uf,&opt_extract_without_path,
164
                                      &opt_overwrite,
165
                                      password) == UNZ_OK)
166
        return 0;
167
    else
168
        return 1;
169
}
170
 
171
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int*, const char* password)
172
{
173
    char filename_inzip[256];
174
    char* filename_withoutpath;
175
    char* p;
176
    int err=UNZ_OK;
177
    FILE *fout=NULL;
178
    void* buf;
179
    uInt size_buf;
180
 
181
    unz_file_info file_info;
182
//    uLong ratio=0;
183
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
184
 
185
    if (err!=UNZ_OK)
186
        return err;
187
 
188
    size_buf = WRITEBUFFERSIZE;
189
    buf = (void*)malloc(size_buf);
190
    if (buf==NULL)
191
        return UNZ_INTERNALERROR;
192
 
193
    p = filename_withoutpath = filename_inzip;
194
    while ((*p) != '\0')
195
    {
196
        if (((*p)=='/') || ((*p)=='\\'))
197
            filename_withoutpath = p+1;
198
        p++;
199
    }
200
 
201
    if ((*filename_withoutpath)=='\0')
202
    {
203
        if ((*popt_extract_without_path)==0)
204
            mymkdir(filename_inzip);
205
    }
206
    else
207
    {
208
        char* write_filename;
209
        int skip=0;
210
 
211
        if ((*popt_extract_without_path)==0)
212
            write_filename = filename_inzip;
213
        else
214
            write_filename = filename_withoutpath;
215
 
216
        err = unzOpenCurrentFilePassword(uf,password);
217
 
218
        if ((skip==0) && (err==UNZ_OK))
219
        {
220
            fout=fopen(write_filename,"wb");
221
 
222
            /* some zipfile don't contain directory alone before file */
223
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
224
                                (filename_withoutpath!=(char*)filename_inzip))
225
            {
226
                char c=*(filename_withoutpath-1);
227
                *(filename_withoutpath-1)='\0';
228
                makedir(write_filename);
229
                *(filename_withoutpath-1)=c;
230
                fout=fopen(write_filename,"wb");
231
            }
232
        }
233
 
234
        if (fout!=NULL)
235
        {
236
 
237
            do
238
            {
239
                err = unzReadCurrentFile(uf,buf,size_buf);
240
                if (err<0)
241
                    break;
242
                if (err>0)
243
                    if (fwrite(buf,err,1,fout)!=1)
244
                    {
245
                        err=UNZ_ERRNO;
246
                        break;
247
                    }
248
            }
249
            while (err>0);
250
            if (fout)
251
                    fclose(fout);
252
 
253
            if (err==0)
254
                change_file_date(write_filename,file_info.dosDate,
255
                                 file_info.tmu_date);
256
        }
257
 
258
        if (err==UNZ_OK)
259
            err = unzCloseCurrentFile (uf);
260
        else
261
            unzCloseCurrentFile(uf); /* don't lose the error */
262
    }
263
 
264
    free(buf);
265
    return err;
266
}
267
 
268
/* change_file_date : change the date/time of a file
269
    filename : the filename of the file where date/time must be modified
270
    dosdate : the new date at the MSDos format (4 bytes)
271
    tmu_date : the SAME new date at the tm_unz format */
272
void change_file_date(const char* filename, uLong, tm_unz tmu_date)
273
{
274
  struct utimbuf ut;
275
  struct tm newdate;
276
  newdate.tm_sec = tmu_date.tm_sec;
277
  newdate.tm_min=tmu_date.tm_min;
278
  newdate.tm_hour=tmu_date.tm_hour;
279
  newdate.tm_mday=tmu_date.tm_mday;
280
  newdate.tm_mon=tmu_date.tm_mon;
281
  if (tmu_date.tm_year > 1900)
282
      newdate.tm_year=tmu_date.tm_year - 1900;
283
  else
284
      newdate.tm_year=tmu_date.tm_year ;
285
  newdate.tm_isdst=-1;
286
 
287
  ut.actime=ut.modtime=mktime(&newdate);
288
  utime(filename,&ut);
289
}
290
 
291