]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
Use wxFileSystem for left hand side. Patches 1169934 and 1173497 from Stas
[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 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& 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 wxFSFile *leftFile = fs.OpenFile(left);
106 if (!leftFile)
107 return NULL;
108
109 s = new wxZipFSInputStream(leftFile);
110 if (s && s->IsOk())
111 {
112 wxZipEntry *ent;
113 bool found = false;
114 while (!found && (ent = s->GetNextEntry())) {
115 if (ent->GetInternalName() == right)
116 found = true;
117 delete ent;
118 }
119 if (found)
120 return new wxFSFile(s,
121 left + wxT("#zip:") + right,
122 GetMimeTypeFromExt(location),
123 GetAnchor(location)
124 #if wxUSE_DATETIME
125 , wxDateTime(wxFileModificationTime(left))
126 #endif // wxUSE_DATETIME
127 );
128 }
129
130 delete s;
131 return NULL;
132 }
133
134
135
136 wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
137 {
138 wxString right = GetRightLocation(spec);
139 wxString left = GetLeftLocation(spec);
140
141 if (right.Last() == wxT('/')) right.RemoveLast();
142
143 if (m_Archive)
144 {
145 delete m_Archive;
146 m_Archive = NULL;
147 }
148
149 switch (flags)
150 {
151 case wxFILE:
152 m_AllowDirs = false, m_AllowFiles = true; break;
153 case wxDIR:
154 m_AllowDirs = true, m_AllowFiles = false; break;
155 default:
156 m_AllowDirs = m_AllowFiles = true; break;
157 }
158
159 m_ZipFile = left;
160
161 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
162 if (leftFile)
163 m_Archive = new wxZipFSInputStream(leftFile);
164
165 m_Pattern = right.AfterLast(wxT('/'));
166 m_BaseDir = right.BeforeLast(wxT('/'));
167
168 if (m_Archive)
169 {
170 if (m_AllowDirs)
171 {
172 delete m_DirsFound;
173 m_DirsFound = new wxZipFilenameHashMap();
174 }
175 return DoFind();
176 }
177 return wxEmptyString;
178 }
179
180
181
182 wxString wxZipFSHandler::FindNext()
183 {
184 if (!m_Archive) return wxEmptyString;
185 return DoFind();
186 }
187
188
189
190 wxString wxZipFSHandler::DoFind()
191 {
192 wxString namestr, dir, filename;
193 wxString match = wxEmptyString;
194
195 while (match == wxEmptyString)
196 {
197 wxZipEntry *entry = m_Archive->GetNextEntry();
198 if (!entry)
199 {
200 delete m_Archive;
201 m_Archive = NULL;
202 break;
203 }
204 namestr = entry->GetName(wxPATH_UNIX);
205 delete entry;
206
207 if (m_AllowDirs)
208 {
209 dir = namestr.BeforeLast(wxT('/'));
210 while (!dir.empty())
211 {
212 if( m_DirsFound->find(dir) == m_DirsFound->end() )
213 {
214 (*m_DirsFound)[dir] = 1;
215 filename = dir.AfterLast(wxT('/'));
216 dir = dir.BeforeLast(wxT('/'));
217 if (!filename.empty() && m_BaseDir == dir &&
218 wxMatchWild(m_Pattern, filename, false))
219 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
220 }
221 else
222 break; // already tranversed
223 }
224 }
225
226 filename = namestr.AfterLast(wxT('/'));
227 dir = namestr.BeforeLast(wxT('/'));
228 if (m_AllowFiles && !filename.empty() && m_BaseDir == dir &&
229 wxMatchWild(m_Pattern, filename, false))
230 match = m_ZipFile + wxT("#zip:") + namestr;
231 }
232
233 return match;
234 }
235
236
237
238 #endif
239 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM