]>
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
16 #include "wx/wxprec.h"
22 #if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM
29 #include "wx/filesys.h"
30 #include "wx/zipstrm.h"
31 #include "wx/fs_zip.h"
33 /* Not the right solution (paths in makefiles) but... */
35 #include "../common/unzip.h"
41 //--------------------------------------------------------------------------------
43 //--------------------------------------------------------------------------------
47 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
50 m_ZipFile
= m_Pattern
= m_BaseDir
= wxEmptyString
;
51 m_AllowDirs
= m_AllowFiles
= TRUE
;
57 wxZipFSHandler::~wxZipFSHandler()
60 unzClose((unzFile
)m_Archive
);
67 bool wxZipFSHandler::CanOpen(const wxString
& location
)
69 wxString p
= GetProtocol(location
);
70 return (p
== wxT("zip")) &&
71 (GetProtocol(GetLeftLocation(location
)) == wxT("file"));
77 wxFSFile
* wxZipFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
79 wxString right
= GetRightLocation(location
);
80 wxString left
= GetLeftLocation(location
);
83 if (GetProtocol(left
) != wxT("file"))
85 wxLogError(_("ZIP handler currently supports only local files!"));
89 s
= new wxZipInputStream(left
, right
);
90 if (s
&& (s
->LastError() == wxStream_NOERROR
))
92 return new wxFSFile(s
,
93 left
+ wxT("#zip:") + right
,
94 GetMimeTypeFromExt(location
),
96 wxDateTime(wxFileModificationTime(left
)));
105 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
107 wxString right
= GetRightLocation(spec
);
108 wxString left
= GetLeftLocation(spec
);
110 if (right
.Last() == wxT('/')) right
.RemoveLast();
114 unzClose((unzFile
)m_Archive
);
118 if (GetProtocol(left
) != wxT("file"))
120 wxLogError(_("ZIP handler currently supports only local files!"));
121 return wxEmptyString
;
127 m_AllowDirs
= FALSE
, m_AllowFiles
= TRUE
; break;
129 m_AllowDirs
= TRUE
, m_AllowFiles
= FALSE
; break;
131 m_AllowDirs
= m_AllowFiles
= TRUE
; break;
135 m_Archive
= (void*) unzOpen(m_ZipFile
.mb_str());
136 m_Pattern
= right
.AfterLast(wxT('/'));
137 m_BaseDir
= right
.BeforeLast(wxT('/'));
141 if (unzGoToFirstFile((unzFile
)m_Archive
) != UNZ_OK
)
143 unzClose((unzFile
)m_Archive
);
151 m_DirsFound
= new wxHashTableLong();
156 return wxEmptyString
;
161 wxString
wxZipFSHandler::FindNext()
163 if (!m_Archive
) return wxEmptyString
;
169 wxString
wxZipFSHandler::DoFind()
171 static char namebuf
[1024]; // char, not wxChar!
173 wxString namestr
, dir
, filename
;
174 wxString match
= wxEmptyString
;
176 while (match
== wxEmptyString
)
178 unzGetCurrentFileInfo((unzFile
)m_Archive
, NULL
, namebuf
, 1024, NULL
, 0, NULL
, 0);
179 for (c
= namebuf
; *c
; c
++) if (*c
== wxT('\\')) *c
= wxT('/');
184 dir
= namestr
.BeforeLast(wxT('/'));
185 while (!dir
.IsEmpty())
188 for (size_t i
= 0; i
< dir
.Length(); i
++) key
+= (wxUChar
)dir
[i
];
189 if (m_DirsFound
->Get(key
) == wxNOT_FOUND
)
191 m_DirsFound
->Put(key
, 1);
192 filename
= dir
.AfterLast(wxT('/'));
193 dir
= dir
.BeforeLast(wxT('/'));
194 if (!filename
.IsEmpty() && m_BaseDir
== dir
&&
195 wxMatchWild(m_Pattern
, filename
, FALSE
))
196 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
199 break; // already tranversed
203 filename
= namestr
.AfterLast(wxT('/'));
204 dir
= namestr
.BeforeLast(wxT('/'));
205 if (m_AllowFiles
&& !filename
.IsEmpty() && m_BaseDir
== dir
&&
206 wxMatchWild(m_Pattern
, filename
, FALSE
))
207 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
209 if (unzGoToNextFile((unzFile
)m_Archive
) != UNZ_OK
)
211 unzClose((unzFile
)m_Archive
);
223 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM