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