]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_zip.cpp
Update to zlib 1.2.3
[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
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
40class 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
62wxZipFSHandler::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
72wxZipFSHandler::~wxZipFSHandler()
73{
f60b1d82 74 Cleanup();
aaa66113
VS
75}
76
77
f60b1d82
VZ
78void wxZipFSHandler::Cleanup()
79{
80 wxDELETE(m_Archive);
81 wxDELETE(m_DirsFound);
82}
83
84
aaa66113 85
5526e819
VS
86bool wxZipFSHandler::CanOpen(const wxString& location)
87{
88 wxString p = GetProtocol(location);
08335000 89 return (p == wxT("zip"));
5526e819
VS
90}
91
92
ba75c6bb 93wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
5526e819
VS
94{
95 wxString right = GetRightLocation(location);
96 wxString left = GetLeftLocation(location);
08335000 97 wxZipInputStream *s;
5526e819 98
ddaf5566
VS
99 if (right.Contains(wxT("./")))
100 {
101 if (right.GetChar(0) != wxT('/')) right = wxT('/') + right;
102 wxFileName rightPart(right, wxPATH_UNIX);
103 rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX);
104 right = rightPart.GetFullPath(wxPATH_UNIX);
105 }
a62848fd 106
505710ca
KB
107 if (right.GetChar(0) == wxT('/')) right = right.Mid(1);
108
ba75c6bb
MW
109 // a new wxFileSystem object is needed here to avoid infinite recursion
110 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
08335000
MW
111 if (!leftFile)
112 return NULL;
2b5f62a0 113
08335000
MW
114 s = new wxZipFSInputStream(leftFile);
115 if (s && s->IsOk())
095472c0 116 {
08335000 117 bool found = false;
49178e65
WS
118 while (!found)
119 {
120 wxZipEntry *ent = s->GetNextEntry();
121 if (!ent)
122 break;
08335000
MW
123 if (ent->GetInternalName() == right)
124 found = true;
125 delete ent;
126 }
127 if (found)
128 return new wxFSFile(s,
58c837a4 129 left + wxT("#zip:") + right,
5526e819 130 GetMimeTypeFromExt(location),
e2b87f38
VZ
131 GetAnchor(location)
132#if wxUSE_DATETIME
133 , wxDateTime(wxFileModificationTime(left))
134#endif // wxUSE_DATETIME
135 );
5526e819 136 }
3ca6a5f0
BP
137
138 delete s;
139 return NULL;
5526e819
VS
140}
141
142
143
aaa66113
VS
144wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
145{
146 wxString right = GetRightLocation(spec);
147 wxString left = GetLeftLocation(spec);
505710ca 148
ba75c6bb 149 if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast();
aaa66113 150
505710ca 151 if (m_Archive)
095472c0 152 {
08335000 153 delete m_Archive;
aaa66113
VS
154 m_Archive = NULL;
155 }
156
505710ca 157 switch (flags)
095472c0 158 {
505710ca 159 case wxFILE:
a62848fd 160 m_AllowDirs = false, m_AllowFiles = true; break;
505710ca 161 case wxDIR:
a62848fd 162 m_AllowDirs = true, m_AllowFiles = false; break;
505710ca 163 default:
a62848fd 164 m_AllowDirs = m_AllowFiles = true; break;
aaa66113
VS
165 }
166
167 m_ZipFile = left;
e06cac58 168
08335000
MW
169 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
170 if (leftFile)
171 m_Archive = new wxZipFSInputStream(leftFile);
e06cac58 172
aaa66113
VS
173 m_Pattern = right.AfterLast(wxT('/'));
174 m_BaseDir = right.BeforeLast(wxT('/'));
ba75c6bb
MW
175 if (m_BaseDir.StartsWith(wxT("/")))
176 m_BaseDir = m_BaseDir.Mid(1);
aaa66113 177
505710ca 178 if (m_Archive)
095472c0 179 {
00375592 180 if (m_AllowDirs)
de0702d0 181 {
00375592 182 delete m_DirsFound;
62d9eab6 183 m_DirsFound = new wxZipFilenameHashMap();
ba75c6bb
MW
184 if (right.empty()) // allow "/" to match the archive root
185 return spec;
de0702d0 186 }
00375592 187 return DoFind();
aaa66113 188 }
095472c0 189 return wxEmptyString;
aaa66113
VS
190}
191
192
193
194wxString wxZipFSHandler::FindNext()
5526e819 195{
aaa66113
VS
196 if (!m_Archive) return wxEmptyString;
197 return DoFind();
5526e819
VS
198}
199
aaa66113
VS
200
201
202wxString wxZipFSHandler::DoFind()
203{
de0702d0 204 wxString namestr, dir, filename;
aaa66113 205 wxString match = wxEmptyString;
aaa66113
VS
206
207 while (match == wxEmptyString)
208 {
00375592
VZ
209 wxZipEntry *entry = m_Archive->GetNextEntry();
210 if (!entry)
211 {
08335000 212 delete m_Archive;
00375592
VZ
213 m_Archive = NULL;
214 break;
215 }
216 namestr = entry->GetName(wxPATH_UNIX);
217 delete entry;
aaa66113 218
de0702d0 219 if (m_AllowDirs)
095472c0 220 {
505710ca 221 dir = namestr.BeforeLast(wxT('/'));
489f6cf7 222 while (!dir.empty())
de0702d0 223 {
62d9eab6 224 if( m_DirsFound->find(dir) == m_DirsFound->end() )
de0702d0 225 {
62d9eab6 226 (*m_DirsFound)[dir] = 1;
de0702d0
VS
227 filename = dir.AfterLast(wxT('/'));
228 dir = dir.BeforeLast(wxT('/'));
489f6cf7 229 if (!filename.empty() && m_BaseDir == dir &&
a62848fd 230 wxMatchWild(m_Pattern, filename, false))
de0702d0
VS
231 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
232 }
505710ca 233 else
de0702d0
VS
234 break; // already tranversed
235 }
aaa66113 236 }
de0702d0
VS
237
238 filename = namestr.AfterLast(wxT('/'));
239 dir = namestr.BeforeLast(wxT('/'));
489f6cf7 240 if (m_AllowFiles && !filename.empty() && m_BaseDir == dir &&
a62848fd 241 wxMatchWild(m_Pattern, filename, false))
de0702d0 242 match = m_ZipFile + wxT("#zip:") + namestr;
aaa66113 243 }
505710ca 244
aaa66113
VS
245 return match;
246}
247
248
249
505710ca 250#endif
24528b0c 251 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM