Subversion Repositories Scribus

Rev

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