unzipfile.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003/2006 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_UNZIPFILE_H
31 #define TNT_UNZIPFILE_H
32 
33 #include <stdexcept>
34 #include <iostream>
35 #include <string>
36 
37 namespace tnt
38 {
39  class unzipError : public std::runtime_error
40  {
41  int err;
42 
43  static std::string formatMsg(int e, const char* msg, const char* function);
44 
45  public:
46  unzipError(int e, const std::string& msg = "unzipError")
47  : std::runtime_error(msg),
48  err(e)
49  { }
50  unzipError(int e, const char* msg, const char* function)
51  : std::runtime_error(formatMsg(e, msg, function)),
52  err(e)
53  { }
54 
55  int getErr() const { return err; }
56  };
57 
59  {
60  public:
61  unzipFileNotFound(const std::string& file)
62  : unzipError(0, "file not found " + file)
63  { }
64  };
65 
67  {
68  public:
69  unzipEndOfListOfFile(const char* function = 0);
70  };
71 
72  class unzipParamError : public unzipError
73  {
74  public:
75  unzipParamError(const char* function = 0);
76  };
77 
78  class unzipBadZipFile : public unzipError
79  {
80  public:
81  unzipBadZipFile(const char* function = 0);
82  };
83 
85  {
86  public:
87  unzipInternalError(const char* function = 0);
88  };
89 
90  class unzipCrcError : public unzipError
91  {
92  public:
93  unzipCrcError(const char* function = 0);
94  };
95 
96  class unzipFile
97  {
98  struct unzFileStruct;
99  unzFileStruct* file;
100 
101  public:
103  : file(0)
104  { }
105 
106  unzipFile(const std::string& path)
107  : file(0)
108  { open(path); }
109 
110  ~unzipFile();
111 
112  void open(const std::string& path);
113  void close();
114 
115  void goToFirstFile();
116  void goToNextFile();
117  void locateFile(const std::string& fileName, bool caseSensitivity = true);
118  void openCurrentFile();
119  void openCurrentFile(const std::string& pw);
120  void closeCurrentFile();
121  int readCurrentFile(void* buf, unsigned len);
122  };
123 
124  class unzipFileStreamBuf : public std::streambuf
125  {
126  char_type buffer[512];
127  unzipFile& file;
128 
129  public:
130  unzipFileStreamBuf(unzipFile& f, const std::string& fileName, bool caseSensitivity)
131  : file(f)
132  {
133  f.locateFile(fileName, caseSensitivity);
134  f.openCurrentFile();
135  }
136 
137  unzipFileStreamBuf(unzipFile& f, const std::string& fileName, bool caseSensitivity,
138  const std::string& password)
139  : file(f)
140  {
141  f.locateFile(fileName, caseSensitivity);
142  f.openCurrentFile(password);
143  }
144 
146  { file.closeCurrentFile(); }
147 
149  int_type overflow(int_type c);
151  int_type underflow();
153  int sync();
154  };
155 
156  class unzipFileStream : public std::istream
157  {
158  unzipFileStreamBuf streambuf;
159 
160  public:
161  unzipFileStream(unzipFile& f, const std::string& fileName, bool caseSensitivity = true)
162  : std::istream(0),
163  streambuf(f, fileName, caseSensitivity)
164  { init(&streambuf); }
165  };
166 
167 }
168 
169 #endif // UNZIPFILE_H
170