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