Esempio n. 1
0
void generator()
{
	int i;
	int numFile;
	int numTags;
	int tagsInserted;
	char file[256];
	char subfolder[256];
	char *tag;
	char *insertedTags[GENERATOR_NUM_TAGS];

	//Init insertedTags DB
	for ( i = 0; i<GENERATOR_NUM_TAGS; i++)
	{
		insertedTags[i]=NULL;
	}

	createFolderIfNotExists(GENERATOR_FOLDER);

	for ( numTags=100; numTags <= GENERATOR_NUM_TAGS; numTags+=100)
	{
		sprintf(subfolder, "%s/%d", GENERATOR_FOLDER, numTags);
		createFolderIfNotExists(subfolder);

		for ( numFile = 0; numFile < GENERATOR_NUM_TAGS; numFile++) 
		{
			tagsInserted = 0;

			while (tagsInserted != numTags)
			{
				//fprintf(stderr, "%d-%d\n", numTags,tagsInserted);
				tag = createTag();

				if (!hasTag(insertedTags, tag))
				{
					insertedTags[tagsInserted++] = tag;
				}
				else
				{
					free(tag);
				}
			}
			sprintf(file, "%s/%04d.txt", subfolder, numFile+1);
			writeTagsOnFile(insertedTags, numTags, file);

			for ( i = 0; i<GENERATOR_NUM_TAGS && (insertedTags[i]!= NULL); i++)
			{
				free(insertedTags[i]);
				insertedTags[i]=NULL;
			}

		}
	}
}
Esempio n. 2
0
bool Unzip::extractAllFilesTo(const std::string & path, const bool & overwriteExistingFile)
{
	std::string dest_path = path;
	bool extraction_ok = true;

	if(! isFolder(dest_path)){
		dest_path += "/";
	}

	int current = 1;
	std::unordered_map<std::string, std::shared_ptr<InnerZipFileInfo> >::const_iterator iter;
	for(iter = fileInfos.cbegin(); iter != fileInfos.cend(); ++iter){
		const std::string & fileName = iter->first;

		if(isFile(fileName)){
			bool ok = extractFileTo_Internal(fileName,
					                         dest_path + fileName,
					                         fileInfos.size(), current++,
					                         overwriteExistingFile);
			if(!ok){
				extraction_ok = false;
			}
		} else {
			if(createFolderIfNotExists(dest_path + fileName) == false){
				extraction_ok = false;
			}
		}
	}

	return extraction_ok;
}
Esempio n. 3
0
bool Unzip::extractFileTo_Internal(
		const std::string & fileName,
		const std::string & path,
		int max,
		int current,
		const bool & overwriteExistingFile)
{
	if(! containsFile(fileName)){
		return false;
	}

	if(!overwriteExistingFile && doesFileExistOnFileSystem(path)){
		return false;
	}

	bool extraction_ok = true;

	try{
		std::string destinationPath = path;
		beforeFileExtraction(destinationPath);

		boost::filesystem::path p(destinationPath);
		if(createFolderIfNotExists(p.parent_path().string()) == false){
			extraction_ok = false;
		}

		//locate filefileContent
		if(! goToFile(fileName)){
			return false;
		}

		//open file
		if(UNZ_OK != unzOpenCurrentFile3(zipfile_handle, NULL, NULL, 0, formatPassword(this->password))){
			return false;
		}

		//destination
		boost::filesystem::ofstream ofs(p, std::ios::out | std::ios::binary);

		//copy the content
		unsigned char buffer[CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE];

		unsigned int len = 0;
		while((len = unzReadCurrentFile(
				zipfile_handle,
				buffer,
				CPPZIP_UNZIP_CHAR_ARRAY_BUFFER_SIZE))
		){
			ofs.write((const char *)buffer, len);
		}

		//close file
		if(UNZ_OK != unzCloseCurrentFile(zipfile_handle)){
			return false;
		}

		ofs.flush();

		if(ofs.fail()){
			extraction_ok = false;
		}
		ofs.close();

		fileExtracted(destinationPath, max, current);
	} catch(...){
		return false;
	}

	return extraction_ok;
}