]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
added a WXDLLEXPORT
[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 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORDLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FS_ZIP
21
22 #ifndef WXPRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #include "wx/filesys.h"
27 #include "wx/zipstrm.h"
28 #include "wx/fs_zip.h"
29
30 /* Not the right solution (paths in makefiles) but... */
31 #ifdef __BORLANDC__
32 #include "../common/unzip.h"
33 #else
34 #include "unzip.h"
35 #endif
36
37
38 //--------------------------------------------------------------------------------
39 // wxZipFSHandler
40 //--------------------------------------------------------------------------------
41
42
43
44 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
45 {
46 m_Archive = NULL;
47 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
48 m_AllowDirs = m_AllowFiles = TRUE;
49 }
50
51
52
53 wxZipFSHandler::~wxZipFSHandler()
54 {
55 if (m_Archive)
56 unzClose((unzFile)m_Archive);
57 }
58
59
60
61 bool wxZipFSHandler::CanOpen(const wxString& location)
62 {
63 wxString p = GetProtocol(location);
64 return (p == "zip");
65 }
66
67
68
69
70 wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
71 {
72 wxString right = GetRightLocation(location);
73 wxString left = GetLeftLocation(location);
74 wxInputStream *s;
75
76 if (GetProtocol(left) != "file") {
77 return NULL;
78 }
79
80 s = new wxZipInputStream(left, right);
81 if (s && (s -> LastError() == wxStream_NOERROR)) {
82 return new wxFSFile(s,
83 left + "#zip:" + right,
84 GetMimeTypeFromExt(location),
85 GetAnchor(location));
86 }
87 else return NULL;
88 }
89
90
91
92 wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
93 {
94 wxString right = GetRightLocation(spec);
95 wxString left = GetLeftLocation(spec);
96
97 if (right.Last() == wxT('/')) right.RemoveLast();
98
99 if (m_Archive) {
100 unzClose((unzFile)m_Archive);
101 m_Archive = NULL;
102 }
103
104 if (GetProtocol(left) != "file") {
105 return wxEmptyString;
106 }
107
108 switch (flags) {
109 case wxFILE : m_AllowDirs = FALSE, m_AllowFiles = TRUE; break;
110 case wxDIR : m_AllowDirs = TRUE, m_AllowFiles = FALSE; break;
111 default : m_AllowDirs = m_AllowFiles = TRUE; break;
112 }
113
114 m_ZipFile = left;
115 m_Archive = (void*) unzOpen(m_ZipFile.fn_str());
116 m_Pattern = right.AfterLast(wxT('/'));
117 m_BaseDir = right.BeforeLast(wxT('/'));
118
119 if (m_Archive) {
120 if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK) {
121 unzClose((unzFile)m_Archive);
122 m_Archive = NULL;
123 }
124 }
125 return DoFind();
126 }
127
128
129
130 wxString wxZipFSHandler::FindNext()
131 {
132 if (!m_Archive) return wxEmptyString;
133 return DoFind();
134 }
135
136
137
138 wxString wxZipFSHandler::DoFind()
139 {
140 static char namebuf[1024]; // char, not wxChar!
141 char *c;
142 wxString fn, dir, name;
143 wxString match = wxEmptyString;
144 bool wasdir;
145
146 while (match == wxEmptyString)
147 {
148 unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0);
149 for (c = namebuf; *c; c++) if (*c == '\\') *c = '/';
150 fn = namebuf;
151 if (fn.Last() == wxT('/')) {
152 fn.RemoveLast();
153 wasdir = TRUE;
154 }
155 else wasdir = FALSE;
156
157 name = fn.AfterLast(wxT('/'));
158 dir = fn.BeforeLast(wxT('/'));
159
160 if (dir == m_BaseDir) {
161 if (m_AllowFiles && !wasdir && wxMatchWild(m_Pattern, name, FALSE))
162 match = m_ZipFile + wxT("#zip:") + fn;
163 if (m_AllowDirs && wasdir && wxMatchWild(m_Pattern, name, FALSE))
164 match = m_ZipFile + wxT("#zip:") + fn;
165 }
166
167 if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK) {
168 unzClose((unzFile)m_Archive);
169 m_Archive = NULL;
170 break;
171 }
172 }
173
174 return match;
175 }
176
177
178
179 #endif // wxUSE_FS_ZIP