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