| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: fs_zip.cpp |
| 3 | // Purpose: ZIP file system |
| 4 | // Author: Vaclav Slavik |
| 5 | // Copyright: (c) 1999 Vaclav Slavik |
| 6 | // CVS-ID: $Id$ |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #include "wx/wxprec.h" |
| 11 | |
| 12 | #ifdef __BORLANDC__ |
| 13 | #pragma hdrstop |
| 14 | #endif |
| 15 | |
| 16 | #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM && wxUSE_ZLIB |
| 17 | |
| 18 | #ifndef WXPRECOMP |
| 19 | #include "wx/intl.h" |
| 20 | #include "wx/log.h" |
| 21 | #endif |
| 22 | |
| 23 | #include "wx/filesys.h" |
| 24 | #include "wx/wfstream.h" |
| 25 | #include "wx/zipstrm.h" |
| 26 | #include "wx/fs_zip.h" |
| 27 | |
| 28 | |
| 29 | //--------------------------------------------------------------------------- |
| 30 | // wxZipFSInputStream |
| 31 | //--------------------------------------------------------------------------- |
| 32 | // Helper class for wxZipFSHandler |
| 33 | |
| 34 | class wxZipFSInputStream : public wxZipInputStream |
| 35 | { |
| 36 | public: |
| 37 | wxZipFSInputStream(wxFSFile *file) |
| 38 | : wxZipInputStream(*file->GetStream()) |
| 39 | { |
| 40 | m_file = file; |
| 41 | #if 1 //WXWIN_COMPATIBILITY_2_6 |
| 42 | m_allowSeeking = true; |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | virtual ~wxZipFSInputStream() { delete m_file; } |
| 47 | |
| 48 | private: |
| 49 | wxFSFile *m_file; |
| 50 | }; |
| 51 | |
| 52 | //---------------------------------------------------------------------------- |
| 53 | // wxZipFSHandler |
| 54 | //---------------------------------------------------------------------------- |
| 55 | |
| 56 | wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler() |
| 57 | { |
| 58 | m_Archive = NULL; |
| 59 | m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString; |
| 60 | m_AllowDirs = m_AllowFiles = true; |
| 61 | m_DirsFound = NULL; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | wxZipFSHandler::~wxZipFSHandler() |
| 67 | { |
| 68 | Cleanup(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void wxZipFSHandler::Cleanup() |
| 73 | { |
| 74 | wxDELETE(m_Archive); |
| 75 | wxDELETE(m_DirsFound); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | bool wxZipFSHandler::CanOpen(const wxString& location) |
| 81 | { |
| 82 | wxString p = GetProtocol(location); |
| 83 | return (p == wxT("zip")); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
| 88 | { |
| 89 | wxString right = GetRightLocation(location); |
| 90 | wxString left = GetLeftLocation(location); |
| 91 | wxZipInputStream *s; |
| 92 | |
| 93 | if (right.Contains(wxT("./"))) |
| 94 | { |
| 95 | if (right.GetChar(0) != wxT('/')) right = wxT('/') + right; |
| 96 | wxFileName rightPart(right, wxPATH_UNIX); |
| 97 | rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX); |
| 98 | right = rightPart.GetFullPath(wxPATH_UNIX); |
| 99 | } |
| 100 | |
| 101 | if (right.GetChar(0) == wxT('/')) right = right.Mid(1); |
| 102 | |
| 103 | // a new wxFileSystem object is needed here to avoid infinite recursion |
| 104 | wxFSFile *leftFile = wxFileSystem().OpenFile(left); |
| 105 | if (!leftFile) |
| 106 | return NULL; |
| 107 | |
| 108 | s = new wxZipFSInputStream(leftFile); |
| 109 | if (s && s->IsOk()) |
| 110 | { |
| 111 | bool found = false; |
| 112 | while (!found) |
| 113 | { |
| 114 | wxZipEntry *ent = s->GetNextEntry(); |
| 115 | if (!ent) |
| 116 | break; |
| 117 | if (ent->GetInternalName() == right) |
| 118 | found = true; |
| 119 | delete ent; |
| 120 | } |
| 121 | if (found) |
| 122 | { |
| 123 | return new wxFSFile(s, |
| 124 | left + wxT("#zip:") + right, |
| 125 | GetMimeTypeFromExt(location), |
| 126 | GetAnchor(location) |
| 127 | #if wxUSE_DATETIME |
| 128 | , wxFileSystem::URLToFileName(left). |
| 129 | GetModificationTime() |
| 130 | #endif // wxUSE_DATETIME |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | delete s; |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags) |
| 142 | { |
| 143 | wxString right = GetRightLocation(spec); |
| 144 | wxString left = GetLeftLocation(spec); |
| 145 | |
| 146 | if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast(); |
| 147 | |
| 148 | if (m_Archive) |
| 149 | { |
| 150 | delete m_Archive; |
| 151 | m_Archive = NULL; |
| 152 | } |
| 153 | |
| 154 | switch (flags) |
| 155 | { |
| 156 | case wxFILE: |
| 157 | m_AllowDirs = false, m_AllowFiles = true; break; |
| 158 | case wxDIR: |
| 159 | m_AllowDirs = true, m_AllowFiles = false; break; |
| 160 | default: |
| 161 | m_AllowDirs = m_AllowFiles = true; break; |
| 162 | } |
| 163 | |
| 164 | m_ZipFile = left; |
| 165 | |
| 166 | wxFSFile *leftFile = wxFileSystem().OpenFile(left); |
| 167 | if (leftFile) |
| 168 | m_Archive = new wxZipFSInputStream(leftFile); |
| 169 | |
| 170 | m_Pattern = right.AfterLast(wxT('/')); |
| 171 | m_BaseDir = right.BeforeLast(wxT('/')); |
| 172 | if (m_BaseDir.StartsWith(wxT("/"))) |
| 173 | m_BaseDir = m_BaseDir.Mid(1); |
| 174 | |
| 175 | if (m_Archive) |
| 176 | { |
| 177 | if (m_AllowDirs) |
| 178 | { |
| 179 | delete m_DirsFound; |
| 180 | m_DirsFound = new wxZipFilenameHashMap(); |
| 181 | if (right.empty()) // allow "/" to match the archive root |
| 182 | return spec; |
| 183 | } |
| 184 | return DoFind(); |
| 185 | } |
| 186 | return wxEmptyString; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | wxString wxZipFSHandler::FindNext() |
| 192 | { |
| 193 | if (!m_Archive) return wxEmptyString; |
| 194 | return DoFind(); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | |
| 199 | wxString wxZipFSHandler::DoFind() |
| 200 | { |
| 201 | wxString namestr, dir, filename; |
| 202 | wxString match = wxEmptyString; |
| 203 | |
| 204 | while (match == wxEmptyString) |
| 205 | { |
| 206 | wxZipEntry *entry = m_Archive->GetNextEntry(); |
| 207 | if (!entry) |
| 208 | { |
| 209 | delete m_Archive; |
| 210 | m_Archive = NULL; |
| 211 | break; |
| 212 | } |
| 213 | namestr = entry->GetName(wxPATH_UNIX); |
| 214 | delete entry; |
| 215 | |
| 216 | if (m_AllowDirs) |
| 217 | { |
| 218 | dir = namestr.BeforeLast(wxT('/')); |
| 219 | while (!dir.empty()) |
| 220 | { |
| 221 | if( m_DirsFound->find(dir) == m_DirsFound->end() ) |
| 222 | { |
| 223 | (*m_DirsFound)[dir] = 1; |
| 224 | filename = dir.AfterLast(wxT('/')); |
| 225 | dir = dir.BeforeLast(wxT('/')); |
| 226 | if (!filename.empty() && m_BaseDir == dir && |
| 227 | wxMatchWild(m_Pattern, filename, false)) |
| 228 | match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename; |
| 229 | } |
| 230 | else |
| 231 | break; // already tranversed |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | filename = namestr.AfterLast(wxT('/')); |
| 236 | dir = namestr.BeforeLast(wxT('/')); |
| 237 | if (m_AllowFiles && !filename.empty() && m_BaseDir == dir && |
| 238 | wxMatchWild(m_Pattern, filename, false)) |
| 239 | match = m_ZipFile + wxT("#zip:") + namestr; |
| 240 | } |
| 241 | |
| 242 | return match; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | #endif |
| 248 | //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM |