]>
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
& 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 wxFSFile
*leftFile
= fs
.OpenFile(left
);
109 s
= new wxZipFSInputStream(leftFile
);
114 while (!found
&& (ent
= s
->GetNextEntry())) {
115 if (ent
->GetInternalName() == right
)
120 return new wxFSFile(s
,
121 left
+ wxT("#zip:") + right
,
122 GetMimeTypeFromExt(location
),
125 , wxDateTime(wxFileModificationTime(left
))
126 #endif // wxUSE_DATETIME
136 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
138 wxString right
= GetRightLocation(spec
);
139 wxString left
= GetLeftLocation(spec
);
141 if (right
.Last() == wxT('/')) right
.RemoveLast();
152 m_AllowDirs
= false, m_AllowFiles
= true; break;
154 m_AllowDirs
= true, m_AllowFiles
= false; break;
156 m_AllowDirs
= m_AllowFiles
= true; break;
161 wxFSFile
*leftFile
= wxFileSystem().OpenFile(left
);
163 m_Archive
= new wxZipFSInputStream(leftFile
);
165 m_Pattern
= right
.AfterLast(wxT('/'));
166 m_BaseDir
= right
.BeforeLast(wxT('/'));
173 m_DirsFound
= new wxZipFilenameHashMap();
177 return wxEmptyString
;
182 wxString
wxZipFSHandler::FindNext()
184 if (!m_Archive
) return wxEmptyString
;
190 wxString
wxZipFSHandler::DoFind()
192 wxString namestr
, dir
, filename
;
193 wxString match
= wxEmptyString
;
195 while (match
== wxEmptyString
)
197 wxZipEntry
*entry
= m_Archive
->GetNextEntry();
204 namestr
= entry
->GetName(wxPATH_UNIX
);
209 dir
= namestr
.BeforeLast(wxT('/'));
212 if( m_DirsFound
->find(dir
) == m_DirsFound
->end() )
214 (*m_DirsFound
)[dir
] = 1;
215 filename
= dir
.AfterLast(wxT('/'));
216 dir
= dir
.BeforeLast(wxT('/'));
217 if (!filename
.empty() && m_BaseDir
== dir
&&
218 wxMatchWild(m_Pattern
, filename
, false))
219 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
222 break; // already tranversed
226 filename
= namestr
.AfterLast(wxT('/'));
227 dir
= namestr
.BeforeLast(wxT('/'));
228 if (m_AllowFiles
&& !filename
.empty() && m_BaseDir
== dir
&&
229 wxMatchWild(m_Pattern
, filename
, false))
230 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
239 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM