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