Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
1576 tsoots 1
/***************************************************************************
2
 *   Copyright (C) 2004 by Riku Leino                                      *
3
 *   tsoots@gmail.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 "fileunzip.h"
22
 
23
#include <stdlib.h>
24
#include <string.h>
25
#include <errno.h>
26
#include <fcntl.h>
2632 craig 27
 
28
#ifdef HAVE_SYS_TYPES_H
29
#include <sys/types.h>
30
#endif
31
#ifdef HAVE_SYS_STAT_H
1620 tsoots 32
#include <sys/stat.h>
2632 craig 33
#endif
1576 tsoots 34
 
35
# include <unistd.h>
36
# include <utime.h>
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
FileUnzip::FileUnzip(QString zipFilePath)
53
{
54
        zipFile = zipFilePath;
55
}
56
 
57
QString FileUnzip::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
FileUnzip::~FileUnzip()
79
{
80
 
81
}
82
 
83
 
84
int mymkdir(char* dirname)
85
{
86
    int ret=0;
87
    ret = mkdir (dirname,0775);
88
    return ret;
89
}
90
 
91
int makedir (char *newdir)
92
{
93
  char *buffer ;
94
  char *p;
95
  int  len = (int)strlen(newdir);
96
 
97
  if (len <= 0)
98
    return 0;
99
 
100
  buffer = (char*)malloc(len+1);
101
  strcpy(buffer,newdir);
102
 
103
  if (buffer[len-1] == '/') {
104
    buffer[len-1] = '\0';
105
  }
106
  if (mymkdir(buffer) == 0)
107
    {
108
      free(buffer);
109
      return 1;
110
    }
111
 
112
  p = buffer+1;
113
  while (1)
114
    {
115
      char hold;
116
 
117
      while(*p && *p != '\\' && *p != '/')
118
        p++;
119
      hold = *p;
120
      *p = 0;
121
      if ((mymkdir(buffer) == -1) && (errno == ENOENT))
122
        {
123
          free(buffer);
124
          return 0;
125
        }
126
      if (hold == 0)
127
        break;
128
      *p++ = hold;
129
    }
130
  free(buffer);
131
  return 1;
132
}
133
 
134
 
135
 
136
int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password)
137
{
138
//    int err = UNZ_OK;
139
    if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
140
        return 2;
141
 
142
    if (do_extract_currentfile(uf,&opt_extract_without_path,
143
                                      &opt_overwrite,
144
                                      password) == UNZ_OK)
145
        return 0;
146
    else
147
        return 1;
148
}
149
 
150
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int*, const char* password)
151
{
152
    char filename_inzip[256];
153
    char* filename_withoutpath;
154
    char* p;
155
    int err=UNZ_OK;
156
    FILE *fout=NULL;
157
    void* buf;
158
    uInt size_buf;
159
 
160
    unz_file_info file_info;
161
//    uLong ratio=0;
162
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
163
 
164
    if (err!=UNZ_OK)
165
        return err;
166
 
167
    size_buf = WRITEBUFFERSIZE;
168
    buf = (void*)malloc(size_buf);
169
    if (buf==NULL)
170
        return UNZ_INTERNALERROR;
171
 
172
    p = filename_withoutpath = filename_inzip;
173
    while ((*p) != '\0')
174
    {
175
        if (((*p)=='/') || ((*p)=='\\'))
176
            filename_withoutpath = p+1;
177
        p++;
178
    }
179
 
180
    if ((*filename_withoutpath)=='\0')
181
    {
182
        if ((*popt_extract_without_path)==0)
183
            mymkdir(filename_inzip);
184
    }
185
    else
186
    {
187
        char* write_filename;
188
        int skip=0;
189
 
190
        if ((*popt_extract_without_path)==0)
191
            write_filename = filename_inzip;
192
        else
193
            write_filename = filename_withoutpath;
194
 
195
        err = unzOpenCurrentFilePassword(uf,password);
196
 
197
        if ((skip==0) && (err==UNZ_OK))
198
        {
199
            fout=fopen(write_filename,"wb");
200
 
201
            /* some zipfile don't contain directory alone before file */
202
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
203
                                (filename_withoutpath!=(char*)filename_inzip))
204
            {
205
                char c=*(filename_withoutpath-1);
206
                *(filename_withoutpath-1)='\0';
207
                makedir(write_filename);
208
                *(filename_withoutpath-1)=c;
209
                fout=fopen(write_filename,"wb");
210
            }
211
        }
212
 
213
        if (fout!=NULL)
214
        {
215
 
216
            do
217
            {
218
                err = unzReadCurrentFile(uf,buf,size_buf);
219
                if (err<0)
220
                    break;
221
                if (err>0)
222
                    if (fwrite(buf,err,1,fout)!=1)
223
                    {
224
                        err=UNZ_ERRNO;
225
                        break;
226
                    }
227
            }
228
            while (err>0);
229
            if (fout)
230
                    fclose(fout);
231
 
232
            if (err==0)
233
                change_file_date(write_filename,file_info.dosDate,
234
                                 file_info.tmu_date);
235
        }
236
 
237
        if (err==UNZ_OK)
238
            err = unzCloseCurrentFile (uf);
239
        else
240
            unzCloseCurrentFile(uf); /* don't lose the error */
241
    }
242
 
243
    free(buf);
244
    return err;
245
}
246
 
247
/* change_file_date : change the date/time of a file
248
    filename : the filename of the file where date/time must be modified
249
    dosdate : the new date at the MSDos format (4 bytes)
250
    tmu_date : the SAME new date at the tm_unz format */
251
void change_file_date(const char* filename, uLong, tm_unz tmu_date)
252
{
253
  struct utimbuf ut;
254
  struct tm newdate;
255
  newdate.tm_sec = tmu_date.tm_sec;
256
  newdate.tm_min=tmu_date.tm_min;
257
  newdate.tm_hour=tmu_date.tm_hour;
258
  newdate.tm_mday=tmu_date.tm_mday;
259
  newdate.tm_mon=tmu_date.tm_mon;
260
  if (tmu_date.tm_year > 1900)
261
      newdate.tm_year=tmu_date.tm_year - 1900;
262
  else
263
      newdate.tm_year=tmu_date.tm_year ;
264
  newdate.tm_isdst=-1;
265
 
266
  ut.actime=ut.modtime=mktime(&newdate);
267
  utime(filename,&ut);
268
}
269
 
270