1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef ZIPFILE_HXX_INCLUDED 29*cdf0e10cSrcweir #define ZIPFILE_HXX_INCLUDED 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #ifndef _WINDOWS 32*cdf0e10cSrcweir #define _WINDOWS 33*cdf0e10cSrcweir #endif 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include <external/zlib/unzip.h> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <string> 40*cdf0e10cSrcweir #include <vector> 41*cdf0e10cSrcweir #include <memory> 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /** A simple zip content provider based on the zlib 44*cdf0e10cSrcweir */ 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir class ZipFile 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir public: 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir typedef std::vector<std::string> Directory_t; 51*cdf0e10cSrcweir typedef std::auto_ptr<Directory_t> DirectoryPtr_t; 52*cdf0e10cSrcweir typedef std::vector<char> ZipContentBuffer_t; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir public: 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** Checks whether a file is a zip file or not 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 59*cdf0e10cSrcweir The file must exist 60*cdf0e10cSrcweir The file must be readable for the current user 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir @returns true if the file is a zip file 63*cdf0e10cSrcweir false if the file is not a zip file 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir @throws ParameterException if the given file name is empty 66*cdf0e10cSrcweir IOException if the specified file doesn't exist 67*cdf0e10cSrcweir AccessViolationException if read access to the file is denied 68*cdf0e10cSrcweir */ 69*cdf0e10cSrcweir static bool IsZipFile(const std::string& FileName); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir static bool IsZipFile(void* stream); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir /** Returns wheter the version of the specified zip file may be uncompressed with the 75*cdf0e10cSrcweir currently used zlib version or not 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 78*cdf0e10cSrcweir The file must exist 79*cdf0e10cSrcweir The file must be readable for the current user 80*cdf0e10cSrcweir The file must be a valid zip file 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir @returns true if the file may be uncompressed with the currently used zlib 83*cdf0e10cSrcweir false if the file may not be uncompressed with the currently used zlib 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir @throws ParameterException if the given file name is empty 86*cdf0e10cSrcweir IOException if the specified file doesn't exist or is no zip file 87*cdf0e10cSrcweir AccessViolationException if read access to the file is denied 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir static bool IsValidZipFileVersionNumber(const std::string& FileName); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir static bool IsValidZipFileVersionNumber(void* stream); 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir public: 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir /** Constructs a zip file from a zip file 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir @precond The given parameter must be a string with length > 0 98*cdf0e10cSrcweir The file must exist 99*cdf0e10cSrcweir The file must be readable for the current user 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir @throws ParameterException if the given file name is empty 102*cdf0e10cSrcweir IOException if the specified file doesn't exist or is no valid zip file 103*cdf0e10cSrcweir AccessViolationException if read access to the file is denied 104*cdf0e10cSrcweir WrongZipVersionException if the zip file cannot be uncompressed 105*cdf0e10cSrcweir with the used zlib version 106*cdf0e10cSrcweir */ 107*cdf0e10cSrcweir ZipFile(const std::string& FileName); 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir ZipFile(void* stream, zlib_filefunc_def* fa); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir /** Destroys a zip file 113*cdf0e10cSrcweir */ 114*cdf0e10cSrcweir ~ZipFile(); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /** Provides an interface to read the uncompressed data of a content of the zip file 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir @param ContentName 119*cdf0e10cSrcweir The name of the content in the zip file 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir @param ppstm 122*cdf0e10cSrcweir Pointer to pointer, will receive an interface pointer 123*cdf0e10cSrcweir to IUnknown on success 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir @precond The specified content must exist in this file 126*cdf0e10cSrcweir ppstm must not be NULL 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir @throws std::bad_alloc if the necessary buffer could not be 129*cdf0e10cSrcweir allocated 130*cdf0e10cSrcweir ZipException if an zip error occurs 131*cdf0e10cSrcweir ZipContentMissException if the specified zip content 132*cdf0e10cSrcweir does not exist in this zip file 133*cdf0e10cSrcweir */ 134*cdf0e10cSrcweir void GetUncompressedContent(const std::string& ContentName, /*inout*/ ZipContentBuffer_t& ContentBuffer); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir /** Returns a list with the content names contained within this file 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir @throws ZipException if an error in the zlib happens 139*cdf0e10cSrcweir */ 140*cdf0e10cSrcweir DirectoryPtr_t GetDirectory() const; 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir /** Convinience query function may even realized with 143*cdf0e10cSrcweir iterating over a ZipFileDirectory returned by 144*cdf0e10cSrcweir GetDirectory 145*cdf0e10cSrcweir */ 146*cdf0e10cSrcweir bool HasContent(const std::string& ContentName) const; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir private: 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir /** Returns the length of the longest file name 151*cdf0e10cSrcweir in the current zip file 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir @throws ZipException if an zip error occurs 154*cdf0e10cSrcweir */ 155*cdf0e10cSrcweir long GetFileLongestFileNameLength() const; 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir private: 158*cdf0e10cSrcweir unzFile m_uzFile; 159*cdf0e10cSrcweir }; 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir #endif 162*cdf0e10cSrcweir 163