Subversion Repositories Scribus

Rev

Rev 2730 | Rev 5917 | 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
 
57
#define CASESENSITIVITY (0)
58
#define WRITEBUFFERSIZE (8192)
59
#define MAXFILENAME (256)
60
 
61
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password);
62
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password);
63
int mymkdir(const char* dirname);
64
int makedir (char *newdir);
65
void change_file_date(const char* filename, uLong dosdate, tm_unz tmu_date);
66
 
67
FileUnzip::FileUnzip(QString zipFilePath)
68
{
69
        zipFile = zipFilePath;
70
}
71
 
72
QString FileUnzip::getFile(QString name)
73
{
74
        QString pwd = QDir::currentDirPath();
75
        QString outDir = zipFile.left(zipFile.findRev("/") + 1);
76
        QFile f(outDir);
77
        QFileInfo fi(f);
78
        if (!fi.isWritable())
79
                outDir = QDir::homeDirPath() + "/.scribus/";
80
        QDir::setCurrent(outDir);
81
        const char *zipfilename = zipFile.ascii();
82
        const char *filename_to_extract = name.ascii();
83
        unzFile uf = unzOpen(zipfilename);;
84
        int ret = do_extract_onefile(uf,filename_to_extract, 0, 1, NULL);
85
        unzCloseCurrentFile(uf);
86
        QDir::setCurrent(pwd);
87
        if (ret != 0)
88
                return NULL;
89
        else
90
                return outDir + name;
91
}
92
 
93
FileUnzip::~FileUnzip()
94
{
95
 
96
}
97
 
98
 
99
int mymkdir(char* dirname)
100
{
101
    int ret=0;
2730 cbradney 102
#ifndef _MSC_VER
1576 tsoots 103
    ret = mkdir (dirname,0775);
2730 cbradney 104
#else
105
        ret = _mkdir(dirname);
106
#endif
1576 tsoots 107
    return ret;
108
}
109
 
110
int makedir (char *newdir)
111
{
112
  char *buffer ;
113
  char *p;
114
  int  len = (int)strlen(newdir);
115
 
116
  if (len <= 0)
117
    return 0;
118
 
119
  buffer = (char*)malloc(len+1);
120
  strcpy(buffer,newdir);
121
 
122
  if (buffer[len-1] == '/') {
123
    buffer[len-1] = '\0';
124
  }
125
  if (mymkdir(buffer) == 0)
126
    {
127
      free(buffer);
128
      return 1;
129
    }
130
 
131
  p = buffer+1;
132
  while (1)
133
    {
134
      char hold;
135
 
136
      while(*p && *p != '\\' && *p != '/')
137
        p++;
138
      hold = *p;
139
      *p = 0;
140
      if ((mymkdir(buffer) == -1) && (errno == ENOENT))
141
        {
142
          free(buffer);
143
          return 0;
144
        }
145
      if (hold == 0)
146
        break;
147
      *p++ = hold;
148
    }
149
  free(buffer);
150
  return 1;
151
}
152
 
153
 
154
 
155
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password)
156
{
157
//    int err = UNZ_OK;
158
    if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
159
        return 2;
160
 
161
    if (do_extract_currentfile(uf,&opt_extract_without_path,
162
                                      &opt_overwrite,
163
                                      password) == UNZ_OK)
164
        return 0;
165
    else
166
        return 1;
167
}
168
 
169
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int*, const char* password)
170
{
171
    char filename_inzip[256];
172
    char* filename_withoutpath;
173
    char* p;
174
    int err=UNZ_OK;
175
    FILE *fout=NULL;
176
    void* buf;
177
    uInt size_buf;
178
 
179
    unz_file_info file_info;
180
//    uLong ratio=0;
181
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
182
 
183
    if (err!=UNZ_OK)
184
        return err;
185
 
186
    size_buf = WRITEBUFFERSIZE;
187
    buf = (void*)malloc(size_buf);
188
    if (buf==NULL)
189
        return UNZ_INTERNALERROR;
190
 
191
    p = filename_withoutpath = filename_inzip;
192
    while ((*p) != '\0')
193
    {
194
        if (((*p)=='/') || ((*p)=='\\'))
195
            filename_withoutpath = p+1;
196
        p++;
197
    }
198
 
199
    if ((*filename_withoutpath)=='\0')
200
    {
201
        if ((*popt_extract_without_path)==0)
202
            mymkdir(filename_inzip);
203
    }
204
    else
205
    {
206
        char* write_filename;
207
        int skip=0;
208
 
209
        if ((*popt_extract_without_path)==0)
210
            write_filename = filename_inzip;
211
        else
212
            write_filename = filename_withoutpath;
213
 
214
        err = unzOpenCurrentFilePassword(uf,password);
215
 
216
        if ((skip==0) && (err==UNZ_OK))
217
        {
218
            fout=fopen(write_filename,"wb");
219
 
220
            /* some zipfile don't contain directory alone before file */
221
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
222
                                (filename_withoutpath!=(char*)filename_inzip))
223
            {
224
                char c=*(filename_withoutpath-1);
225
                *(filename_withoutpath-1)='\0';
226
                makedir(write_filename);
227
                *(filename_withoutpath-1)=c;
228
                fout=fopen(write_filename,"wb");
229
            }
230
        }
231
 
232
        if (fout!=NULL)
233
        {
234
 
235
            do
236
            {
237
                err = unzReadCurrentFile(uf,buf,size_buf);
238
                if (err<0)
239
                    break;
240
                if (err>0)
241
                    if (fwrite(buf,err,1,fout)!=1)
242
                    {
243
                        err=UNZ_ERRNO;
244
                        break;
245
                    }
246
            }
247
            while (err>0);
248
            if (fout)
249
                    fclose(fout);
250
 
251
            if (err==0)
252
                change_file_date(write_filename,file_info.dosDate,
253
                                 file_info.tmu_date);
254
        }
255
 
256
        if (err==UNZ_OK)
257
            err = unzCloseCurrentFile (uf);
258
        else
259
            unzCloseCurrentFile(uf); /* don't lose the error */
260
    }
261
 
262
    free(buf);
263
    return err;
264
}
265
 
266
/* change_file_date : change the date/time of a file
267
    filename : the filename of the file where date/time must be modified
268
    dosdate : the new date at the MSDos format (4 bytes)
269
    tmu_date : the SAME new date at the tm_unz format */
270
void change_file_date(const char* filename, uLong, tm_unz tmu_date)
271
{
272
  struct utimbuf ut;
273
  struct tm newdate;
274
  newdate.tm_sec = tmu_date.tm_sec;
275
  newdate.tm_min=tmu_date.tm_min;
276
  newdate.tm_hour=tmu_date.tm_hour;
277
  newdate.tm_mday=tmu_date.tm_mday;
278
  newdate.tm_mon=tmu_date.tm_mon;
279
  if (tmu_date.tm_year > 1900)
280
      newdate.tm_year=tmu_date.tm_year - 1900;
281
  else
282
      newdate.tm_year=tmu_date.tm_year ;
283
  newdate.tm_isdst=-1;
284
 
285
  ut.actime=ut.modtime=mktime(&newdate);
286
  utime(filename,&ut);
287
}
288
 
289