1 /************************************************************************
\r
3 ** Copyright (C) 2010 Strahinja Markovic
\r
5 ** This file is part of FlightCrew.
\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
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
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
20 *************************************************************************/
\r
22 #include "zipios++/zipios-config.h"
\r
24 #include "zipios++/meta-iostreams.h"
\r
27 #include <boost/scoped_ptr.hpp>
\r
29 #include "zipios++/zipfile.h"
\r
30 #include "zipios++/zipinputstream.h"
\r
32 #include "zipios++/zipextraction.h"
\r
33 #include "zipios++/fcollexceptions.h"
\r
39 void WriteEntryToFile( const std::istream &stream, const fs::path &filepath )
\r
41 fs::ofstream ofs( filepath, ios::out | ios::binary );
\r
42 ofs << stream.rdbuf();
\r
47 void CreateFilepath( const fs::path &filepath )
\r
49 if ( filepath.empty() )
\r
51 throw IOException();
\r
53 if ( !fs::exists( filepath.parent_path() ) )
\r
55 fs::create_directories( filepath.parent_path() );
\r
57 if ( fs::is_regular_file( filepath ) )
\r
59 fs::ofstream( filepath );
\r
61 else if ( fs::is_directory( filepath ) )
\r
63 fs::create_directory( filepath );
\r
67 void ExtractZipToFolder( const fs::path &path_to_zip, const fs::path &path_to_folder )
\r
69 ZipFile zip( path_to_zip );
\r
71 ConstEntries entries = zip.entries();
\r
72 for ( ConstEntries::iterator it = entries.begin(); it != entries.end(); ++it )
\r
74 boost::scoped_ptr< std::istream > stream( zip.getInputStream( *it ) );
\r
76 fs::path new_file_path = path_to_folder / (*it)->getName();
\r
78 CreateFilepath( new_file_path );
\r
79 WriteEntryToFile( *stream, new_file_path );
\r
84 } // namespace zipios
\r