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 #include <external/zlib/unzip.h> 33 34 35 #include <string> 36 #include <vector> 37 #include <memory> 38 39 /** A simple zip content provider based on the zlib 40 */ 41 42 class ZipFile 43 { 44 public: 45 46 typedef std::vector<std::string> Directory_t; 47 typedef std::auto_ptr<Directory_t> DirectoryPtr_t; 48 typedef std::vector<char> ZipContentBuffer_t; 49 50 public: 51 52 /** Checks whether a file is a zip file or not 53 54 @precond The given parameter must be a string with length > 0 55 The file must exist 56 The file must be readable for the current user 57 58 @returns true if the file is a zip file 59 false if the file is not a zip file 60 61 @throws ParameterException if the given file name is empty 62 IOException if the specified file doesn't exist 63 AccessViolationException if read access to the file is denied 64 */ 65 static bool IsZipFile(const std::string& FileName); 66 67 static bool IsZipFile(void* stream); 68 69 70 /** Returns wheter the version of the specified zip file may be uncompressed with the 71 currently used zlib version or not 72 73 @precond The given parameter must be a string with length > 0 74 The file must exist 75 The file must be readable for the current user 76 The file must be a valid zip file 77 78 @returns true if the file may be uncompressed with the currently used zlib 79 false if the file may not be uncompressed with the currently used zlib 80 81 @throws ParameterException if the given file name is empty 82 IOException if the specified file doesn't exist or is no zip file 83 AccessViolationException if read access to the file is denied 84 */ 85 static bool IsValidZipFileVersionNumber(const std::string& FileName); 86 87 static bool IsValidZipFileVersionNumber(void* stream); 88 89 public: 90 91 /** Constructs a zip file from a zip file 92 93 @precond The given parameter must be a string with length > 0 94 The file must exist 95 The file must be readable for the current user 96 97 @throws ParameterException if the given file name is empty 98 IOException if the specified file doesn't exist or is no valid zip file 99 AccessViolationException if read access to the file is denied 100 WrongZipVersionException if the zip file cannot be uncompressed 101 with the used zlib version 102 */ 103 ZipFile(const std::string& FileName); 104 105 ZipFile(void* stream, zlib_filefunc_def* fa); 106 107 108 /** Destroys a zip file 109 */ 110 ~ZipFile(); 111 112 /** Provides an interface to read the uncompressed data of a content of the zip file 113 114 @param ContentName 115 The name of the content in the zip file 116 117 @param ppstm 118 Pointer to pointer, will receive an interface pointer 119 to IUnknown on success 120 121 @precond The specified content must exist in this file 122 ppstm must not be NULL 123 124 @throws std::bad_alloc if the necessary buffer could not be 125 allocated 126 ZipException if an zip error occurs 127 ZipContentMissException if the specified zip content 128 does not exist in this zip file 129 */ 130 void GetUncompressedContent(const std::string& ContentName, /*inout*/ ZipContentBuffer_t& ContentBuffer); 131 132 /** Returns a list with the content names contained within this file 133 134 @throws ZipException if an error in the zlib happens 135 */ 136 DirectoryPtr_t GetDirectory() const; 137 138 /** Convinience query function may even realized with 139 iterating over a ZipFileDirectory returned by 140 GetDirectory 141 */ 142 bool HasContent(const std::string& ContentName) const; 143 144 private: 145 146 /** Returns the length of the longest file name 147 in the current zip file 148 149 @throws ZipException if an zip error occurs 150 */ 151 long GetFileLongestFileNameLength() const; 152 153 private: 154 unzFile m_uzFile; 155 }; 156 157 #endif 158 159