PageRenderTime 22ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/src/IO_ZipArchiver/ZipArchiver.cpp

http://github.com/Akranar/daguerreo
C++ | 57 lines | 48 code | 9 blank | 0 comment | 3 complexity | 7affb8cffbc57dddb3023e835923559e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
 1#include "ZipArchiver.h"
 2#include "ZipExplorer.h"
 3#include <Core_Exception.h>
 4#include <fstream>
 5
 6
 7
 8void ZipArchiver::BuildDirectory(const char * filename)
 9{
10	ZipExplorer zip_explorer;
11	zip_explorer.Open(filename);
12	zip_filename = filename;
13	zip_explorer.SeekToFirstFile();
14	char filename_buffer[1000];
15	
16	do
17	{
18		ZipFileInfo current_file;
19		current_file.location_in_zip = 
20			zip_explorer.CurrentReadDataParamsOnly(
21				current_file.size_compressed, 
22				current_file.size_uncompressed, 
23				current_file.compression_method);
24
25		zip_explorer.CurrentReadFilenameOnly(filename_buffer, 1000);
26
27		zip_directory.insert(std::make_pair(filename_buffer, current_file));
28
29	} while (zip_explorer.NextFile());
30	zip_explorer.Close();
31}
32unsigned int ZipArchiver::ReadFileData(const char * filename, void * dest, unsigned int dest_length)
33{
34	std::map<std::string, ZipFileInfo>::iterator search_result = zip_directory.find(filename);
35	CONDITIONAL_EXCEPTION(search_result == zip_directory.end(), "File not found in Zip File Directory");
36
37	unsigned int data_location = search_result->second.location_in_zip;
38	unsigned int compressed_size = search_result->second.size_compressed;
39	unsigned int data_to_read = std::min<int>(dest_length, compressed_size);
40
41	std::fstream stream(zip_filename.c_str(), std::ios_base::binary | std::ios_base::in);
42	CONDITIONAL_EXCEPTION(stream.bad(), "Zip archive file could not be opened");
43	stream.seekg(data_location);
44	stream.read((char *) dest, data_to_read);
45	stream.close();
46	return data_to_read;
47}
48const ZipFileInfo & ZipArchiver::GetFileInfo(const char * filename) const
49{
50	std::map<std::string, ZipFileInfo>::const_iterator search_result = zip_directory.find(filename);
51	CONDITIONAL_EXCEPTION(search_result == zip_directory.end(), "File not found in Zip File Directory");
52	return search_result->second;
53}
54const char * ZipArchiver::GetArchiveFilename() const
55{
56	return zip_filename.c_str();
57}