Пример #1
0
static void on_zip_downloaded(_unused_ void* userdata, void* buffer, int size)
{
	log_info("Done.");
	mz_zip_archive za;
	if (!mz_zip_reader_init_mem(&za, buffer, size, 0)) {
		log_error("Cannot unzip game files: invalid archive");
		return;
	}

	for (mz_uint i = 0; i < mz_zip_reader_get_num_files(&za); i++) {
		mz_zip_archive_file_stat file_stat;
		if (!mz_zip_reader_file_stat(&za, i, &file_stat)) {
			log_error("Cannot unzip game files");
			break;
		}
		const char* filename = file_stat.m_filename;

		int r = mkdir_p(filename);
		if (r < 0) {
			log_error("Cannot unzip game files: %s", strerror(-r));
			break;
		}
		if (!mz_zip_reader_is_file_a_directory(&za, i)) {
			mz_zip_reader_extract_to_file(&za, i, filename, 0);
		}
	}
	mz_zip_reader_end(&za);
	engine_load();
}
Пример #2
0
static int Lzip_read_string(lua_State *L) {
    size_t len;
    const char *s = luaL_checklstring(L, 1, &len);
    mz_uint32 flags = (mz_uint32)luaL_optinteger(L, 2, 0);
    mz_zip_archive *za = lua_newuserdata(L, sizeof(mz_zip_archive));
    mz_zip_zero_struct(za);
    if (!mz_zip_reader_init_mem(za, s, len, flags))
        return lmz_zip_pusherror(L, za, NULL);
    luaL_setmetatable(L, LMZ_ZIP_READER);
    lua_pushvalue(L, 1);
    lua_rawsetp(L, LUA_REGISTRYINDEX, za);
    return 1;
}
Пример #3
0
	// open from memory
	bool ZipIO::openArchive(const void *Memory, size_t Size) {
		// first close last opened archive
		_kaname = "";
		closeArchive();

		// inite object
		memset(&_kzarchive, 0, sizeof(_kzarchive));

		// open archive
		mz_bool status = mz_zip_reader_init_mem(&_kzarchive, Memory, Size, 0);
		if (!status) return false;

		_kaname = "memory-block";
		_kisopen = true;
		return true;
	}
