]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: ZIP file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "fs_zip.h"
16 #include "wx/wxprec.h"
22 #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM && wxUSE_ZLIB
29 #include "wx/filesys.h"
30 #include "wx/wfstream.h"
31 #include "wx/zipstrm.h"
32 #include "wx/fs_zip.h"
35 //---------------------------------------------------------------------------
37 //---------------------------------------------------------------------------
38 // Helper class for wxZipFSHandler
40 class wxZipFSInputStream
: public wxZipInputStream
43 wxZipFSInputStream(wxFSFile
*file
)
44 : wxZipInputStream(*file
->GetStream())
47 #if 1 //WXWIN_COMPATIBILITY_2_6
48 m_allowSeeking
= true;
52 virtual ~wxZipFSInputStream() { delete m_file
; }
58 //----------------------------------------------------------------------------
60 //----------------------------------------------------------------------------
62 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
65 m_ZipFile
= m_Pattern
= m_BaseDir
= wxEmptyString
;
66 m_AllowDirs
= m_AllowFiles
= true;
72 wxZipFSHandler::~wxZipFSHandler()
78 void wxZipFSHandler::Cleanup()
81 wxDELETE(m_DirsFound
);
86 bool wxZipFSHandler::CanOpen(const wxString
& location
)
88 wxString p
= GetProtocol(location
);
89 return (p
== wxT("zip"));
93 wxFSFile
* wxZipFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
95 wxString right
= GetRightLocation(location
);
96 wxString left
= GetLeftLocation(location
);
99 if (right
.Contains(wxT("./")))
101 if (right
.GetChar(0) != wxT('/')) right
= wxT('/') + right
;
102 wxFileName
rightPart(right
, wxPATH_UNIX
);
103 rightPart
.Normalize(wxPATH_NORM_DOTS
, wxT("/"), wxPATH_UNIX
);
104 right
= rightPart
.GetFullPath(wxPATH_UNIX
);
107 if (right
.GetChar(0) == wxT('/')) right
= right
.Mid(1);
109 // a new wxFileSystem object is needed here to avoid infinite recursion
110 wxFSFile
*leftFile
= wxFileSystem().OpenFile(left
);
114 s
= new wxZipFSInputStream(leftFile
);
120 wxZipEntry
*ent
= s
->GetNextEntry();
123 if (ent
->GetInternalName() == right
)
128 return new wxFSFile(s
,
129 left
+ wxT("#zip:") + right
,
130 GetMimeTypeFromExt(location
),
133 , wxDateTime(wxFileModificationTime(left
))
134 #endif // wxUSE_DATETIME
144 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
146 wxString right
= GetRightLocation(spec
);
147 wxString left
= GetLeftLocation(spec
);
149 if (!right
.empty() && right
.Last() == wxT('/')) right
.RemoveLast();
160 m_AllowDirs
= false, m_AllowFiles
= true; break;
162 m_AllowDirs
= true, m_AllowFiles
= false; break;
164 m_AllowDirs
= m_AllowFiles
= true; break;
169 wxFSFile
*leftFile
= wxFileSystem().OpenFile(left
);
171 m_Archive
= new wxZipFSInputStream(leftFile
);
173 m_Pattern
= right
.AfterLast(wxT('/'));
174 m_BaseDir
= right
.BeforeLast(wxT('/'));
175 if (m_BaseDir
.StartsWith(wxT("/")))
176 m_BaseDir
= m_BaseDir
.Mid(1);
183 m_DirsFound
= new wxZipFilenameHashMap();
184 if (right
.empty()) // allow "/" to match the archive root
189 return wxEmptyString
;
194 wxString
wxZipFSHandler::FindNext()
196 if (!m_Archive
) return wxEmptyString
;
202 wxString
wxZipFSHandler::DoFind()
204 wxString namestr
, dir
, filename
;
205 wxString match
= wxEmptyString
;
207 while (match
== wxEmptyString
)
209 wxZipEntry
*entry
= m_Archive
->GetNextEntry();
216 namestr
= entry
->GetName(wxPATH_UNIX
);
221 dir
= namestr
.BeforeLast(wxT('/'));
224 if( m_DirsFound
->find(dir
) == m_DirsFound
->end() )
226 (*m_DirsFound
)[dir
] = 1;
227 filename
= dir
.AfterLast(wxT('/'));
228 dir
= dir
.BeforeLast(wxT('/'));
229 if (!filename
.empty() && m_BaseDir
== dir
&&
230 wxMatchWild(m_Pattern
, filename
, false))
231 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
234 break; // already tranversed
238 filename
= namestr
.AfterLast(wxT('/'));
239 dir
= namestr
.BeforeLast(wxT('/'));
240 if (m_AllowFiles
&& !filename
.empty() && m_BaseDir
== dir
&&
241 wxMatchWild(m_Pattern
, filename
, false))
242 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
251 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM