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