2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "idatastream.h"
24 #include "bytestreamutils.h"
26 #include "modulesystem.h"
30 #include "stream/filestream.h"
31 #include "container/array.h"
32 #include "archivelib.h"
33 #include "zlibstream.h"
35 class DeflatedArchiveFile : public ArchiveFile
38 FileInputStream m_istream;
39 SubFileInputStream m_substream;
40 DeflatedInputStream m_zipstream;
41 FileInputStream::size_type m_size;
43 typedef FileInputStream::size_type size_type;
44 typedef FileInputStream::position_type position_type;
46 DeflatedArchiveFile(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
47 : m_name(name), m_istream(archiveName), m_substream(m_istream, position, stream_size), m_zipstream(m_substream), m_size(file_size)
55 size_type size() const
59 const char* getName() const
61 return m_name.c_str();
63 InputStream& getInputStream()
69 class DeflatedArchiveTextFile : public ArchiveTextFile
72 FileInputStream m_istream;
73 SubFileInputStream m_substream;
74 DeflatedInputStream m_zipstream;
75 BinaryToTextInputStream<DeflatedInputStream> m_textStream;
77 typedef FileInputStream::size_type size_type;
78 typedef FileInputStream::position_type position_type;
80 DeflatedArchiveTextFile(const char* name, const char* archiveName, position_type position, size_type stream_size)
81 : m_name(name), m_istream(archiveName), m_substream(m_istream, position, stream_size), m_zipstream(m_substream), m_textStream(m_zipstream)
89 TextInputStream& getInputStream()
98 #include "string/string.h"
99 #include "fs_filesystem.h"
102 class ZipArchive : public Archive
107 enum ECompressionMode
112 ZipRecord(unsigned int position, unsigned int compressed_size, unsigned int uncompressed_size, ECompressionMode mode)
113 : m_position(position), m_stream_size(compressed_size), m_file_size(uncompressed_size), m_mode(mode)
116 unsigned int m_position;
117 unsigned int m_stream_size;
118 unsigned int m_file_size;
119 ECompressionMode m_mode;
122 typedef GenericFileSystem<ZipRecord> ZipFileSystem;
123 ZipFileSystem m_filesystem;
125 FileInputStream m_istream;
130 istream_read_zip_magic(m_istream, magic);
131 if(!(magic == zip_root_dirent_magic))
135 zip_version version_encoder;
136 istream_read_zip_version(m_istream, version_encoder);
137 zip_version version_extract;
138 istream_read_zip_version(m_istream, version_extract);
139 //unsigned short flags =
140 istream_read_int16_le(m_istream);
141 unsigned short compression_mode = istream_read_int16_le(m_istream);
142 if(compression_mode != Z_DEFLATED && compression_mode != 0)
147 istream_read_zip_dostime(m_istream, dostime);
148 //unsigned int crc32 =
149 istream_read_int32_le(m_istream);
150 unsigned int compressed_size = istream_read_uint32_le(m_istream);
151 unsigned int uncompressed_size = istream_read_uint32_le(m_istream);
152 unsigned int namelength = istream_read_uint16_le(m_istream);
153 unsigned short extras = istream_read_uint16_le(m_istream);
154 unsigned short comment = istream_read_uint16_le(m_istream);
155 //unsigned short diskstart =
156 istream_read_int16_le(m_istream);
157 //unsigned short filetype =
158 istream_read_int16_le(m_istream);
159 //unsigned int filemode =
160 istream_read_int32_le(m_istream);
161 unsigned int position = istream_read_int32_le(m_istream);
163 Array<char> filename(namelength+1);
164 m_istream.read(reinterpret_cast<FileInputStream::byte_type*>(filename.data()), namelength);
165 filename[namelength] = '\0';
167 m_istream.seek(extras + comment, FileInputStream::cur);
169 if(path_is_directory(filename.data()))
171 m_filesystem[filename.data()] = 0;
175 ZipFileSystem::entry_type& file = m_filesystem[filename.data()];
176 if(!file.is_directory())
178 globalOutputStream() << "Warning: zip archive " << makeQuoted(m_name.c_str()) << " contains duplicated file: " << makeQuoted(filename.data()) << "\n";
182 file = new ZipRecord(position, compressed_size, uncompressed_size, (compression_mode == Z_DEFLATED) ? ZipRecord::eDeflated : ZipRecord::eStored);
191 SeekableStream::position_type pos = pkzip_find_disk_trailer(m_istream);
194 zip_disk_trailer disk_trailer;
196 istream_read_zip_disk_trailer(m_istream, disk_trailer);
197 if(!(disk_trailer.z_magic == zip_disk_trailer_magic))
202 m_istream.seek(disk_trailer.z_rootseek);
203 for(unsigned int i = 0; i < disk_trailer.z_entries; ++i)
215 ZipArchive(const char* name)
216 : m_name(name), m_istream(name)
218 if(!m_istream.failed())
222 globalErrorStream() << "ERROR: invalid zip-file " << makeQuoted(name) << '\n';
228 for(ZipFileSystem::iterator i = m_filesystem.begin(); i != m_filesystem.end(); ++i)
230 delete i->second.file();
236 return m_istream.failed();
243 ArchiveFile* openFile(const char* name)
245 ZipFileSystem::iterator i = m_filesystem.find(name);
246 if(i != m_filesystem.end() && !i->second.is_directory())
248 ZipRecord* file = i->second.file();
250 m_istream.seek(file->m_position);
251 zip_file_header file_header;
252 istream_read_zip_file_header(m_istream, file_header);
253 if(file_header.z_magic != zip_file_header_magic)
255 globalErrorStream() << "error reading zip file " << makeQuoted(m_name.c_str());
261 case ZipRecord::eStored:
262 return StoredArchiveFile::create(name, m_name.c_str(), m_istream.tell(), file->m_stream_size, file->m_file_size);
263 case ZipRecord::eDeflated:
264 return new DeflatedArchiveFile(name, m_name.c_str(), m_istream.tell(), file->m_stream_size, file->m_file_size);
269 ArchiveTextFile* openTextFile(const char* name)
271 ZipFileSystem::iterator i = m_filesystem.find(name);
272 if(i != m_filesystem.end() && !i->second.is_directory())
274 ZipRecord* file = i->second.file();
276 m_istream.seek(file->m_position);
277 zip_file_header file_header;
278 istream_read_zip_file_header(m_istream, file_header);
279 if(file_header.z_magic != zip_file_header_magic)
281 globalErrorStream() << "error reading zip file " << makeQuoted(m_name.c_str());
287 case ZipRecord::eStored:
288 return StoredArchiveTextFile::create(name, m_name.c_str(), m_istream.tell(), file->m_stream_size);
289 case ZipRecord::eDeflated:
290 return new DeflatedArchiveTextFile(name, m_name.c_str(), m_istream.tell(), file->m_stream_size);
295 bool containsFile(const char* name)
297 ZipFileSystem::iterator i = m_filesystem.find(name);
298 return i != m_filesystem.end() && !i->second.is_directory();
300 void forEachFile(VisitorFunc visitor, const char* root)
302 m_filesystem.traverse(visitor, root);
306 Archive* OpenArchive(const char* name)
308 return new ZipArchive(name);
315 class TestVisitor : public Archive::IVisitor
318 void visit(const char* name)
326 testzip("c:/quake3/baseq3/mapmedia.pk3", "textures/radiant/notex.tga");
329 void testzip(const char* name, const char* filename)
331 Archive* archive = OpenArchive(name);
332 ArchiveFile* file = archive->openFile(filename);
335 unsigned char buffer[4096];
336 std::size_t count = file->getInputStream().read((InputStream::byte_type*)buffer, 4096);
340 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 0), "");
341 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "");
342 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 1), "");
343 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eDirectories, 1), "");
344 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures");
345 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures/");
346 archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "");