Exemplo n.º 1
0
bool ZipArchive::extract(boost::filesystem::path from, boost::filesystem::path where, std::vector<std::string> what)
{
	unzFile archive = unzOpen2_64(from.c_str(), FileStream::GetMinizipFilefunc());

	auto onExit = vstd::makeScopeGuard([&]()
	{
		unzClose(archive);
	});

	for (const std::string & file : what)
	{
		if (unzLocateFile(archive, file.c_str(), 1) != UNZ_OK)
			return false;

		const boost::filesystem::path fullName = where / file;
		const boost::filesystem::path fullPath = fullName.parent_path();

		boost::filesystem::create_directories(fullPath);
		// directory. No file to extract
		// TODO: better way to detect directory? Probably check return value of unzOpenCurrentFile?
		if (boost::algorithm::ends_with(file, "/"))
			continue;

		FileStream destFile(fullName, std::ios::out | std::ios::binary);
		if (!destFile.good())
			return false;

		if (!extractCurrent(archive, destFile))
			return false;
	}
	return true;
}
Exemplo n.º 2
0
bool ZipArchive::extract(std::string from, std::string where, std::vector<std::string> what)
{
	unzFile archive = unzOpen(from.c_str());

	auto onExit = vstd::makeScopeGuard([&]()
	{
		unzClose(archive);
	});

	for (std::string & file : what)
	{
		if (unzLocateFile(archive, file.c_str(), 1) != UNZ_OK)
			return false;

		std::string fullName = where + '/' + file;
		std::string fullPath = fullName.substr(0, fullName.find_last_of("/"));

		boost::filesystem::create_directories(fullPath);
		// directory. No file to extract
		// TODO: better way to detect directory? Probably check return value of unzOpenCurrentFile?
		if (boost::algorithm::ends_with(file, "/"))
			continue;

		std::ofstream destFile(fullName, std::ofstream::binary);
		if (!destFile.good())
			return false;

		if (!extractCurrent(archive, destFile))
			return false;
	}
	return true;
}