]>
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
29 #include "wx/hashmap.h"
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"
41 WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash
, wxIntegerEqual
,
42 wxLongToLongHashMap
, class WXDLLIMPEXP_BASE
);
44 //----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
50 wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
53 m_ZipFile
= m_Pattern
= m_BaseDir
= wxEmptyString
;
54 m_AllowDirs
= m_AllowFiles
= TRUE
;
60 wxZipFSHandler::~wxZipFSHandler()
63 unzClose((unzFile
)m_Archive
);
70 bool wxZipFSHandler::CanOpen(const wxString
& location
)
72 wxString p
= GetProtocol(location
);
73 return (p
== wxT("zip")) &&
74 (GetProtocol(GetLeftLocation(location
)) == wxT("file"));
80 wxFSFile
* wxZipFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
82 wxString right
= GetRightLocation(location
);
83 wxString left
= GetLeftLocation(location
);
86 if (GetProtocol(left
) != wxT("file"))
88 wxLogError(_("ZIP handler currently supports only local files!"));
92 if (right
.Contains(wxT("./")))
94 if (right
.GetChar(0) != wxT('/')) right
= wxT('/') + right
;
95 wxFileName
rightPart(right
, wxPATH_UNIX
);
96 rightPart
.Normalize(wxPATH_NORM_DOTS
, wxT("/"), wxPATH_UNIX
);
97 right
= rightPart
.GetFullPath(wxPATH_UNIX
);
100 if (right
.GetChar(0) == wxT('/')) right
= right
.Mid(1);
102 wxFileName leftFilename
= wxFileSystem::URLToFileName(left
);
104 s
= new wxZipInputStream(leftFilename
.GetFullPath(), right
);
107 return new wxFSFile(s
,
108 left
+ wxT("#zip:") + right
,
109 GetMimeTypeFromExt(location
),
112 , wxDateTime(wxFileModificationTime(left
))
113 #endif // wxUSE_DATETIME
123 wxString
wxZipFSHandler::FindFirst(const wxString
& spec
, int flags
)
125 wxString right
= GetRightLocation(spec
);
126 wxString left
= GetLeftLocation(spec
);
128 if (right
.Last() == wxT('/')) right
.RemoveLast();
132 unzClose((unzFile
)m_Archive
);
136 if (GetProtocol(left
) != wxT("file"))
138 wxLogError(_("ZIP handler currently supports only local files!"));
139 return wxEmptyString
;
145 m_AllowDirs
= FALSE
, m_AllowFiles
= TRUE
; break;
147 m_AllowDirs
= TRUE
, m_AllowFiles
= FALSE
; break;
149 m_AllowDirs
= m_AllowFiles
= TRUE
; break;
153 wxString nativename
= wxFileSystem::URLToFileName(m_ZipFile
).GetFullPath();
154 m_Archive
= (void*) unzOpen(nativename
.mb_str(wxConvFile
));
155 m_Pattern
= right
.AfterLast(wxT('/'));
156 m_BaseDir
= right
.BeforeLast(wxT('/'));
160 if (unzGoToFirstFile((unzFile
)m_Archive
) != UNZ_OK
)
162 unzClose((unzFile
)m_Archive
);
170 m_DirsFound
= new wxLongToLongHashMap();
175 return wxEmptyString
;
180 wxString
wxZipFSHandler::FindNext()
182 if (!m_Archive
) return wxEmptyString
;
188 wxString
wxZipFSHandler::DoFind()
190 static char namebuf
[1024]; // char, not wxChar!
192 wxString namestr
, dir
, filename
;
193 wxString match
= wxEmptyString
;
195 while (match
== wxEmptyString
)
197 unzGetCurrentFileInfo((unzFile
)m_Archive
, NULL
, namebuf
, 1024, NULL
, 0, NULL
, 0);
198 for (c
= namebuf
; *c
; c
++) if (*c
== '\\') *c
= '/';
199 namestr
= wxString::FromAscii(namebuf
); // TODO what encoding does ZIP use?
203 dir
= namestr
.BeforeLast(wxT('/'));
204 while (!dir
.IsEmpty())
207 for (size_t i
= 0; i
< dir
.Length(); i
++) key
+= (wxUChar
)dir
[i
];
208 wxLongToLongHashMap::iterator it
= m_DirsFound
->find(key
);
209 if (it
== m_DirsFound
->end())
211 (*m_DirsFound
)[key
] = 1;
212 filename
= dir
.AfterLast(wxT('/'));
213 dir
= dir
.BeforeLast(wxT('/'));
214 if (!filename
.IsEmpty() && m_BaseDir
== dir
&&
215 wxMatchWild(m_Pattern
, filename
, FALSE
))
216 match
= m_ZipFile
+ wxT("#zip:") + dir
+ wxT("/") + filename
;
219 break; // already tranversed
223 filename
= namestr
.AfterLast(wxT('/'));
224 dir
= namestr
.BeforeLast(wxT('/'));
225 if (m_AllowFiles
&& !filename
.IsEmpty() && m_BaseDir
== dir
&&
226 wxMatchWild(m_Pattern
, filename
, FALSE
))
227 match
= m_ZipFile
+ wxT("#zip:") + namestr
;
229 if (unzGoToNextFile((unzFile
)m_Archive
) != UNZ_OK
)
231 unzClose((unzFile
)m_Archive
);
243 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM