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