]>
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); | |
58c837a4 | 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 | ||
58c837a4 | 76 | if (GetProtocol(left) != wxT("file")) { |
5526e819 VS |
77 | return NULL; |
78 | } | |
79 | ||
80 | s = new wxZipInputStream(left, right); | |
81 | if (s && (s -> LastError() == wxStream_NOERROR)) { | |
82 | return new wxFSFile(s, | |
58c837a4 | 83 | left + wxT("#zip:") + right, |
5526e819 | 84 | GetMimeTypeFromExt(location), |
6ee654e6 VS |
85 | GetAnchor(location), |
86 | wxDateTime(wxFileModificationTime(left))); | |
5526e819 VS |
87 | } |
88 | else return NULL; | |
89 | } | |
90 | ||
91 | ||
92 | ||
aaa66113 VS |
93 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) |
94 | { | |
95 | wxString right = GetRightLocation(spec); | |
96 | wxString left = GetLeftLocation(spec); | |
97 | ||
98 | if (right.Last() == wxT('/')) right.RemoveLast(); | |
99 | ||
100 | if (m_Archive) { | |
101 | unzClose((unzFile)m_Archive); | |
102 | m_Archive = NULL; | |
103 | } | |
104 | ||
58c837a4 | 105 | if (GetProtocol(left) != wxT("file")) { |
aaa66113 VS |
106 | return wxEmptyString; |
107 | } | |
108 | ||
109 | switch (flags) { | |
110 | case wxFILE : m_AllowDirs = FALSE, m_AllowFiles = TRUE; break; | |
111 | case wxDIR : m_AllowDirs = TRUE, m_AllowFiles = FALSE; break; | |
112 | default : m_AllowDirs = m_AllowFiles = TRUE; break; | |
113 | } | |
114 | ||
115 | m_ZipFile = left; | |
116 | m_Archive = (void*) unzOpen(m_ZipFile.fn_str()); | |
117 | m_Pattern = right.AfterLast(wxT('/')); | |
118 | m_BaseDir = right.BeforeLast(wxT('/')); | |
119 | ||
120 | if (m_Archive) { | |
121 | if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK) { | |
122 | unzClose((unzFile)m_Archive); | |
123 | m_Archive = NULL; | |
124 | } | |
125 | } | |
126 | return DoFind(); | |
127 | } | |
128 | ||
129 | ||
130 | ||
131 | wxString wxZipFSHandler::FindNext() | |
5526e819 | 132 | { |
aaa66113 VS |
133 | if (!m_Archive) return wxEmptyString; |
134 | return DoFind(); | |
5526e819 VS |
135 | } |
136 | ||
aaa66113 VS |
137 | |
138 | ||
139 | wxString wxZipFSHandler::DoFind() | |
140 | { | |
141 | static char namebuf[1024]; // char, not wxChar! | |
142 | char *c; | |
143 | wxString fn, dir, name; | |
144 | wxString match = wxEmptyString; | |
145 | bool wasdir; | |
146 | ||
147 | while (match == wxEmptyString) | |
148 | { | |
149 | unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0); | |
58c837a4 | 150 | for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/'); |
aaa66113 VS |
151 | fn = namebuf; |
152 | if (fn.Last() == wxT('/')) { | |
153 | fn.RemoveLast(); | |
154 | wasdir = TRUE; | |
155 | } | |
156 | else wasdir = FALSE; | |
157 | ||
158 | name = fn.AfterLast(wxT('/')); | |
159 | dir = fn.BeforeLast(wxT('/')); | |
160 | ||
161 | if (dir == m_BaseDir) { | |
162 | if (m_AllowFiles && !wasdir && wxMatchWild(m_Pattern, name, FALSE)) | |
163 | match = m_ZipFile + wxT("#zip:") + fn; | |
164 | if (m_AllowDirs && wasdir && wxMatchWild(m_Pattern, name, FALSE)) | |
165 | match = m_ZipFile + wxT("#zip:") + fn; | |
166 | } | |
167 | ||
168 | if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK) { | |
169 | unzClose((unzFile)m_Archive); | |
170 | m_Archive = NULL; | |
171 | break; | |
172 | } | |
173 | } | |
174 | ||
175 | return match; | |
176 | } | |
177 | ||
178 | ||
179 | ||
24528b0c VS |
180 | #endif |
181 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |