]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
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 | ||
3096bd2f | 14 | #include "wx/wxprec.h" |
5526e819 VS |
15 | |
16 | #ifdef __BORDLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
24528b0c | 20 | #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |
e3e717ec | 21 | |
5526e819 | 22 | #ifndef WXPRECOMP |
3096bd2f | 23 | #include "wx/wx.h" |
5526e819 VS |
24 | #endif |
25 | ||
26 | #include "wx/filesys.h" | |
6001e347 | 27 | #include "wx/zipstrm.h" |
5526e819 VS |
28 | #include "wx/fs_zip.h" |
29 | ||
aaa66113 VS |
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 | ||
5526e819 VS |
37 | |
38 | //-------------------------------------------------------------------------------- | |
39 | // wxZipFSHandler | |
40 | //-------------------------------------------------------------------------------- | |
41 | ||
42 | ||
43 | ||
aaa66113 VS |
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 | ||
5526e819 VS |
61 | bool wxZipFSHandler::CanOpen(const wxString& location) |
62 | { | |
63 | wxString p = GetProtocol(location); | |
095472c0 | 64 | return (p == wxT("zip")); |
5526e819 VS |
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 | ||
095472c0 VS |
76 | if (GetProtocol(left) != wxT("file")) |
77 | { | |
78 | wxLogError(_("ZIP handler currently supports only local files!")); | |
5526e819 VS |
79 | return NULL; |
80 | } | |
81 | ||
82 | s = new wxZipInputStream(left, right); | |
095472c0 VS |
83 | if (s && (s->LastError() == wxStream_NOERROR)) |
84 | { | |
5526e819 | 85 | return new wxFSFile(s, |
58c837a4 | 86 | left + wxT("#zip:") + right, |
5526e819 | 87 | GetMimeTypeFromExt(location), |
6ee654e6 VS |
88 | GetAnchor(location), |
89 | wxDateTime(wxFileModificationTime(left))); | |
5526e819 | 90 | } |
3ca6a5f0 BP |
91 | |
92 | delete s; | |
93 | return NULL; | |
5526e819 VS |
94 | } |
95 | ||
96 | ||
97 | ||
aaa66113 VS |
98 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) |
99 | { | |
100 | wxString right = GetRightLocation(spec); | |
101 | wxString left = GetLeftLocation(spec); | |
102 | ||
103 | if (right.Last() == wxT('/')) right.RemoveLast(); | |
104 | ||
095472c0 VS |
105 | if (m_Archive) |
106 | { | |
aaa66113 VS |
107 | unzClose((unzFile)m_Archive); |
108 | m_Archive = NULL; | |
109 | } | |
110 | ||
095472c0 | 111 | if (GetProtocol(left) != wxT("file")) |
aaa66113 | 112 | return wxEmptyString; |
aaa66113 | 113 | |
095472c0 VS |
114 | switch (flags) |
115 | { | |
116 | case wxFILE: | |
117 | m_AllowDirs = FALSE, m_AllowFiles = TRUE; break; | |
118 | case wxDIR: | |
119 | m_AllowDirs = TRUE, m_AllowFiles = FALSE; break; | |
120 | default: | |
121 | m_AllowDirs = m_AllowFiles = TRUE; break; | |
aaa66113 VS |
122 | } |
123 | ||
124 | m_ZipFile = left; | |
f6bcfd97 | 125 | m_Archive = (void*) unzOpen(m_ZipFile.mb_str()); |
aaa66113 VS |
126 | m_Pattern = right.AfterLast(wxT('/')); |
127 | m_BaseDir = right.BeforeLast(wxT('/')); | |
128 | ||
095472c0 VS |
129 | if (m_Archive) |
130 | { | |
131 | if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK) | |
132 | { | |
aaa66113 VS |
133 | unzClose((unzFile)m_Archive); |
134 | m_Archive = NULL; | |
135 | } | |
095472c0 | 136 | else return DoFind(); |
aaa66113 | 137 | } |
095472c0 | 138 | return wxEmptyString; |
aaa66113 VS |
139 | } |
140 | ||
141 | ||
142 | ||
143 | wxString wxZipFSHandler::FindNext() | |
5526e819 | 144 | { |
aaa66113 VS |
145 | if (!m_Archive) return wxEmptyString; |
146 | return DoFind(); | |
5526e819 VS |
147 | } |
148 | ||
aaa66113 VS |
149 | |
150 | ||
151 | wxString wxZipFSHandler::DoFind() | |
152 | { | |
153 | static char namebuf[1024]; // char, not wxChar! | |
154 | char *c; | |
155 | wxString fn, dir, name; | |
156 | wxString match = wxEmptyString; | |
157 | bool wasdir; | |
158 | ||
159 | while (match == wxEmptyString) | |
160 | { | |
161 | unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0); | |
58c837a4 | 162 | for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/'); |
aaa66113 | 163 | fn = namebuf; |
095472c0 VS |
164 | if (fn.Length() > 0 && fn.Last() == wxT('/')) |
165 | { | |
aaa66113 VS |
166 | fn.RemoveLast(); |
167 | wasdir = TRUE; | |
168 | } | |
169 | else wasdir = FALSE; | |
170 | ||
171 | name = fn.AfterLast(wxT('/')); | |
172 | dir = fn.BeforeLast(wxT('/')); | |
173 | ||
095472c0 VS |
174 | if (dir == m_BaseDir) |
175 | { | |
aaa66113 VS |
176 | if (m_AllowFiles && !wasdir && wxMatchWild(m_Pattern, name, FALSE)) |
177 | match = m_ZipFile + wxT("#zip:") + fn; | |
178 | if (m_AllowDirs && wasdir && wxMatchWild(m_Pattern, name, FALSE)) | |
179 | match = m_ZipFile + wxT("#zip:") + fn; | |
180 | } | |
181 | ||
095472c0 VS |
182 | if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK) |
183 | { | |
aaa66113 VS |
184 | unzClose((unzFile)m_Archive); |
185 | m_Archive = NULL; | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | return match; | |
191 | } | |
192 | ||
193 | ||
194 | ||
24528b0c VS |
195 | #endif |
196 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |