xref: /trunk/main/shell/inc/internal/zipfile.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef ZIPFILE_HXX_INCLUDED
29 #define ZIPFILE_HXX_INCLUDED
30 
31 #ifndef _WINDOWS
32 #define _WINDOWS
33 #endif
34 
35 
36 #include <external/zlib/unzip.h>
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     /** Convinience 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