]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/zipextraction.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / zipextraction.cpp
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #include "zipios++/zipios-config.h"\r
23 \r
24 #include "zipios++/meta-iostreams.h"\r
25 #include <memory>\r
26 #include <stdlib.h>\r
27 #include <boost/scoped_ptr.hpp>\r
28 \r
29 #include "zipios++/zipfile.h"\r
30 #include "zipios++/zipinputstream.h"\r
31 \r
32 #include "zipios++/zipextraction.h"\r
33 #include "zipios++/fcollexceptions.h"\r
34 \r
35 namespace zipios \r
36 {\r
37 \r
38 \r
39 void WriteEntryToFile( const std::istream &stream, const fs::path &filepath )\r
40 {\r
41     fs::ofstream ofs( filepath, ios::out | ios::binary );\r
42     ofs << stream.rdbuf();\r
43     ofs.close();\r
44 }\r
45 \r
46 \r
47 void CreateFilepath( const fs::path &filepath )\r
48 {\r
49     if ( filepath.empty() )\r
50 \r
51         throw IOException();\r
52 \r
53     if ( !fs::exists( filepath.parent_path() ) )\r
54 \r
55         fs::create_directories( filepath.parent_path() );\r
56 \r
57     if ( fs::is_regular_file( filepath ) )\r
58     \r
59         fs::ofstream( filepath );\r
60 \r
61     else if ( fs::is_directory( filepath ) )\r
62 \r
63         fs::create_directory( filepath );\r
64 }\r
65 \r
66 \r
67 void ExtractZipToFolder( const fs::path &path_to_zip, const fs::path &path_to_folder )\r
68 {\r
69     ZipFile zip( path_to_zip );\r
70 \r
71     ConstEntries entries = zip.entries();\r
72     for ( ConstEntries::iterator it = entries.begin(); it != entries.end(); ++it )\r
73     {\r
74         boost::scoped_ptr< std::istream > stream( zip.getInputStream( *it ) );\r
75 \r
76         fs::path new_file_path = path_to_folder / (*it)->getName();\r
77 \r
78         CreateFilepath( new_file_path );\r
79         WriteEntryToFile( *stream, new_file_path );\r
80     }\r
81 }\r
82 \r
83 \r
84 } // namespace zipios\r