]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "fs_zip.h"
16 #include "wx/wxprec.h"
22 #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM
30 #include "wx/filesys.h"
31 #include "wx/zipstrm.h"
32 #include "wx/fs_zip.h"
34 /* Not the right solution (paths in makefiles) but... */
36 #include "../common/unzip.h"
42 //--------------------------------------------------------------------------------
44 //--------------------------------------------------------------------------------
48 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
51 m_ZipFile
= m_Pattern
= m_BaseDir
= wxEmptyString
;
52 m_AllowDirs
= m_AllowFiles
= TRUE
;
58 wxZipFSHandler::~wxZipFSHandler()
61 unzClose((unzFile
)m_Archive
);
68 bool wxZipFSHandler::CanOpen(const wxString
& location
)
70 wxString p
= GetProtocol(location
);
71 return (p
== wxT("zip")) &&
72 (GetProtocol(GetLeftLocation(location
)) == wxT("file"));
78 wxFSFile
* wxZipFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
80 wxString right
= GetRightLocation(location
);
81 wxString left
= GetLeftLocation(location
);
84 if (GetProtocol(left
) != wxT("file"))
86 wxLogError(_("ZIP handler currently supports only local files!"));
90 if (right
.GetChar(0) == wxT('/')) right
= right
.Mid(1);
92 s
= new wxZipInputStream(left
, right
);
93 if (s
&& (s
->LastError() == wxStream_NOERROR
))
95 return new wxFSFile(s
,
96 left
+ wxT("#zip:") + right
,
97 GetMimeTypeFromExt(location
),
99 wxDateTime(wxFileModificationTime(left
)));
108 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
110 wxString right
= GetRightLocation(spec
);
111 wxString left
= GetLeftLocation(spec
);
113 if (right
.Last() == wxT('/')) right
.RemoveLast();
117 unzClose((unzFile
)m_Archive
);
121 if (GetProtocol(left
) != wxT("file"))
123 wxLogError(_("ZIP handler currently supports only local files!"));
124 return wxEmptyString
;
130 m_AllowDirs
= FALSE
, m_AllowFiles
= TRUE
; break;
132 m_AllowDirs
= TRUE
, m_AllowFiles
= FALSE
; break;
134 m_AllowDirs
= m_AllowFiles
= TRUE
; break;
138 m_Archive
= (void*) unzOpen(m_ZipFile
.mb_str());
139 m_Pattern
= right
.AfterLast(wxT('/'));
140 m_BaseDir
= right
.BeforeLast(wxT('/'));
144 if (unzGoToFirstFile((unzFile
)m_Archive
) != UNZ_OK
)
146 unzClose((unzFile
)m_Archive
);
154 m_DirsFound
= new wxHashTableLong();
159 return wxEmptyString
;
164 wxString
wxZipFSHandler::FindNext()
166 if (!m_Archive
) return wxEmptyString
;
172 wxString
wxZipFSHandler::DoFind()
174 static char namebuf
[1024]; // char, not wxChar!
176 wxString namestr
, dir
, filename
;
177 wxString match
= wxEmptyString
;
179 while (match
== wxEmptyString
)
181 unzGetCurrentFileInfo((unzFile
)m_Archive
, NULL
, namebuf
, 1024, NULL
, 0, NULL
, 0);
182 for (c
= namebuf
; *c
; c
++) if (*c
== wxT('\\')) *c
= wxT('/');
187 dir
= namestr
.BeforeLast(wxT('/'));
188 while (!dir
.IsEmpty())
191 for (size_t i
= 0; i
< dir
.Length(); i
++) key
+= (wxUChar
)dir
[i
];
192 if (m_DirsFound
->Get(key
) == wxNOT_FOUND
)
194 m_DirsFound
->Put(key
, 1);
195 filename
= dir
.AfterLast(wxT('/'));
196 dir
= dir
.BeforeLast(wxT('/'));
197 if (!filename
.IsEmpty() && m_BaseDir
== dir
&&
198 wxMatchWild(m_Pattern
, filename
, FALSE
))
199 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
202 break; // already tranversed
206 filename
= namestr
.AfterLast(wxT('/'));
207 dir
= namestr
.BeforeLast(wxT('/'));
208 if (m_AllowFiles
&& !filename
.IsEmpty() && m_BaseDir
== dir
&&
209 wxMatchWild(m_Pattern
, filename
, FALSE
))
210 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
212 if (unzGoToNextFile((unzFile
)m_Archive
) != UNZ_OK
)
214 unzClose((unzFile
)m_Archive
);
226 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM