1 #ifndef OSGDB_ZIPARCHIVE
2 #define OSGDB_ZIPARCHIVE 1
3 
4 #include <osgDB/ReaderWriter>
5 #include <osgDB/FileUtils>
6 
7 #include <osgDB/Archive>
8 #include <OpenThreads/Mutex>
9 
10 #include "unzip.h"
11 
12 
13 class ZipArchive : public osgDB::Archive
14 {
15     public:
16         ZipArchive();
17         virtual ~ZipArchive();
18 
libraryName()19         virtual const char* libraryName() const { return "osgDB"; }
20 
className()21         virtual const char* className() const { return "ZipArchive"; }
22 
acceptsExtension(const std::string &)23         virtual bool acceptsExtension(const std::string& /*extension*/) const { return true; }
24 
25         /** close the archive.*/
26         virtual void close();
27 
28         /** open the archive.*/
29         virtual bool open(const std::string& filename, ArchiveStatus status, const osgDB::ReaderWriter::Options* options);
30 
31         /** open the archive for reading.*/
32         virtual bool open(std::istream& fin, const osgDB::ReaderWriter::Options* options);
33 
34         /** return true if file exists in archive.*/
35         virtual bool fileExists(const std::string& filename) const;
36 
37         /** Get the filename that refers to the archived file on disk*/
38         virtual std::string getArchiveFileName() const;
39 
40         /** Get the file name which represents the master file recorded in the Archive.*/
41         virtual std::string getMasterFileName() const;
42 
43         /** Get the full list of file names available in the archive.*/
44         virtual bool getFileNames(osgDB::Archive::FileNameList& fileNameList) const;
45 
46         /** return type of file. */
47         virtual osgDB::FileType getFileType(const std::string& filename) const;
48 
49         /** return the contents of a directory.
50         * returns an empty array on any error.*/
51         virtual osgDB::DirectoryContents getDirectoryContents(const std::string& dirName) const;
52 
53         virtual osgDB::ReaderWriter::ReadResult readObject(const std::string& /*fileName*/, const osgDB::ReaderWriter::Options* =NULL) const;
54         virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
55         virtual osgDB::ReaderWriter::ReadResult readHeightField(const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
56         virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& /*fileName*/, const osgDB::ReaderWriter::Options* =NULL) const;
57         virtual osgDB::ReaderWriter::ReadResult readShader(const std::string& /*fileName*/, const osgDB::ReaderWriter::Options* =NULL) const;
58 
59         virtual osgDB::ReaderWriter::WriteResult writeObject(const osg::Object& /*obj*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
60         virtual osgDB::ReaderWriter::WriteResult writeImage(const osg::Image& /*image*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
61         virtual osgDB::ReaderWriter::WriteResult writeHeightField(const osg::HeightField& /*heightField*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
62         virtual osgDB::ReaderWriter::WriteResult writeNode(const osg::Node& /*node*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
63         virtual osgDB::ReaderWriter::WriteResult writeShader(const osg::Shader& /*shader*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
64 
65     protected:
66 
67         osgDB::ReaderWriter* ReadFromZipEntry(const ZIPENTRY* ze, const osgDB::ReaderWriter::Options* options, std::stringstream& streamIn) const;
68 
69         void IndexZipFiles(HZIP hz);
70         const ZIPENTRY* GetZipEntry(const std::string& filename) const;
71         ZIPENTRY* GetZipEntry(const std::string& filename);
72 
73         std::string ReadPassword(const osgDB::ReaderWriter::Options* options) const;
74         bool CheckZipErrorCode(ZRESULT result) const;
75 
76     private:
77 
78 
79         typedef std::pair<std::string, ZIPENTRY*> ZipEntryMapping;
80         typedef std::map<std::string, ZIPENTRY*> ZipEntryMap;
81 
82         std::string _filename, _password, _membuffer;
83 
84         OpenThreads::Mutex _zipMutex;
85         bool               _zipLoaded;
86         ZipEntryMap        _zipIndex;
87         ZIPENTRY           _mainRecord;
88 
89         struct PerThreadData {
90             HZIP _zipHandle;
91         };
92 
93         typedef std::map<OpenThreads::Thread*, PerThreadData> PerThreadDataMap;
94         PerThreadDataMap _perThreadData;
95 
96         const PerThreadData& getData() const;
97         const PerThreadData& getDataNoLock() const;
98 };
99 
100 
101 #endif //OSGDB_ZIPARCHIVE
102 
103