]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: fs_zip.cpp | |
3 | // Purpose: ZIP file system | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // CVS-ID: $Id$ | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "fs_zip.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM && wxUSE_ZLIB | |
23 | ||
24 | #ifndef WXPRECOMP | |
25 | #include "wx/intl.h" | |
26 | #include "wx/log.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/filesys.h" | |
30 | #include "wx/wfstream.h" | |
31 | #include "wx/zipstrm.h" | |
32 | #include "wx/fs_zip.h" | |
33 | ||
34 | ||
35 | //--------------------------------------------------------------------------- | |
36 | // wxZipFSInputStream | |
37 | //--------------------------------------------------------------------------- | |
38 | // Helper class for wxZipFSHandler | |
39 | ||
40 | class wxZipFSInputStream : public wxZipInputStream | |
41 | { | |
42 | public: | |
43 | wxZipFSInputStream(wxFSFile *file) | |
44 | : wxZipInputStream(*file->GetStream()) | |
45 | { | |
46 | m_file = file; | |
47 | #if 1 //WXWIN_COMPATIBILITY_2_6 | |
48 | m_allowSeeking = true; | |
49 | #endif | |
50 | } | |
51 | ||
52 | virtual ~wxZipFSInputStream() { delete m_file; } | |
53 | ||
54 | private: | |
55 | wxFSFile *m_file; | |
56 | }; | |
57 | ||
58 | //---------------------------------------------------------------------------- | |
59 | // wxZipFSHandler | |
60 | //---------------------------------------------------------------------------- | |
61 | ||
62 | wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler() | |
63 | { | |
64 | m_Archive = NULL; | |
65 | m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString; | |
66 | m_AllowDirs = m_AllowFiles = true; | |
67 | m_DirsFound = NULL; | |
68 | } | |
69 | ||
70 | ||
71 | ||
72 | wxZipFSHandler::~wxZipFSHandler() | |
73 | { | |
74 | if (m_Archive) | |
75 | delete m_Archive; | |
76 | if (m_DirsFound) | |
77 | delete m_DirsFound; | |
78 | } | |
79 | ||
80 | ||
81 | ||
82 | bool wxZipFSHandler::CanOpen(const wxString& location) | |
83 | { | |
84 | wxString p = GetProtocol(location); | |
85 | return (p == wxT("zip")); | |
86 | } | |
87 | ||
88 | ||
89 | wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
90 | { | |
91 | wxString right = GetRightLocation(location); | |
92 | wxString left = GetLeftLocation(location); | |
93 | wxZipInputStream *s; | |
94 | ||
95 | if (right.Contains(wxT("./"))) | |
96 | { | |
97 | if (right.GetChar(0) != wxT('/')) right = wxT('/') + right; | |
98 | wxFileName rightPart(right, wxPATH_UNIX); | |
99 | rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX); | |
100 | right = rightPart.GetFullPath(wxPATH_UNIX); | |
101 | } | |
102 | ||
103 | if (right.GetChar(0) == wxT('/')) right = right.Mid(1); | |
104 | ||
105 | // a new wxFileSystem object is needed here to avoid infinite recursion | |
106 | wxFSFile *leftFile = wxFileSystem().OpenFile(left); | |
107 | if (!leftFile) | |
108 | return NULL; | |
109 | ||
110 | s = new wxZipFSInputStream(leftFile); | |
111 | if (s && s->IsOk()) | |
112 | { | |
113 | bool found = false; | |
114 | while (!found) | |
115 | { | |
116 | wxZipEntry *ent = s->GetNextEntry(); | |
117 | if (!ent) | |
118 | break; | |
119 | if (ent->GetInternalName() == right) | |
120 | found = true; | |
121 | delete ent; | |
122 | } | |
123 | if (found) | |
124 | return new wxFSFile(s, | |
125 | left + wxT("#zip:") + right, | |
126 | GetMimeTypeFromExt(location), | |
127 | GetAnchor(location) | |
128 | #if wxUSE_DATETIME | |
129 | , wxDateTime(wxFileModificationTime(left)) | |
130 | #endif // wxUSE_DATETIME | |
131 | ); | |
132 | } | |
133 | ||
134 | delete s; | |
135 | return NULL; | |
136 | } | |
137 | ||
138 | ||
139 | ||
140 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) | |
141 | { | |
142 | wxString right = GetRightLocation(spec); | |
143 | wxString left = GetLeftLocation(spec); | |
144 | ||
145 | if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast(); | |
146 | ||
147 | if (m_Archive) | |
148 | { | |
149 | delete m_Archive; | |
150 | m_Archive = NULL; | |
151 | } | |
152 | ||
153 | switch (flags) | |
154 | { | |
155 | case wxFILE: | |
156 | m_AllowDirs = false, m_AllowFiles = true; break; | |
157 | case wxDIR: | |
158 | m_AllowDirs = true, m_AllowFiles = false; break; | |
159 | default: | |
160 | m_AllowDirs = m_AllowFiles = true; break; | |
161 | } | |
162 | ||
163 | m_ZipFile = left; | |
164 | ||
165 | wxFSFile *leftFile = wxFileSystem().OpenFile(left); | |
166 | if (leftFile) | |
167 | m_Archive = new wxZipFSInputStream(leftFile); | |
168 | ||
169 | m_Pattern = right.AfterLast(wxT('/')); | |
170 | m_BaseDir = right.BeforeLast(wxT('/')); | |
171 | if (m_BaseDir.StartsWith(wxT("/"))) | |
172 | m_BaseDir = m_BaseDir.Mid(1); | |
173 | ||
174 | if (m_Archive) | |
175 | { | |
176 | if (m_AllowDirs) | |
177 | { | |
178 | delete m_DirsFound; | |
179 | m_DirsFound = new wxZipFilenameHashMap(); | |
180 | if (right.empty()) // allow "/" to match the archive root | |
181 | return spec; | |
182 | } | |
183 | return DoFind(); | |
184 | } | |
185 | return wxEmptyString; | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | wxString wxZipFSHandler::FindNext() | |
191 | { | |
192 | if (!m_Archive) return wxEmptyString; | |
193 | return DoFind(); | |
194 | } | |
195 | ||
196 | ||
197 | ||
198 | wxString wxZipFSHandler::DoFind() | |
199 | { | |
200 | wxString namestr, dir, filename; | |
201 | wxString match = wxEmptyString; | |
202 | ||
203 | while (match == wxEmptyString) | |
204 | { | |
205 | wxZipEntry *entry = m_Archive->GetNextEntry(); | |
206 | if (!entry) | |
207 | { | |
208 | delete m_Archive; | |
209 | m_Archive = NULL; | |
210 | break; | |
211 | } | |
212 | namestr = entry->GetName(wxPATH_UNIX); | |
213 | delete entry; | |
214 | ||
215 | if (m_AllowDirs) | |
216 | { | |
217 | dir = namestr.BeforeLast(wxT('/')); | |
218 | while (!dir.empty()) | |
219 | { | |
220 | if( m_DirsFound->find(dir) == m_DirsFound->end() ) | |
221 | { | |
222 | (*m_DirsFound)[dir] = 1; | |
223 | filename = dir.AfterLast(wxT('/')); | |
224 | dir = dir.BeforeLast(wxT('/')); | |
225 | if (!filename.empty() && m_BaseDir == dir && | |
226 | wxMatchWild(m_Pattern, filename, false)) | |
227 | match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename; | |
228 | } | |
229 | else | |
230 | break; // already tranversed | |
231 | } | |
232 | } | |
233 | ||
234 | filename = namestr.AfterLast(wxT('/')); | |
235 | dir = namestr.BeforeLast(wxT('/')); | |
236 | if (m_AllowFiles && !filename.empty() && m_BaseDir == dir && | |
237 | wxMatchWild(m_Pattern, filename, false)) | |
238 | match = m_ZipFile + wxT("#zip:") + namestr; | |
239 | } | |
240 | ||
241 | return match; | |
242 | } | |
243 | ||
244 | ||
245 | ||
246 | #endif | |
247 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |