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