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