Пример #4
0
CZipArchive::CZipArchive(FileReader &file) : ArchiveBase(file)
//------------------------------------------------------------
{
	zipFile = new mz_zip_archive();
	
	mz_zip_archive *zip = static_cast<mz_zip_archive*>(zipFile);
	
	MemsetZero(*zip);
	if(!mz_zip_reader_init_mem(zip, file.GetRawData(), file.GetLength(), 0))
	{
		delete zip;
		zip = nullptr;
		zipFile = nullptr;
	}

	if(!zip)
	{
		return;
	}

	for(mz_uint i = 0; i < mz_zip_reader_get_num_files(zip); ++i)
	{
		ArchiveFileInfo info;
		info.type = ArchiveFileInvalid;
		mz_zip_archive_file_stat stat;
		MemsetZero(stat);
		if(mz_zip_reader_file_stat(zip, i, &stat))
		{
			info.type = ArchiveFileNormal;
			info.name = mpt::PathString::FromWide(mpt::ToWide(mpt::CharsetCP437, stat.m_filename));
			info.size = stat.m_uncomp_size;
		}
		if(mz_zip_reader_is_file_a_directory(zip, i))
		{
			info.type = ArchiveFileSpecial;
		} else if(mz_zip_reader_is_file_encrypted(zip, i))
		{
			info.type = ArchiveFileSpecial;
		}
		contents.push_back(info);
	}

}
Пример #5
0
bool decompress_archive( const char* data, std::size_t size, const std::string& directory ) {
	auto zip_archive = sys::create_zeroed<mz_zip_archive>();

	if( !mz_zip_reader_init_mem( &zip_archive, data, size, 0 ) ) {
		std::cout << "mz_zip_reader_init_mem() failed!\n";
		return false;
	}

	for( unsigned int i = 0; i < mz_zip_reader_get_num_files( &zip_archive ); i++ ) {
		mz_zip_archive_file_stat file_stat;

		if( !mz_zip_reader_file_stat( &zip_archive, i, &file_stat ) ) {
			std::cout << "mz_zip_reader_file_stat() failed!\n";
			mz_zip_reader_end( &zip_archive );
			return false;
		}

		if( !mz_zip_reader_is_file_a_directory( &zip_archive, i ) ) {
			auto filename = directory + ( directory.empty() ? "" : "/" ) + file_stat.m_filename;

			if( !sys::create_directory_if_required( filename ) ) {
				std::cout << "decompress_archive could not create directory.\n";
				return false;
			}

			if( !mz_zip_reader_extract_to_file( &zip_archive, i, filename.c_str(), 0 ) ) {
				std::cout << "mz_zip_reader_extract_to_file() failed!\n";
				mz_zip_reader_end( &zip_archive );
				return false;
			}
		}
	}

	if( !mz_zip_reader_end( &zip_archive ) ) {
		std::cout << "mz_zip_reader_end() failed!\n";
		return false;
	}

	return true;
}
Пример #6
0
bool ZipArchive::loadFromMemory(std::vector<char> compressedArchiveData) {
    // Reset to an empty state.
    reset();
    // Initialize the buffer and archive with the input data.
    buffer.swap(compressedArchiveData);
    if (!mz_zip_reader_init_mem(&minizData, buffer.data(), buffer.size(), 0)) {
        return false;
    }
    // Scan the archive entries into a list.
    auto numberOfFiles = mz_zip_reader_get_num_files(&minizData);
    entryList.reserve(numberOfFiles);
    for (size_t i = 0; i < numberOfFiles; i++) {
        Entry entry;
        mz_zip_archive_file_stat stats;
        if (mz_zip_reader_file_stat(&minizData, i, &stats)) {
            entry.path = stats.m_filename;
            entry.uncompressedSize = stats.m_uncomp_size;
        }
        entryList.push_back(entry);
    }
    return true;
}
Пример #7
0
    bool pak::bin( const std::string &bin_import ) //const
    {
        this->clear();
        std::vector<pakfile> result;

        if( !bin_import.size() )
            return true; // :)

        if( type == paktype::ZIP )
        {
            // Try to open the archive.
            mz_zip_archive zip_archive;
            memset(&zip_archive, 0, sizeof(zip_archive));

            mz_bool status = mz_zip_reader_init_mem( &zip_archive, (void *)bin_import.c_str(), bin_import.size(), 0 );

            if( !status )
                return "mz_zip_reader_init_file() failed!", false;

            // Get and print information about each file in the archive.
            for( unsigned int i = 0; i < mz_zip_reader_get_num_files(&zip_archive); i++ )
            {
                mz_zip_archive_file_stat file_stat;

                if( !mz_zip_reader_file_stat( &zip_archive, i, &file_stat ) )
                {
                    mz_zip_reader_end( &zip_archive );
                    return "mz_zip_reader_file_stat() failed!", false;
                }

                result.push_back( pakfile() );

                result.back()["filename"] = file_stat.m_filename;
                result.back()["comment"] = file_stat.m_comment;
                result.back()["size"] = (unsigned int)file_stat.m_uncomp_size;
                result.back()["size_z"] = (unsigned int)file_stat.m_comp_size;
                //result.back()["modify_time"] = ze.mtime;
                //result.back()["access_time"] = ze.atime;
                //result.back()["create_time"] = ze.ctime;
                //result.back()["attributes"] = ze.attr;

                if( const bool decode = true )
                {
                    // Try to extract file to the heap.
                    size_t uncomp_size;

                    void *p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncomp_size, 0);

                    if( !p )
                    {
                        mz_zip_reader_end(&zip_archive);
                        return "mz_zip_reader_extract_file_to_heap() failed!", false;
                    }

                    // Make sure the extraction really succeeded.
                    /*
                    if ((uncomp_size != strlen(s_pStr)) || (memcmp(p, s_pStr, strlen(s_pStr))))
                    {
                    free(p);
                    mz_zip_reader_end(&zip_archive);
                    return "mz_zip_reader_extract_file_to_heap() failed to extract the proper data", false;
                    }
                    */

                    result.back()["content"].resize( uncomp_size );
                    memcpy( (void *)result.back()["content"].data(), p, uncomp_size );

                    free(p);
                }
            }

            // We're done.
            mz_zip_reader_end(&zip_archive);
        }
        else
        {}

        this->resize( result.size() );
        std::copy( result.begin(), result.end(), this->begin() );

        return true;
    }