]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: fs_zip.cpp | |
3 | // Purpose: ZIP file system | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
de0702d0 | 6 | // CVS-ID: $Id$ |
5526e819 VS |
7 | // Licence: wxWindows Licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
de0702d0 | 11 | |
5526e819 | 12 | #ifdef __GNUG__ |
c3f4609e | 13 | #pragma implementation "fs_zip.h" |
5526e819 VS |
14 | #endif |
15 | ||
3096bd2f | 16 | #include "wx/wxprec.h" |
5526e819 VS |
17 | |
18 | #ifdef __BORDLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
24528b0c | 22 | #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |
e3e717ec | 23 | |
5526e819 | 24 | #ifndef WXPRECOMP |
04dbb646 VZ |
25 | #include "wx/intl.h" |
26 | #include "wx/log.h" | |
5526e819 VS |
27 | #endif |
28 | ||
de0702d0 | 29 | #include "wx/hash.h" |
5526e819 | 30 | #include "wx/filesys.h" |
6001e347 | 31 | #include "wx/zipstrm.h" |
5526e819 VS |
32 | #include "wx/fs_zip.h" |
33 | ||
aaa66113 VS |
34 | /* Not the right solution (paths in makefiles) but... */ |
35 | #ifdef __BORLANDC__ | |
36 | #include "../common/unzip.h" | |
37 | #else | |
38 | #include "unzip.h" | |
39 | #endif | |
40 | ||
5526e819 VS |
41 | |
42 | //-------------------------------------------------------------------------------- | |
43 | // wxZipFSHandler | |
44 | //-------------------------------------------------------------------------------- | |
45 | ||
46 | ||
47 | ||
aaa66113 VS |
48 | wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler() |
49 | { | |
50 | m_Archive = NULL; | |
51 | m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString; | |
52 | m_AllowDirs = m_AllowFiles = TRUE; | |
de0702d0 | 53 | m_DirsFound = NULL; |
aaa66113 VS |
54 | } |
55 | ||
56 | ||
57 | ||
58 | wxZipFSHandler::~wxZipFSHandler() | |
59 | { | |
60 | if (m_Archive) | |
61 | unzClose((unzFile)m_Archive); | |
de0702d0 VS |
62 | if (m_DirsFound) |
63 | delete m_DirsFound; | |
aaa66113 VS |
64 | } |
65 | ||
66 | ||
67 | ||
5526e819 VS |
68 | bool wxZipFSHandler::CanOpen(const wxString& location) |
69 | { | |
70 | wxString p = GetProtocol(location); | |
08b50c2c VS |
71 | return (p == wxT("zip")) && |
72 | (GetProtocol(GetLeftLocation(location)) == wxT("file")); | |
5526e819 VS |
73 | } |
74 | ||
75 | ||
76 | ||
77 | ||
78 | wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
79 | { | |
80 | wxString right = GetRightLocation(location); | |
81 | wxString left = GetLeftLocation(location); | |
82 | wxInputStream *s; | |
83 | ||
095472c0 VS |
84 | if (GetProtocol(left) != wxT("file")) |
85 | { | |
86 | wxLogError(_("ZIP handler currently supports only local files!")); | |
5526e819 VS |
87 | return NULL; |
88 | } | |
89 | ||
90 | s = new wxZipInputStream(left, right); | |
095472c0 VS |
91 | if (s && (s->LastError() == wxStream_NOERROR)) |
92 | { | |
5526e819 | 93 | return new wxFSFile(s, |
58c837a4 | 94 | left + wxT("#zip:") + right, |
5526e819 | 95 | GetMimeTypeFromExt(location), |
6ee654e6 VS |
96 | GetAnchor(location), |
97 | wxDateTime(wxFileModificationTime(left))); | |
5526e819 | 98 | } |
3ca6a5f0 BP |
99 | |
100 | delete s; | |
101 | return NULL; | |
5526e819 VS |
102 | } |
103 | ||
104 | ||
105 | ||
aaa66113 VS |
106 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) |
107 | { | |
108 | wxString right = GetRightLocation(spec); | |
109 | wxString left = GetLeftLocation(spec); | |
110 | ||
111 | if (right.Last() == wxT('/')) right.RemoveLast(); | |
112 | ||
095472c0 VS |
113 | if (m_Archive) |
114 | { | |
aaa66113 VS |
115 | unzClose((unzFile)m_Archive); |
116 | m_Archive = NULL; | |
117 | } | |
118 | ||
de0702d0 VS |
119 | if (GetProtocol(left) != wxT("file")) |
120 | { | |
121 | wxLogError(_("ZIP handler currently supports only local files!")); | |
aaa66113 | 122 | return wxEmptyString; |
de0702d0 | 123 | } |
aaa66113 | 124 | |
095472c0 VS |
125 | switch (flags) |
126 | { | |
127 | case wxFILE: | |
128 | m_AllowDirs = FALSE, m_AllowFiles = TRUE; break; | |
129 | case wxDIR: | |
130 | m_AllowDirs = TRUE, m_AllowFiles = FALSE; break; | |
131 | default: | |
132 | m_AllowDirs = m_AllowFiles = TRUE; break; | |
aaa66113 VS |
133 | } |
134 | ||
135 | m_ZipFile = left; | |
f6bcfd97 | 136 | m_Archive = (void*) unzOpen(m_ZipFile.mb_str()); |
aaa66113 VS |
137 | m_Pattern = right.AfterLast(wxT('/')); |
138 | m_BaseDir = right.BeforeLast(wxT('/')); | |
139 | ||
095472c0 VS |
140 | if (m_Archive) |
141 | { | |
142 | if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK) | |
143 | { | |
aaa66113 VS |
144 | unzClose((unzFile)m_Archive); |
145 | m_Archive = NULL; | |
146 | } | |
de0702d0 VS |
147 | else |
148 | { | |
149 | if (m_AllowDirs) | |
150 | { | |
151 | delete m_DirsFound; | |
152 | m_DirsFound = new wxHashTableLong(); | |
153 | } | |
154 | return DoFind(); | |
155 | } | |
aaa66113 | 156 | } |
095472c0 | 157 | return wxEmptyString; |
aaa66113 VS |
158 | } |
159 | ||
160 | ||
161 | ||
162 | wxString wxZipFSHandler::FindNext() | |
5526e819 | 163 | { |
aaa66113 VS |
164 | if (!m_Archive) return wxEmptyString; |
165 | return DoFind(); | |
5526e819 VS |
166 | } |
167 | ||
aaa66113 VS |
168 | |
169 | ||
170 | wxString wxZipFSHandler::DoFind() | |
171 | { | |
172 | static char namebuf[1024]; // char, not wxChar! | |
173 | char *c; | |
de0702d0 | 174 | wxString namestr, dir, filename; |
aaa66113 | 175 | wxString match = wxEmptyString; |
aaa66113 VS |
176 | |
177 | while (match == wxEmptyString) | |
178 | { | |
179 | unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0); | |
58c837a4 | 180 | for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/'); |
de0702d0 | 181 | namestr = namebuf; |
aaa66113 | 182 | |
de0702d0 | 183 | if (m_AllowDirs) |
095472c0 | 184 | { |
de0702d0 VS |
185 | dir = namestr.BeforeLast(wxT('/')); |
186 | while (!dir.IsEmpty()) | |
187 | { | |
188 | long key = 0; | |
189 | for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i]; | |
190 | if (m_DirsFound->Get(key) == wxNOT_FOUND) | |
191 | { | |
192 | m_DirsFound->Put(key, 1); | |
193 | filename = dir.AfterLast(wxT('/')); | |
194 | dir = dir.BeforeLast(wxT('/')); | |
195 | if (!filename.IsEmpty() && m_BaseDir == dir && | |
196 | wxMatchWild(m_Pattern, filename, FALSE)) | |
197 | match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename; | |
198 | } | |
199 | else | |
200 | break; // already tranversed | |
201 | } | |
aaa66113 | 202 | } |
de0702d0 VS |
203 | |
204 | filename = namestr.AfterLast(wxT('/')); | |
205 | dir = namestr.BeforeLast(wxT('/')); | |
206 | if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir && | |
207 | wxMatchWild(m_Pattern, filename, FALSE)) | |
208 | match = m_ZipFile + wxT("#zip:") + namestr; | |
aaa66113 | 209 | |
095472c0 VS |
210 | if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK) |
211 | { | |
aaa66113 VS |
212 | unzClose((unzFile)m_Archive); |
213 | m_Archive = NULL; | |
214 | break; | |
215 | } | |
216 | } | |
217 | ||
218 | return match; | |
219 | } | |
220 | ||
221 | ||
222 | ||
24528b0c VS |
223 | #endif |
224 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |