]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_zip.cpp
made wxLocale::GetSystemLanguage and wxLocale::AddLanguage static;
[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
VS
12#ifdef __GNUG__
13#pragma implementation
14#endif
15
3096bd2f 16#include "wx/wxprec.h"
5526e819
VS
17
18#ifdef __BORDLANDC__
19#pragma hdrstop
20#endif
21
24528b0c 22#if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM
e3e717ec 23
5526e819 24#ifndef WXPRECOMP
3096bd2f 25#include "wx/wx.h"
5526e819
VS
26#endif
27
de0702d0 28#include "wx/hash.h"
5526e819 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
5526e819
VS
40
41//--------------------------------------------------------------------------------
42// wxZipFSHandler
43//--------------------------------------------------------------------------------
44
45
46
aaa66113
VS
47wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
48{
49 m_Archive = NULL;
50 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
51 m_AllowDirs = m_AllowFiles = TRUE;
de0702d0 52 m_DirsFound = NULL;
aaa66113
VS
53}
54
55
56
57wxZipFSHandler::~wxZipFSHandler()
58{
59 if (m_Archive)
60 unzClose((unzFile)m_Archive);
de0702d0
VS
61 if (m_DirsFound)
62 delete m_DirsFound;
aaa66113
VS
63}
64
65
66
5526e819
VS
67bool wxZipFSHandler::CanOpen(const wxString& location)
68{
69 wxString p = GetProtocol(location);
095472c0 70 return (p == wxT("zip"));
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
095472c0
VS
82 if (GetProtocol(left) != wxT("file"))
83 {
84 wxLogError(_("ZIP handler currently supports only local files!"));
5526e819
VS
85 return NULL;
86 }
87
88 s = new wxZipInputStream(left, right);
095472c0
VS
89 if (s && (s->LastError() == wxStream_NOERROR))
90 {
5526e819 91 return new wxFSFile(s,
58c837a4 92 left + wxT("#zip:") + right,
5526e819 93 GetMimeTypeFromExt(location),
6ee654e6
VS
94 GetAnchor(location),
95 wxDateTime(wxFileModificationTime(left)));
5526e819 96 }
3ca6a5f0
BP
97
98 delete s;
99 return NULL;
5526e819
VS
100}
101
102
103
aaa66113
VS
104wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
105{
106 wxString right = GetRightLocation(spec);
107 wxString left = GetLeftLocation(spec);
108
109 if (right.Last() == wxT('/')) right.RemoveLast();
110
095472c0
VS
111 if (m_Archive)
112 {
aaa66113
VS
113 unzClose((unzFile)m_Archive);
114 m_Archive = NULL;
115 }
116
de0702d0
VS
117 if (GetProtocol(left) != wxT("file"))
118 {
119 wxLogError(_("ZIP handler currently supports only local files!"));
aaa66113 120 return wxEmptyString;
de0702d0 121 }
aaa66113 122
095472c0
VS
123 switch (flags)
124 {
125 case wxFILE:
126 m_AllowDirs = FALSE, m_AllowFiles = TRUE; break;
127 case wxDIR:
128 m_AllowDirs = TRUE, m_AllowFiles = FALSE; break;
129 default:
130 m_AllowDirs = m_AllowFiles = TRUE; break;
aaa66113
VS
131 }
132
133 m_ZipFile = left;
f6bcfd97 134 m_Archive = (void*) unzOpen(m_ZipFile.mb_str());
aaa66113
VS
135 m_Pattern = right.AfterLast(wxT('/'));
136 m_BaseDir = right.BeforeLast(wxT('/'));
137
095472c0
VS
138 if (m_Archive)
139 {
140 if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK)
141 {
aaa66113
VS
142 unzClose((unzFile)m_Archive);
143 m_Archive = NULL;
144 }
de0702d0
VS
145 else
146 {
147 if (m_AllowDirs)
148 {
149 delete m_DirsFound;
150 m_DirsFound = new wxHashTableLong();
151 }
152 return DoFind();
153 }
aaa66113 154 }
095472c0 155 return wxEmptyString;
aaa66113
VS
156}
157
158
159
160wxString wxZipFSHandler::FindNext()
5526e819 161{
aaa66113
VS
162 if (!m_Archive) return wxEmptyString;
163 return DoFind();
5526e819
VS
164}
165
aaa66113
VS
166
167
168wxString wxZipFSHandler::DoFind()
169{
170 static char namebuf[1024]; // char, not wxChar!
171 char *c;
de0702d0 172 wxString namestr, dir, filename;
aaa66113 173 wxString match = wxEmptyString;
aaa66113
VS
174
175 while (match == wxEmptyString)
176 {
177 unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0);
58c837a4 178 for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/');
de0702d0 179 namestr = namebuf;
aaa66113 180
de0702d0 181 if (m_AllowDirs)
095472c0 182 {
de0702d0
VS
183 dir = namestr.BeforeLast(wxT('/'));
184 while (!dir.IsEmpty())
185 {
186 long key = 0;
187 for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i];
188 if (m_DirsFound->Get(key) == wxNOT_FOUND)
189 {
190 m_DirsFound->Put(key, 1);
191 filename = dir.AfterLast(wxT('/'));
192 dir = dir.BeforeLast(wxT('/'));
193 if (!filename.IsEmpty() && m_BaseDir == dir &&
194 wxMatchWild(m_Pattern, filename, FALSE))
195 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
196 }
197 else
198 break; // already tranversed
199 }
aaa66113 200 }
de0702d0
VS
201
202 filename = namestr.AfterLast(wxT('/'));
203 dir = namestr.BeforeLast(wxT('/'));
204 if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir &&
205 wxMatchWild(m_Pattern, filename, FALSE))
206 match = m_ZipFile + wxT("#zip:") + namestr;
aaa66113 207
095472c0
VS
208 if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK)
209 {
aaa66113
VS
210 unzClose((unzFile)m_Archive);
211 m_Archive = NULL;
212 break;
213 }
214 }
215
216 return match;
217}
218
219
220
24528b0c
VS
221#endif
222 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM