]>
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$ |
5526e819 VS |
7 | // Licence: wxWindows Licence |
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 | ||
24528b0c | 22 | #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |
e3e717ec | 23 | |
5526e819 | 24 | #ifndef WXPRECOMP |
04dbb646 VZ |
25 | #include "wx/intl.h" |
26 | #include "wx/log.h" | |
5526e819 VS |
27 | #endif |
28 | ||
ba8c1601 | 29 | #include "wx/hashmap.h" |
5526e819 | 30 | #include "wx/filesys.h" |
6001e347 | 31 | #include "wx/zipstrm.h" |
5526e819 VS |
32 | #include "wx/fs_zip.h" |
33 | ||
aaa66113 VS |
34 | /* Not the right solution (paths in makefiles) but... */ |
35 | #ifdef __BORLANDC__ | |
36 | #include "../common/unzip.h" | |
37 | #else | |
38 | #include "unzip.h" | |
39 | #endif | |
40 | ||
7c4728f6 VS |
41 | WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual, |
42 | wxLongToLongHashMap, class WXDLLIMPEXP_BASE ); | |
5526e819 | 43 | |
ddaf5566 | 44 | //---------------------------------------------------------------------------- |
5526e819 | 45 | // wxZipFSHandler |
ddaf5566 | 46 | //---------------------------------------------------------------------------- |
5526e819 VS |
47 | |
48 | ||
49 | ||
aaa66113 VS |
50 | wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler() |
51 | { | |
52 | m_Archive = NULL; | |
53 | m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString; | |
54 | m_AllowDirs = m_AllowFiles = TRUE; | |
de0702d0 | 55 | m_DirsFound = NULL; |
aaa66113 VS |
56 | } |
57 | ||
58 | ||
59 | ||
60 | wxZipFSHandler::~wxZipFSHandler() | |
61 | { | |
505710ca | 62 | if (m_Archive) |
aaa66113 | 63 | unzClose((unzFile)m_Archive); |
de0702d0 VS |
64 | if (m_DirsFound) |
65 | delete m_DirsFound; | |
aaa66113 VS |
66 | } |
67 | ||
68 | ||
69 | ||
5526e819 VS |
70 | bool wxZipFSHandler::CanOpen(const wxString& location) |
71 | { | |
72 | wxString p = GetProtocol(location); | |
505710ca | 73 | return (p == wxT("zip")) && |
08b50c2c | 74 | (GetProtocol(GetLeftLocation(location)) == wxT("file")); |
5526e819 VS |
75 | } |
76 | ||
77 | ||
78 | ||
79 | ||
80 | wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
81 | { | |
82 | wxString right = GetRightLocation(location); | |
83 | wxString left = GetLeftLocation(location); | |
84 | wxInputStream *s; | |
85 | ||
505710ca | 86 | if (GetProtocol(left) != wxT("file")) |
095472c0 VS |
87 | { |
88 | wxLogError(_("ZIP handler currently supports only local files!")); | |
5526e819 VS |
89 | return NULL; |
90 | } | |
91 | ||
ddaf5566 VS |
92 | if (right.Contains(wxT("./"))) |
93 | { | |
94 | if (right.GetChar(0) != wxT('/')) right = wxT('/') + right; | |
95 | wxFileName rightPart(right, wxPATH_UNIX); | |
96 | rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX); | |
97 | right = rightPart.GetFullPath(wxPATH_UNIX); | |
98 | } | |
99 | ||
505710ca KB |
100 | if (right.GetChar(0) == wxT('/')) right = right.Mid(1); |
101 | ||
9548c49a | 102 | wxFileName leftFilename = wxFileSystem::URLToFileName(left); |
2b5f62a0 | 103 | |
9548c49a | 104 | s = new wxZipInputStream(leftFilename.GetFullPath(), right); |
2b5f62a0 | 105 | if (s && s->IsOk() ) |
095472c0 | 106 | { |
5526e819 | 107 | return new wxFSFile(s, |
58c837a4 | 108 | left + wxT("#zip:") + right, |
5526e819 | 109 | GetMimeTypeFromExt(location), |
e2b87f38 VZ |
110 | GetAnchor(location) |
111 | #if wxUSE_DATETIME | |
112 | , wxDateTime(wxFileModificationTime(left)) | |
113 | #endif // wxUSE_DATETIME | |
114 | ); | |
5526e819 | 115 | } |
3ca6a5f0 BP |
116 | |
117 | delete s; | |
118 | return NULL; | |
5526e819 VS |
119 | } |
120 | ||
121 | ||
122 | ||
aaa66113 VS |
123 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) |
124 | { | |
125 | wxString right = GetRightLocation(spec); | |
126 | wxString left = GetLeftLocation(spec); | |
505710ca | 127 | |
aaa66113 VS |
128 | if (right.Last() == wxT('/')) right.RemoveLast(); |
129 | ||
505710ca | 130 | if (m_Archive) |
095472c0 | 131 | { |
aaa66113 VS |
132 | unzClose((unzFile)m_Archive); |
133 | m_Archive = NULL; | |
134 | } | |
135 | ||
de0702d0 VS |
136 | if (GetProtocol(left) != wxT("file")) |
137 | { | |
138 | wxLogError(_("ZIP handler currently supports only local files!")); | |
aaa66113 | 139 | return wxEmptyString; |
de0702d0 | 140 | } |
aaa66113 | 141 | |
505710ca | 142 | switch (flags) |
095472c0 | 143 | { |
505710ca | 144 | case wxFILE: |
095472c0 | 145 | m_AllowDirs = FALSE, m_AllowFiles = TRUE; break; |
505710ca | 146 | case wxDIR: |
095472c0 | 147 | m_AllowDirs = TRUE, m_AllowFiles = FALSE; break; |
505710ca | 148 | default: |
095472c0 | 149 | m_AllowDirs = m_AllowFiles = TRUE; break; |
aaa66113 VS |
150 | } |
151 | ||
152 | m_ZipFile = left; | |
9548c49a VS |
153 | wxString nativename = wxFileSystem::URLToFileName(m_ZipFile).GetFullPath(); |
154 | m_Archive = (void*) unzOpen(nativename.mb_str()); | |
aaa66113 VS |
155 | m_Pattern = right.AfterLast(wxT('/')); |
156 | m_BaseDir = right.BeforeLast(wxT('/')); | |
157 | ||
505710ca | 158 | if (m_Archive) |
095472c0 | 159 | { |
505710ca | 160 | if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK) |
095472c0 | 161 | { |
aaa66113 | 162 | unzClose((unzFile)m_Archive); |
505710ca | 163 | m_Archive = NULL; |
aaa66113 | 164 | } |
505710ca | 165 | else |
de0702d0 VS |
166 | { |
167 | if (m_AllowDirs) | |
168 | { | |
169 | delete m_DirsFound; | |
ba8c1601 | 170 | m_DirsFound = new wxLongToLongHashMap(); |
de0702d0 VS |
171 | } |
172 | return DoFind(); | |
173 | } | |
aaa66113 | 174 | } |
095472c0 | 175 | return wxEmptyString; |
aaa66113 VS |
176 | } |
177 | ||
178 | ||
179 | ||
180 | wxString wxZipFSHandler::FindNext() | |
5526e819 | 181 | { |
aaa66113 VS |
182 | if (!m_Archive) return wxEmptyString; |
183 | return DoFind(); | |
5526e819 VS |
184 | } |
185 | ||
aaa66113 VS |
186 | |
187 | ||
188 | wxString wxZipFSHandler::DoFind() | |
189 | { | |
190 | static char namebuf[1024]; // char, not wxChar! | |
191 | char *c; | |
de0702d0 | 192 | wxString namestr, dir, filename; |
aaa66113 | 193 | wxString match = wxEmptyString; |
aaa66113 VS |
194 | |
195 | while (match == wxEmptyString) | |
196 | { | |
197 | unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0); | |
2b5f62a0 VZ |
198 | for (c = namebuf; *c; c++) if (*c == '\\') *c = '/'; |
199 | namestr = wxString::FromAscii( namebuf ); // TODO what encoding does ZIP use? | |
aaa66113 | 200 | |
de0702d0 | 201 | if (m_AllowDirs) |
095472c0 | 202 | { |
505710ca | 203 | dir = namestr.BeforeLast(wxT('/')); |
de0702d0 VS |
204 | while (!dir.IsEmpty()) |
205 | { | |
206 | long key = 0; | |
207 | for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i]; | |
ba8c1601 MB |
208 | wxLongToLongHashMap::iterator it = m_DirsFound->find(key); |
209 | if (it == m_DirsFound->end()) | |
de0702d0 | 210 | { |
d8771ac7 | 211 | (*m_DirsFound)[key] = 1; |
de0702d0 VS |
212 | filename = dir.AfterLast(wxT('/')); |
213 | dir = dir.BeforeLast(wxT('/')); | |
214 | if (!filename.IsEmpty() && m_BaseDir == dir && | |
215 | wxMatchWild(m_Pattern, filename, FALSE)) | |
216 | match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename; | |
217 | } | |
505710ca | 218 | else |
de0702d0 VS |
219 | break; // already tranversed |
220 | } | |
aaa66113 | 221 | } |
de0702d0 VS |
222 | |
223 | filename = namestr.AfterLast(wxT('/')); | |
224 | dir = namestr.BeforeLast(wxT('/')); | |
225 | if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir && | |
226 | wxMatchWild(m_Pattern, filename, FALSE)) | |
227 | match = m_ZipFile + wxT("#zip:") + namestr; | |
505710ca KB |
228 | |
229 | if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK) | |
095472c0 | 230 | { |
aaa66113 VS |
231 | unzClose((unzFile)m_Archive); |
232 | m_Archive = NULL; | |
233 | break; | |
234 | } | |
235 | } | |
505710ca | 236 | |
aaa66113 VS |
237 | return match; |
238 | } | |
239 | ||
240 | ||
241 | ||
505710ca | 242 | #endif |
24528b0c | 243 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |