Subversion Repositories Scribus

Rev

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