]>
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
.Contains(wxT("./")))
92 if (right
.GetChar(0) != wxT('/')) right
= wxT('/') + right
;
93 wxFileName
rightPart(right
, wxPATH_UNIX
);
94 rightPart
.Normalize(wxPATH_NORM_DOTS
, wxT("/"), wxPATH_UNIX
);
95 right
= rightPart
.GetFullPath(wxPATH_UNIX
);
98 if (right
.GetChar(0) == wxT('/')) right
= right
.Mid(1);
100 wxFileName leftFilename
= wxFileSystem::URLToFileName(left
);
102 s
= new wxZipInputStream(leftFilename
.GetFullPath(), right
);
105 return new wxFSFile(s
,
106 left
+ wxT("#zip:") + right
,
107 GetMimeTypeFromExt(location
),
110 , wxDateTime(wxFileModificationTime(left
))
111 #endif // wxUSE_DATETIME
121 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
123 wxString right
= GetRightLocation(spec
);
124 wxString left
= GetLeftLocation(spec
);
126 if (right
.Last() == wxT('/')) right
.RemoveLast();
130 unzClose((unzFile
)m_Archive
);
134 if (GetProtocol(left
) != wxT("file"))
136 wxLogError(_("ZIP handler currently supports only local files!"));
137 return wxEmptyString
;
143 m_AllowDirs
= FALSE
, m_AllowFiles
= TRUE
; break;
145 m_AllowDirs
= TRUE
, m_AllowFiles
= FALSE
; break;
147 m_AllowDirs
= m_AllowFiles
= TRUE
; break;
151 wxString nativename
= wxFileSystem::URLToFileName(m_ZipFile
).GetFullPath();
152 m_Archive
= (void*) unzOpen(nativename
.mb_str());
153 m_Pattern
= right
.AfterLast(wxT('/'));
154 m_BaseDir
= right
.BeforeLast(wxT('/'));
158 if (unzGoToFirstFile((unzFile
)m_Archive
) != UNZ_OK
)
160 unzClose((unzFile
)m_Archive
);
168 m_DirsFound
= new wxHashTableLong();
173 return wxEmptyString
;
178 wxString
wxZipFSHandler::FindNext()
180 if (!m_Archive
) return wxEmptyString
;
186 wxString
wxZipFSHandler::DoFind()
188 static char namebuf
[1024]; // char, not wxChar!
190 wxString namestr
, dir
, filename
;
191 wxString match
= wxEmptyString
;
193 while (match
== wxEmptyString
)
195 unzGetCurrentFileInfo((unzFile
)m_Archive
, NULL
, namebuf
, 1024, NULL
, 0, NULL
, 0);
196 for (c
= namebuf
; *c
; c
++) if (*c
== '\\') *c
= '/';
197 namestr
= wxString::FromAscii( namebuf
); // TODO what encoding does ZIP use?
201 dir
= namestr
.BeforeLast(wxT('/'));
202 while (!dir
.IsEmpty())
205 for (size_t i
= 0; i
< dir
.Length(); i
++) key
+= (wxUChar
)dir
[i
];
206 if (m_DirsFound
->Get(key
) == wxNOT_FOUND
)
208 m_DirsFound
->Put(key
, 1);
209 filename
= dir
.AfterLast(wxT('/'));
210 dir
= dir
.BeforeLast(wxT('/'));
211 if (!filename
.IsEmpty() && m_BaseDir
== dir
&&
212 wxMatchWild(m_Pattern
, filename
, FALSE
))
213 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
216 break; // already tranversed
220 filename
= namestr
.AfterLast(wxT('/'));
221 dir
= namestr
.BeforeLast(wxT('/'));
222 if (m_AllowFiles
&& !filename
.IsEmpty() && m_BaseDir
== dir
&&
223 wxMatchWild(m_Pattern
, filename
, FALSE
))
224 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
226 if (unzGoToNextFile((unzFile
)m_Archive
) != UNZ_OK
)
228 unzClose((unzFile
)m_Archive
);
240 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM