diff --git a/etk/Hach.h b/etk/Hach.h index df1327e..fe48c3b 100644 --- a/etk/Hach.h +++ b/etk/Hach.h @@ -81,7 +81,7 @@ namespace etk * @param[in] _key Name of the hash requested * @return true if the element exist */ - bool Exist(const etk::UString& _name) + bool Exist(const etk::UString& _name) const { int64_t elementId = GetId(_name); if (elementId<0) { diff --git a/etk/archive/Archive.cpp b/etk/archive/Archive.cpp new file mode 100644 index 0000000..5294f17 --- /dev/null +++ b/etk/archive/Archive.cpp @@ -0,0 +1,47 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include +#include + +const etk::Archive::Content& etk::Archive::GetContent(const etk::UString& _key) const +{ + static const etk::Archive::Content g_error; + if (m_content.Exist(_key)==false) { + TK_ERROR("File does not exist : " << _key); + return g_error; + } + return m_content[_key]; +} + +void etk::Archive::Display(void) +{ + for (esize_t iii=0; iii +#include +#include + +namespace etk +{ + class Archive + { + public: + class Content + { + private: + etk::Vector m_data; // if null ==> this is a folder... + public: + Content(esize_t _basicSize=0) : m_data(_basicSize) { }; + esize_t Size(void) const { return m_data.Size(); }; + void* Data(void) { return (void*)m_data.DataPointer(); }; + etk::Vector& GetDataVector(void) { return m_data; }; + }; + public: + Archive(const etk::UString& _fileName) : m_fileName(_fileName) { }; + virtual ~Archive(void) { }; + protected: + etk::UString m_fileName; //!< File name when it came from an file + public: + /** + * @brief Get the current file name. + * @return the requested file name. + */ + const etk::UString& GetFileName(void) { return m_fileName; }; + protected: + etk::Hash m_content; + public: + /** + * @brief Get the number of elements + * @return nb files in the archive + */ + esize_t Size(void) const { return m_content.Size(); }; + /** + * @brief Get the File name of the ID + * @param[in] _id id of the element (must be < Size()) + * @return FileName of the requested id + */ + const etk::UString& GetName(esize_t _id) const { return m_content.GetKey(_id); }; + /** + * @brief Get the File name of the ID + * @param[in] _id id of the element (must be < Size()) + * @return the archive content + */ + const Content& GetContent(esize_t _id) const { return m_content.GetValue(_id); }; + /** + * @brief Get the File name of the ID + * @param[in] _key name of the file + * @return FileName of the requested id + */ + const Content& GetContent(const etk::UString& _key) const; + /** + * @brief Display all Element in the archive + */ + void Display(void); + public: + /** + * @brief Load an Achive with a specific name. + * @param[in] _fileName File name of the specific archive. + * @return A pointer an the specified archive, the user might delete it. + */ + static Archive* Load(const etk::UString& _fileName); + + /** + * @brief Create an Achive with a specific name. + * @param[in] _fileName File name of the specific archive. + * @return A pointer an the specified archive. it is empty due to the fact of create a new archive file. + */ + //Archive* Create(const etk::UString& _fileName); + }; +}; +#endif + diff --git a/etk/archive/Zip.cpp b/etk/archive/Zip.cpp new file mode 100644 index 0000000..b234f60 --- /dev/null +++ b/etk/archive/Zip.cpp @@ -0,0 +1,80 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#include +#include + +etk::archive::Zip::Zip(const etk::UString& _fileName) : + etk::Archive(_fileName), + m_ctx(NULL) +{ + /* Open the zip file */ + m_ctx = unzOpen(m_fileName.c_str()); + if(!m_ctx) { + TK_ERROR("Unable to open the zip file '" << m_fileName << "'"); + return; + } + /* Get info about the zip file */ + if(unzGetGlobalInfo(m_ctx, &m_info) != UNZ_OK) { + TK_ERROR("Unable to read the global info related to the '" << m_fileName << "' zip file"); + return; + } + + // Store all the file in the standard structure + for(int32_t iii=0; iii 0 ); + } + unzCloseCurrentFile(m_ctx); + /* Go the the next entry listed in the zip file. */ + if((iii+1) < m_info.number_entry) { + if (unzGoToNextFile(m_ctx) != UNZ_OK) { + TK_ERROR("Could not read next file from the zip file '" << m_fileName << "'"); + return; + } + } + } +} + +etk::archive::Zip::~Zip(void) +{ + if (m_ctx!= NULL) { + unzClose(m_ctx); + m_ctx = NULL; + }; +}; + diff --git a/etk/archive/Zip.h b/etk/archive/Zip.h new file mode 100644 index 0000000..0d5ec0c --- /dev/null +++ b/etk/archive/Zip.h @@ -0,0 +1,32 @@ +/** + * @author Edouard DUPIN + * + * @copyright 2011, Edouard DUPIN, all right reserved + * + * @license BSD v3 (see license file) + */ + +#ifndef __ETK_ARCHIVE_ZIP_H__ +#define __ETK_ARCHIVE_ZIP_H__ + +#include +#include + +namespace etk +{ + namespace archive + { + class Zip : public etk::Archive + { + private: + unzFile m_ctx; //!< mini zip context + unz_global_info m_info; //!< global information of the Zip + public: + Zip(const etk::UString& _fileName); + virtual ~Zip(void); + }; + }; +}; + +#endif + diff --git a/lutin_etk.py b/lutin_etk.py index 3099f84..2399c6c 100644 --- a/lutin_etk.py +++ b/lutin_etk.py @@ -25,7 +25,9 @@ def Create(target): 'etk/math/Vector3D.cpp', 'etk/os/FSNode.cpp', 'etk/os/FSNodeRight.cpp', - 'etk/os/Memory.cpp']) + 'etk/os/Memory.cpp', + 'etk/archive/Archive.cpp', + 'etk/archive/Zip.cpp']) if target.name=="Windows": myModule.AddSrcFile('etk/os/Mutex.Windows.cpp') @@ -36,6 +38,8 @@ def Create(target): # name of the dependency myModule.AddModuleDepend('linearmath') + myModule.AddModuleDepend('minizip') + if target.name=="Android": myModule.AddModuleDepend('zip') diff --git a/test/main.cpp b/test/main.cpp index 00fa0b0..7e749b5 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #undef __class__ #define __class__ "etktest" @@ -117,6 +118,16 @@ void testFSNode(void) TK_INFO("==> Stop test of FSNode"); } + +void testArchive(void) +{ + TK_INFO("==> Start test of archive"); + etk::Archive* tmpArchive = etk::Archive::Load("testzip.zip"); + tmpArchive->Display(); + + TK_INFO("==> End test of archive"); +} + /* void testDimension(void) { @@ -147,6 +158,7 @@ int main(int argc, const char *argv[]) testHash(); //testFSNode(); //testDimension(); + testArchive(); return 0; }