]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
wxMediaCtrl patch from Ryan:
[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
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 Cleanup();
75 }
76
77
78 void wxZipFSHandler::Cleanup()
79 {
80 wxDELETE(m_Archive);
81 wxDELETE(m_DirsFound);
82 }
83
84
85
86 bool wxZipFSHandler::CanOpen(const wxString& location)
87 {
88 wxString p = GetProtocol(location);
89 return (p == wxT("zip"));
90 }
91
92
93 wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
94 {
95 wxString right = GetRightLocation(location);
96 wxString left = GetLeftLocation(location);
97 wxZipInputStream *s;
98
99 if (right.Contains(wxT("./")))
100 {
101 if (right.GetChar(0) != wxT('/')) right = wxT('/') + right;
102 wxFileName rightPart(right, wxPATH_UNIX);
103 rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX);
104 right = rightPart.GetFullPath(wxPATH_UNIX);
105 }
106
107 if (right.GetChar(0) == wxT('/')) right = right.Mid(1);
108
109 // a new wxFileSystem object is needed here to avoid infinite recursion
110 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
111 if (!leftFile)
112 return NULL;
113
114 s = new wxZipFSInputStream(leftFile);
115 if (s && s->IsOk())
116 {
117 bool found = false;
118 while (!found)
119 {
120 wxZipEntry *ent = s->GetNextEntry();
121 if (!ent)
122 break;
123 if (ent->GetInternalName() == right)
124 found = true;
125 delete ent;
126 }
127 if (found)
128 return new wxFSFile(s,
129 left + wxT("#zip:") + right,
130 GetMimeTypeFromExt(location),
131 GetAnchor(location)
132 #if wxUSE_DATETIME
133 , wxDateTime(wxFileModificationTime(left))
134 #endif // wxUSE_DATETIME
135 );
136 }
137
138 delete s;
139 return NULL;
140 }
141
142
143
144 wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
145 {
146 wxString right = GetRightLocation(spec);
147 wxString left = GetLeftLocation(spec);
148
149 if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast();
150
151 if (m_Archive)
152 {
153 delete m_Archive;
154 m_Archive = NULL;
155 }
156
157 switch (flags)
158 {
159 case wxFILE:
160 m_AllowDirs = false, m_AllowFiles = true; break;
161 case wxDIR:
162 m_AllowDirs = true, m_AllowFiles = false; break;
163 default:
164 m_AllowDirs = m_AllowFiles = true; break;
165 }
166
167 m_ZipFile = left;
168
169 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
170 if (leftFile)
171 m_Archive = new wxZipFSInputStream(leftFile);
172
173 m_Pattern = right.AfterLast(wxT('/'));
174 m_BaseDir = right.BeforeLast(wxT('/'));
175 if (m_BaseDir.StartsWith(wxT("/")))
176 m_BaseDir = m_BaseDir.Mid(1);
177
178 if (m_Archive)
179 {
180 if (m_AllowDirs)
181 {
182 delete m_DirsFound;
183 m_DirsFound = new wxZipFilenameHashMap();
184 if (right.empty()) // allow "/" to match the archive root
185 return spec;
186 }
187 return DoFind();
188 }
189 return wxEmptyString;
190 }
191
192
193
194 wxString wxZipFSHandler::FindNext()
195 {
196 if (!m_Archive) return wxEmptyString;
197 return DoFind();
198 }
199
200
201
202 wxString wxZipFSHandler::DoFind()
203 {
204 wxString namestr, dir, filename;
205 wxString match = wxEmptyString;
206
207 while (match == wxEmptyString)
208 {
209 wxZipEntry *entry = m_Archive->GetNextEntry();
210 if (!entry)
211 {
212 delete m_Archive;
213 m_Archive = NULL;
214 break;
215 }
216 namestr = entry->GetName(wxPATH_UNIX);
217 delete entry;
218
219 if (m_AllowDirs)
220 {
221 dir = namestr.BeforeLast(wxT('/'));
222 while (!dir.empty())
223 {
224 if( m_DirsFound->find(dir) == m_DirsFound->end() )
225 {
226 (*m_DirsFound)[dir] = 1;
227 filename = dir.AfterLast(wxT('/'));
228 dir = dir.BeforeLast(wxT('/'));
229 if (!filename.empty() && m_BaseDir == dir &&
230 wxMatchWild(m_Pattern, filename, false))
231 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
232 }
233 else
234 break; // already tranversed
235 }
236 }
237
238 filename = namestr.AfterLast(wxT('/'));
239 dir = namestr.BeforeLast(wxT('/'));
240 if (m_AllowFiles && !filename.empty() && m_BaseDir == dir &&
241 wxMatchWild(m_Pattern, filename, false))
242 match = m_ZipFile + wxT("#zip:") + namestr;
243 }
244
245 return match;
246 }
247
248
249
250 #endif
251 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM