1ed2f6d3bSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ed2f6d3bSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ed2f6d3bSAndrew Rist * or more contributor license agreements. See the NOTICE file 5ed2f6d3bSAndrew Rist * distributed with this work for additional information 6ed2f6d3bSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ed2f6d3bSAndrew Rist * to you under the Apache License, Version 2.0 (the 8ed2f6d3bSAndrew Rist * "License"); you may not use this file except in compliance 9ed2f6d3bSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11ed2f6d3bSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13ed2f6d3bSAndrew Rist * Unless required by applicable law or agreed to in writing, 14ed2f6d3bSAndrew Rist * software distributed under the License is distributed on an 15ed2f6d3bSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ed2f6d3bSAndrew Rist * KIND, either express or implied. See the License for the 17ed2f6d3bSAndrew Rist * specific language governing permissions and limitations 18ed2f6d3bSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20ed2f6d3bSAndrew Rist *************************************************************/ 21ed2f6d3bSAndrew Rist 22ed2f6d3bSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef ZIPFILE_HXX_INCLUDED 25cdf0e10cSrcweir #define ZIPFILE_HXX_INCLUDED 26cdf0e10cSrcweir 27cdf0e10cSrcweir #ifndef _WINDOWS 28cdf0e10cSrcweir #define _WINDOWS 29cdf0e10cSrcweir #endif 30cdf0e10cSrcweir 31cdf0e10cSrcweir 325dc019ceSYuri Dario #ifdef OS2 335dc019ceSYuri Dario #include <minizip/unzip.h> 345dc019ceSYuri Dario #else 35cdf0e10cSrcweir #include <external/zlib/unzip.h> 365dc019ceSYuri Dario #endif 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir #include <string> 40cdf0e10cSrcweir #include <vector> 41cdf0e10cSrcweir #include <memory> 42cdf0e10cSrcweir 43cdf0e10cSrcweir /** A simple zip content provider based on the zlib 44cdf0e10cSrcweir */ 45cdf0e10cSrcweir 46cdf0e10cSrcweir class ZipFile 47cdf0e10cSrcweir { 48cdf0e10cSrcweir public: 49cdf0e10cSrcweir 50cdf0e10cSrcweir typedef std::vector<std::string> Directory_t; 51cdf0e10cSrcweir typedef std::auto_ptr<Directory_t> DirectoryPtr_t; 52cdf0e10cSrcweir typedef std::vector<char> ZipContentBuffer_t; 53cdf0e10cSrcweir 54cdf0e10cSrcweir public: 55cdf0e10cSrcweir 56cdf0e10cSrcweir /** Checks whether a file is a zip file or not 57cdf0e10cSrcweir 58cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 59cdf0e10cSrcweir The file must exist 60cdf0e10cSrcweir The file must be readable for the current user 61cdf0e10cSrcweir 62cdf0e10cSrcweir @returns true if the file is a zip file 63cdf0e10cSrcweir false if the file is not a zip file 64cdf0e10cSrcweir 65cdf0e10cSrcweir @throws ParameterException if the given file name is empty 66cdf0e10cSrcweir IOException if the specified file doesn't exist 67cdf0e10cSrcweir AccessViolationException if read access to the file is denied 68cdf0e10cSrcweir */ 69cdf0e10cSrcweir static bool IsZipFile(const std::string& FileName); 70cdf0e10cSrcweir 71cdf0e10cSrcweir static bool IsZipFile(void* stream); 72cdf0e10cSrcweir 73cdf0e10cSrcweir 74*0ba0bc4aSmseidel /** Returns whether the version of the specified zip file may be uncompressed with the 75cdf0e10cSrcweir currently used zlib version or not 76cdf0e10cSrcweir 77cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 78cdf0e10cSrcweir The file must exist 79cdf0e10cSrcweir The file must be readable for the current user 80cdf0e10cSrcweir The file must be a valid zip file 81cdf0e10cSrcweir 82cdf0e10cSrcweir @returns true if the file may be uncompressed with the currently used zlib 83cdf0e10cSrcweir false if the file may not be uncompressed with the currently used zlib 84cdf0e10cSrcweir 85cdf0e10cSrcweir @throws ParameterException if the given file name is empty 86cdf0e10cSrcweir IOException if the specified file doesn't exist or is no zip file 87cdf0e10cSrcweir AccessViolationException if read access to the file is denied 88cdf0e10cSrcweir */ 89cdf0e10cSrcweir static bool IsValidZipFileVersionNumber(const std::string& FileName); 90cdf0e10cSrcweir 91cdf0e10cSrcweir static bool IsValidZipFileVersionNumber(void* stream); 92cdf0e10cSrcweir 93cdf0e10cSrcweir public: 94cdf0e10cSrcweir 95cdf0e10cSrcweir /** Constructs a zip file from a zip file 96cdf0e10cSrcweir 97cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 98cdf0e10cSrcweir The file must exist 99cdf0e10cSrcweir The file must be readable for the current user 100cdf0e10cSrcweir 101cdf0e10cSrcweir @throws ParameterException if the given file name is empty 102cdf0e10cSrcweir IOException if the specified file doesn't exist or is no valid zip file 103cdf0e10cSrcweir AccessViolationException if read access to the file is denied 104cdf0e10cSrcweir WrongZipVersionException if the zip file cannot be uncompressed 105cdf0e10cSrcweir with the used zlib version 106cdf0e10cSrcweir */ 107cdf0e10cSrcweir ZipFile(const std::string& FileName); 108cdf0e10cSrcweir 109cdf0e10cSrcweir ZipFile(void* stream, zlib_filefunc_def* fa); 110cdf0e10cSrcweir 111cdf0e10cSrcweir 112cdf0e10cSrcweir /** Destroys a zip file 113cdf0e10cSrcweir */ 114cdf0e10cSrcweir ~ZipFile(); 115cdf0e10cSrcweir 116cdf0e10cSrcweir /** Provides an interface to read the uncompressed data of a content of the zip file 117cdf0e10cSrcweir 118cdf0e10cSrcweir @param ContentName 119cdf0e10cSrcweir The name of the content in the zip file 120cdf0e10cSrcweir 121cdf0e10cSrcweir @param ppstm 122cdf0e10cSrcweir Pointer to pointer, will receive an interface pointer 123cdf0e10cSrcweir to IUnknown on success 124cdf0e10cSrcweir 125cdf0e10cSrcweir @precond The specified content must exist in this file 126cdf0e10cSrcweir ppstm must not be NULL 127cdf0e10cSrcweir 128cdf0e10cSrcweir @throws std::bad_alloc if the necessary buffer could not be 129cdf0e10cSrcweir allocated 130*0ba0bc4aSmseidel ZipException if a zip error occurs 131cdf0e10cSrcweir ZipContentMissException if the specified zip content 132cdf0e10cSrcweir does not exist in this zip file 133cdf0e10cSrcweir */ 134cdf0e10cSrcweir void GetUncompressedContent(const std::string& ContentName, /*inout*/ ZipContentBuffer_t& ContentBuffer); 135cdf0e10cSrcweir 136cdf0e10cSrcweir /** Returns a list with the content names contained within this file 137cdf0e10cSrcweir 138cdf0e10cSrcweir @throws ZipException if an error in the zlib happens 139cdf0e10cSrcweir */ 140cdf0e10cSrcweir DirectoryPtr_t GetDirectory() const; 141cdf0e10cSrcweir 14230acf5e8Spfg /** Convenience query function may even realized with 143cdf0e10cSrcweir iterating over a ZipFileDirectory returned by 144cdf0e10cSrcweir GetDirectory 145cdf0e10cSrcweir */ 146cdf0e10cSrcweir bool HasContent(const std::string& ContentName) const; 147cdf0e10cSrcweir 148cdf0e10cSrcweir private: 149cdf0e10cSrcweir 150cdf0e10cSrcweir /** Returns the length of the longest file name 151cdf0e10cSrcweir in the current zip file 152cdf0e10cSrcweir 153*0ba0bc4aSmseidel @throws ZipException if a zip error occurs 154cdf0e10cSrcweir */ 155cdf0e10cSrcweir long GetFileLongestFileNameLength() const; 156cdf0e10cSrcweir 157cdf0e10cSrcweir private: 158cdf0e10cSrcweir unzFile m_uzFile; 159cdf0e10cSrcweir }; 160cdf0e10cSrcweir 161cdf0e10cSrcweir #endif 162cdf0e10cSrcweir 163*0ba0bc4aSmseidel /* vim: set noet sw=4 ts=4: */ 164