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