]>
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()
82 bool wxZipFSHandler::CanOpen(const wxString
& location
)
84 wxString p
= GetProtocol(location
);
85 return (p
== wxT("zip"));
89 wxFSFile
* wxZipFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
91 wxString right
= GetRightLocation(location
);
92 wxString left
= GetLeftLocation(location
);
95 if (right
.Contains(wxT("./")))
97 if (right
.GetChar(0) != wxT('/')) right
= wxT('/') + right
;
98 wxFileName
rightPart(right
, wxPATH_UNIX
);
99 rightPart
.Normalize(wxPATH_NORM_DOTS
, wxT("/"), wxPATH_UNIX
);
100 right
= rightPart
.GetFullPath(wxPATH_UNIX
);
103 if (right
.GetChar(0) == wxT('/')) right
= right
.Mid(1);
105 // a new wxFileSystem object is needed here to avoid infinite recursion
106 wxFSFile
*leftFile
= wxFileSystem().OpenFile(left
);
110 s
= new wxZipFSInputStream(leftFile
);
116 wxZipEntry
*ent
= s
->GetNextEntry();
119 if (ent
->GetInternalName() == right
)
124 return new wxFSFile(s
,
125 left
+ wxT("#zip:") + right
,
126 GetMimeTypeFromExt(location
),
129 , wxDateTime(wxFileModificationTime(left
))
130 #endif // wxUSE_DATETIME
140 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
142 wxString right
= GetRightLocation(spec
);
143 wxString left
= GetLeftLocation(spec
);
145 if (!right
.empty() && right
.Last() == wxT('/')) right
.RemoveLast();
156 m_AllowDirs
= false, m_AllowFiles
= true; break;
158 m_AllowDirs
= true, m_AllowFiles
= false; break;
160 m_AllowDirs
= m_AllowFiles
= true; break;
165 wxFSFile
*leftFile
= wxFileSystem().OpenFile(left
);
167 m_Archive
= new wxZipFSInputStream(leftFile
);
169 m_Pattern
= right
.AfterLast(wxT('/'));
170 m_BaseDir
= right
.BeforeLast(wxT('/'));
171 if (m_BaseDir
.StartsWith(wxT("/")))
172 m_BaseDir
= m_BaseDir
.Mid(1);
179 m_DirsFound
= new wxZipFilenameHashMap();
180 if (right
.empty()) // allow "/" to match the archive root
185 return wxEmptyString
;
190 wxString
wxZipFSHandler::FindNext()
192 if (!m_Archive
) return wxEmptyString
;
198 wxString
wxZipFSHandler::DoFind()
200 wxString namestr
, dir
, filename
;
201 wxString match
= wxEmptyString
;
203 while (match
== wxEmptyString
)
205 wxZipEntry
*entry
= m_Archive
->GetNextEntry();
212 namestr
= entry
->GetName(wxPATH_UNIX
);
217 dir
= namestr
.BeforeLast(wxT('/'));
220 if( m_DirsFound
->find(dir
) == m_DirsFound
->end() )
222 (*m_DirsFound
)[dir
] = 1;
223 filename
= dir
.AfterLast(wxT('/'));
224 dir
= dir
.BeforeLast(wxT('/'));
225 if (!filename
.empty() && m_BaseDir
== dir
&&
226 wxMatchWild(m_Pattern
, filename
, false))
227 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
230 break; // already tranversed
234 filename
= namestr
.AfterLast(wxT('/'));
235 dir
= namestr
.BeforeLast(wxT('/'));
236 if (m_AllowFiles
&& !filename
.empty() && m_BaseDir
== dir
&&
237 wxMatchWild(m_Pattern
, filename
, false))
238 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
247 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM