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