~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

Viewing changes to src/tools/zip/zip.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2006-01-20 00:20:36 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060120002036-7nw6yo6totip0ee5
Tags: 0.10-2
* Added upstream changelog (Closes: Bug#327748)
* Mention --no-gpg and --no-gpg-agent in manpage (Closes: Bug#204416)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * zip.cpp - zip/unzip files
 
3
 * Copyright (C) 2003  Justin Karneges
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library 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 GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include"zip.h"
 
22
 
 
23
#include<qcstring.h>
 
24
#include<qstringlist.h>
 
25
#include<qfile.h>
 
26
 
 
27
#include"minizip/unzip.h"
 
28
 
 
29
class UnZipPrivate
 
30
{
 
31
public:
 
32
        UnZipPrivate() {}
 
33
 
 
34
        QString name;
 
35
        unzFile uf;
 
36
        QStringList listing;
 
37
};
 
38
 
 
39
UnZip::UnZip(const QString &name)
 
40
{
 
41
        d = new UnZipPrivate;
 
42
        d->uf = 0;
 
43
        d->name = name;
 
44
}
 
45
 
 
46
UnZip::~UnZip()
 
47
{
 
48
        close();
 
49
        delete d;
 
50
}
 
51
 
 
52
void UnZip::setName(const QString &name)
 
53
{
 
54
        d->name = name;
 
55
}
 
56
 
 
57
const QString & UnZip::name() const
 
58
{
 
59
        return d->name;
 
60
}
 
61
 
 
62
bool UnZip::open()
 
63
{
 
64
        close();
 
65
 
 
66
        d->uf = unzOpen(QFile::encodeName(d->name).data());
 
67
        if(!d->uf)
 
68
                return false;
 
69
 
 
70
        return getList();
 
71
}
 
72
 
 
73
void UnZip::close()
 
74
{
 
75
        if(d->uf) {
 
76
                unzClose(d->uf);
 
77
                d->uf = 0;
 
78
        }
 
79
 
 
80
        d->listing.clear();
 
81
}
 
82
 
 
83
const QStringList & UnZip::list() const
 
84
{
 
85
        return d->listing;
 
86
}
 
87
 
 
88
bool UnZip::getList()
 
89
{
 
90
        unz_global_info gi;
 
91
        int err = unzGetGlobalInfo(d->uf, &gi);
 
92
        if(err != UNZ_OK)
 
93
                return false;
 
94
 
 
95
        QStringList l;
 
96
        for(int n = 0; n < (int)gi.number_entry; ++n) {
 
97
                char filename_inzip[256];
 
98
                unz_file_info file_info;
 
99
                int err = unzGetCurrentFileInfo(d->uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
 
100
                if(err != UNZ_OK)
 
101
                        return false;
 
102
 
 
103
                l += filename_inzip;
 
104
 
 
105
                if((n+1) < (int)gi.number_entry) {
 
106
                        err = unzGoToNextFile(d->uf);
 
107
                        if(err != UNZ_OK)
 
108
                                return false;
 
109
                }
 
110
        }
 
111
 
 
112
        d->listing = l;
 
113
 
 
114
        return true;
 
115
}
 
116
 
 
117
bool UnZip::readFile(const QString &fname, QByteArray *buf, int max)
 
118
{
 
119
        int err = unzLocateFile(d->uf, QFile::encodeName(fname).data(), 0);
 
120
        if(err != UNZ_OK)
 
121
                return false;
 
122
 
 
123
        char filename_inzip[256];
 
124
        unz_file_info file_info;
 
125
        err = unzGetCurrentFileInfo(d->uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
 
126
        if(err != UNZ_OK)
 
127
                return false;
 
128
 
 
129
        err = unzOpenCurrentFile(d->uf);
 
130
        if(err != UNZ_OK)
 
131
                return false;
 
132
 
 
133
        QByteArray a(0);
 
134
        QByteArray chunk(16384);
 
135
        while(1) {
 
136
                err = unzReadCurrentFile(d->uf, chunk.data(), chunk.size());
 
137
                if(err < 0) {
 
138
                        unzCloseCurrentFile(d->uf);
 
139
                        return false;
 
140
                }
 
141
                if(err == 0)
 
142
                        break;
 
143
 
 
144
                int oldsize = a.size();
 
145
                if(max > 0 && oldsize + err > max)
 
146
                        err = max - oldsize;
 
147
                a.resize(oldsize + err);
 
148
                memcpy(a.data() + oldsize, chunk.data(), err);
 
149
 
 
150
                if(max > 0 && (int)a.size() >= max)
 
151
                        break;
 
152
        }
 
153
 
 
154
        err = unzCloseCurrentFile(d->uf);
 
155
        if(err != UNZ_OK)
 
156
                return false;
 
157
 
 
158
        *buf = a;
 
159
        return true;
 
160
}