]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
compilation fix for WXWIN_COMPATIBILITY_2_4 == 0
[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 // wxZipFSHandler
37 //----------------------------------------------------------------------------
38
39
40
41 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
42 {
43 m_Archive = NULL;
44 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
45 m_AllowDirs = m_AllowFiles = true;
46 m_DirsFound = NULL;
47 }
48
49
50
51 wxZipFSHandler::~wxZipFSHandler()
52 {
53 if (m_Archive)
54 CloseArchive(m_Archive);
55 if (m_DirsFound)
56 delete m_DirsFound;
57 }
58
59
60
61 void wxZipFSHandler::CloseArchive(wxZipInputStream *archive)
62 {
63 wxInputStream *stream = archive->GetFilterInputStream();
64 delete archive;
65 delete stream;
66 }
67
68
69
70 bool wxZipFSHandler::CanOpen(const wxString& location)
71 {
72 wxString p = GetProtocol(location);
73 return (p == wxT("zip")) &&
74 (GetProtocol(GetLeftLocation(location)) == wxT("file"));
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
86 if (GetProtocol(left) != wxT("file"))
87 {
88 wxLogError(_("ZIP handler currently supports only local files!"));
89 return NULL;
90 }
91
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
100 if (right.GetChar(0) == wxT('/')) right = right.Mid(1);
101
102 wxFileName leftFilename = wxFileSystem::URLToFileName(left);
103
104 s = new wxZipInputStream(leftFilename.GetFullPath(), right);
105 if (s && s->IsOk() )
106 {
107 return new wxFSFile(s,
108 left + wxT("#zip:") + right,
109 GetMimeTypeFromExt(location),
110 GetAnchor(location)
111 #if wxUSE_DATETIME
112 , wxDateTime(wxFileModificationTime(left))
113 #endif // wxUSE_DATETIME
114 );
115 }
116
117 delete s;
118 return NULL;
119 }
120
121
122
123 wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
124 {
125 wxString right = GetRightLocation(spec);
126 wxString left = GetLeftLocation(spec);
127
128 if (right.Last() == wxT('/')) right.RemoveLast();
129
130 if (m_Archive)
131 {
132 CloseArchive(m_Archive);
133 m_Archive = NULL;
134 }
135
136 if (GetProtocol(left) != wxT("file"))
137 {
138 wxLogError(_("ZIP handler currently supports only local files!"));
139 return wxEmptyString;
140 }
141
142 switch (flags)
143 {
144 case wxFILE:
145 m_AllowDirs = false, m_AllowFiles = true; break;
146 case wxDIR:
147 m_AllowDirs = true, m_AllowFiles = false; break;
148 default:
149 m_AllowDirs = m_AllowFiles = true; break;
150 }
151
152 m_ZipFile = left;
153 wxString nativename = wxFileSystem::URLToFileName(m_ZipFile).GetFullPath();
154
155 wxFFileInputStream *fs = new wxFFileInputStream(nativename);
156 if (fs->Ok())
157 m_Archive = new wxZipInputStream(*fs);
158 else
159 delete fs;
160
161 m_Pattern = right.AfterLast(wxT('/'));
162 m_BaseDir = right.BeforeLast(wxT('/'));
163
164 if (m_Archive)
165 {
166 if (m_AllowDirs)
167 {
168 delete m_DirsFound;
169 m_DirsFound = new wxLongToLongHashMap();
170 }
171 return DoFind();
172 }
173 return wxEmptyString;
174 }
175
176
177
178 wxString wxZipFSHandler::FindNext()
179 {
180 if (!m_Archive) return wxEmptyString;
181 return DoFind();
182 }
183
184
185
186 wxString wxZipFSHandler::DoFind()
187 {
188 wxString namestr, dir, filename;
189 wxString match = wxEmptyString;
190
191 while (match == wxEmptyString)
192 {
193 wxZipEntry *entry = m_Archive->GetNextEntry();
194 if (!entry)
195 {
196 CloseArchive(m_Archive);
197 m_Archive = NULL;
198 break;
199 }
200 namestr = entry->GetName(wxPATH_UNIX);
201 delete entry;
202
203 if (m_AllowDirs)
204 {
205 dir = namestr.BeforeLast(wxT('/'));
206 while (!dir.IsEmpty())
207 {
208 long key = 0;
209 for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i];
210 wxLongToLongHashMap::iterator it = m_DirsFound->find(key);
211 if (it == m_DirsFound->end())
212 {
213 (*m_DirsFound)[key] = 1;
214 filename = dir.AfterLast(wxT('/'));
215 dir = dir.BeforeLast(wxT('/'));
216 if (!filename.IsEmpty() && m_BaseDir == dir &&
217 wxMatchWild(m_Pattern, filename, false))
218 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
219 }
220 else
221 break; // already tranversed
222 }
223 }
224
225 filename = namestr.AfterLast(wxT('/'));
226 dir = namestr.BeforeLast(wxT('/'));
227 if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir &&
228 wxMatchWild(m_Pattern, filename, false))
229 match = m_ZipFile + wxT("#zip:") + namestr;
230 }
231
232 return match;
233 }
234
235
236
237 #endif
238 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM