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         virtual osgDB::ReaderWriter::ReadResult readScript(const std::string& /*fileName*/, const osgDB::ReaderWriter::Options* =NULL) const;
59 
60         virtual osgDB::ReaderWriter::WriteResult writeObject(const osg::Object& /*obj*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
61         virtual osgDB::ReaderWriter::WriteResult writeImage(const osg::Image& /*image*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
62         virtual osgDB::ReaderWriter::WriteResult writeHeightField(const osg::HeightField& /*heightField*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
63         virtual osgDB::ReaderWriter::WriteResult writeNode(const osg::Node& /*node*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
64         virtual osgDB::ReaderWriter::WriteResult writeShader(const osg::Shader& /*shader*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
65         virtual osgDB::ReaderWriter::WriteResult writeScript(const osg::Script& /*script*/, const std::string& /*fileName*/,const osgDB::ReaderWriter::Options* =NULL) const;
66 
67     protected:
68 
69         osgDB::ReaderWriter* ReadFromZipEntry(const ZIPENTRY* ze, const osgDB::ReaderWriter::Options* options, std::stringstream& streamIn) const;
70 
71         void IndexZipFiles(HZIP hz);
72         const ZIPENTRY* GetZipEntry(const std::string& filename) const;
73         ZIPENTRY* GetZipEntry(const std::string& filename);
74 
75         std::string ReadPassword(const osgDB::ReaderWriter::Options* options) const;
76         bool CheckZipErrorCode(ZRESULT result) const;
77 
78     private:
79 
80 
81         typedef std::pair<std::string, ZIPENTRY*> ZipEntryMapping;
82         typedef std::map<std::string, ZIPENTRY*> ZipEntryMap;
83 
84         std::string _filename, _password, _membuffer;
85 
86         OpenThreads::Mutex _zipMutex;
87         bool               _zipLoaded;
88         ZipEntryMap        _zipIndex;
89         ZIPENTRY           _mainRecord;
90 
91         struct PerThreadData {
92             HZIP _zipHandle;
93         };
94 
95         typedef std::map<size_t, PerThreadData> PerThreadDataMap;
96         PerThreadDataMap _perThreadData;
97 
98         const PerThreadData& getData() const;
99         const PerThreadData& getDataNoLock() const;
100 };
101 
102 
103 #endif //OSGDB_ZIPARCHIVE
104 
105