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