{"id":19794492,"url":"https://github.com/wyanassert/wywebresource","last_synced_at":"2026-02-09T13:32:38.361Z","repository":{"id":56927197,"uuid":"115305294","full_name":"wyanassert/WYWebResource","owner":"wyanassert","description":"Download and unzip resource from web server.","archived":false,"fork":false,"pushed_at":"2019-06-27T07:48:15.000Z","size":1784,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-26T15:54:54.583Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wyanassert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-25T03:58:53.000Z","updated_at":"2022-03-27T07:24:14.000Z","dependencies_parsed_at":"2022-08-21T05:50:54.375Z","dependency_job_id":null,"html_url":"https://github.com/wyanassert/WYWebResource","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wyanassert%2FWYWebResource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wyanassert%2FWYWebResource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wyanassert%2FWYWebResource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wyanassert%2FWYWebResource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wyanassert","download_url":"https://codeload.github.com/wyanassert/WYWebResource/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251812320,"owners_count":21647885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T07:13:25.833Z","updated_at":"2026-02-09T13:32:38.330Z","avatar_url":"https://github.com/wyanassert.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WYWebResource\nDownload and unzip resource from web server.\n\n下载一个zip资源包, 解压缩, 对压缩包中的特定资源建立磁盘缓存. 有如下优点:\n\n1. 避免反复调用导致重复下载.\n\n2. 支持多个任务并发与线程安全.\n\n3. 多个资源包中有同一个资源时只会保留一份该资源. 删除资源包时, 该资源包包含的资源也会被一起删除, 避免浪费磁盘空间(如果该资源被某个未被删除的资源包包含的话, 则不会被删除).\n\n\n---\n\n## 引入方法\n\t1. 手动导入\n\t将Core文件夹重命名为WYWebResource, 再拖到工程中, 添加`AFNetworking`和`SSZipArchive`的引用\n\n\t2. Pod\n\n\tpod 'WYWebResource'\n\n---\n\n## 使用方法\n\n1. 资源包的结构\n\n  * 资源包应为一系列文件全选并右键压缩, 请不要放到一个文件夹再对文件夹进行压缩(并没有做这方面的错误处理).\n  * 资源包中要有一个`index.json`的文件, 其他的资源也直接放在同一目录下(暂时不支持文件夹嵌套寻找资源).\n  * 同种资源的文件名记录在`index.json`中, 资源包的结构与index.json的一个示例如下所示.\n\n  **资源包目录**\n\n\t```\n\tindex.json\n\tblend.jpg       \n\tblend_light2.jpg\n\tblend_light1.jpg\n\tblend_light3.jpg\n\t```\n\n  **index.json**\n\n\t\t```\n\t\t{\n\t  \t\"id\": \"3000\",\n\t  \t\"type\": \"overlay\",\n\t  \t\"overlay\": [\n\t    \t\"blend.jpg\",\n\t   \t \t\"blend_light1.jpg\",\n\t    \t\"blend_light2.jpg\",\n\t    \t\"blend_light3.jpg\"\n\t  \t\t]\n\t\t}\n\t\t```\n\n\t`overlay`中记录了四张图片, 这四张图片在资源包的根目录, 此模块会将这四张图片放在一个`overlay`目录下, 并且将`index.json`作下处理并返回, 处理之后的字典如下所示(`index.json`中只有`overlay`的value会被处理, 还要注意不要存储这个地址, 这个地址是会变动的).\n\n\t```\n\t{\n    \"id\" = \"3000\";\n    \"type\" = \"overlay\";\n   \t\"overlay\" =     {\n        \"blend.jpg\": \"/var/mobile/Containers/Data/Application/4BD2BE89-EFAA-4D56-90B2-EE25FEFB7FB3/Library/WYWebResource/overlay/blend.jpg\",\n        \"blend_light1.jpg\" : \"/var/mobile/Containers/Data/Application/4BD2BE89-EFAA-4D56-90B2-EE25FEFB7FB3/Library/WYWebResource/overlay/blend_light1.jpg\",\n        \"blend_light2.jpg\" : \"/var/mobile/Containers/Data/Application/4BD2BE89-EFAA-4D56-90B2-EE25FEFB7FB3/Library/WYWebResource/overlay/blend_light2.jpg\",\n        \"blend_light3.jpg\" : \"/var/mobile/Containers/Data/Application/4BD2BE89-EFAA-4D56-90B2-EE25FEFB7FB3/Library/WYWebResource/overlay/blend_light3.jpg\"\n    };\n    }\n\n\t```\n\n2. 代码引用\n\t* 设定哪些资源需要处理\n\t\t`[WYWebResourceManager sharedManager].cacheTypeList = @[@\"overlay\", @\"image\", @\"filter\", @\"font\"];`\n\t\t`overlay ` `image ` `filter ` `font ` 就是index.json中需要处理的资源的key名.\n\t\t不设置默认的话是如上四种类型.\n\t* 从远程下载 先下载然后解压缩并处理资源\n\n\t```\n\t[[WYWebResourceManager sharedManager] requestWYWebResource: @\"remote URL\"\n                                                      progress:^(NSProgress *progress, NSURL *targetURL) {\n\n                                                      }\n                                                    completion:^(NSDictionary * resourceInfo, NSError *error, NSURL *url) {\n                                                        NSLog(@\"\\nresource:\\n%@, \\nerror:%@, \\nurl:%@\", resourceInfo, error, url);\n                                                    }];\n\n\t```\n\n\t* 从MainBundle加载\n\n\t```\n\tNSString *bundlePath = [[NSBundle mainBundle] pathForResource:@\"Test\" ofType:@\"bundle\"];\n    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];\n    NSString *resourcePath = [bundle pathForResource:@\"3000\" ofType:@\"zip\"];\n\n    [[WYWebResourceManager sharedManager] requestWYWebResource:[NSURL fileURLWithPath:resourcePath]\n                                                      progress:nil\n                                                    completion:^(NSDictionary * _Nullable resourceInfo, NSError * _Nonnull error, NSURL * _Nonnull url) {\n                                                        NSLog(@\"resource:%@, \\nerror:%@, \\nurl:%@\", resourceInfo, error, url);\n                                                    }];\n\t```\n\n\t* 需要解压密码\n\n\t```\n\t[[WYWebResourceManager sharedManager] requestWYWebResource:[NSURL URLWithString:@\"https://firebasestorage.googleapis.com/v0/b/wydemo-93c17.appspot.com/o/zip%2F111.zip?alt=media\u0026token=ca883d44-46e5-4ca7-9f5b-11521b63d293\"] zipPw:@\"111\" progress:nil completion:^(NSDictionary * _Nullable resourceInfo, NSError * _Nonnull error, NSURL * _Nonnull url) {\n\n    }];\n\t```\n\n**注意 demo中的资源放在Google的Firebase上, 所以,,,**\n\n---\n## 代码结构\n\n### WYWebResourceManager\n1. 初始化\n\n\tpath是调用者预设的存储路径,(若该路径不存在, 则会使用模块默认的路径), 会在下面生成若干个目录来存放, 资源包(从服务器下载), 字体, 滤镜, 遮罩, 资源索引等资源, 每个文件夹只放一类资源.\n\n\t```\n\t- (instance)initWithStorePath:(NSString *)path folder:(NSString *)name;\n\t```\n\n2. 设定需要处理的资源类型\n\n\t参数是一个string数组, 每一项表示一个需要处理的资源类型, 比如相片,视频,遮罩等, 此模块会读取压缩包中的`index.json`, 将这些资源放到Cache目录下并返回该资源的地址. 不在这个数组中的数据不会被处理.\n\t```\n\t@property (nonatomic, strong) NSArray\u003cNSString *\u003e *cacheTypeList;\n\t```\n\n2. 请求资源\n\n\turl是访问资源的唯一标志, 会先检测是否有资源的资料, 没有的话去服务器下载, 并解压, 为资源建立索引. 如果找到本地有相关的资源, 则会直接把需要的字体,遮罩等资源的url传出去, 若是这些资源并不全, 则解压源资源包, 更新索引, 并将文件传出去. 若资源包不存在, 则重新从服务器下载, 并更新资源.\n\n\t```\n\t- (void)requestWYWebResourceWithResourceId:(NSURL *)url progress:(BLOCK)progressBlock comloetion:(BLOCK)completionBlock;\n\t```\n\n2. 取消一次资源的下载\n\n\t取消下载请求.\n\n\t```\n\t- (void)cancelRequestForResource:(NSURL *)url;\n\t```\n\n3. 取消当前所有的下载\n\n\t```\n\t- (void)cancelAllRequest;\n\t```\n\n3. 某个资源包是否可用\n\n\t检查资源包各个子文件是否存在, 若不存在, 检查资源包是否存在\n\n\t```\n\t- (BOOL)isResourceAvailable:(NSURL *)url;\n\t```\n\n4. 某个具体的资源是否可用\n\n\t在索引中找资源是否存在, 找到索引, 根据所以找文件是否存在与该目录.\n\n\t```\n\t- (BOOL)isSubResourceAvailable:(SubResourceType)type subResourceName:(NSString *)resourceName;\n\t```\n\n5. 清除某个资源包的缓存\n\n\t删除该资源包, 并根据索引确认是否删除资源文件,\n\n\t```\n\t- (void)deleteCache:(NSURL *)url;\n\t```\n\n6. 清除所有的缓存, 谨慎使用\n\n\t```\n\t- (void)deleteAllCache;\n\t```\n\n---\n### WYWebResourceDownloader\n\n1. 把资源包下载到指定位置, 并调用Cache存储子资源, 建立索引.\n\n\t```\n\t- (void)requestWYWebResourceWithResourceId:(NSURL *)url comloetion:(BLOCK)block;\n\t```\n\n2. 取消一次资源的下载操作\n\n\t```\n\t- (void)cancelRequestForResource:(NSURL *)url;\n\t```\n\n3. 取消所有当前下载\n\n\t```\n\t- (void)cancelAllRequest;\n\t```\n\n### WYWebResourceDownloadOperation\n\n1. 每次调用生成一次回调的Token返回, 避免同时下载多个请求\n\n\t```\n\t- (Token)addHandlersForProgress:(BLOCK)progressBlock completed:(BLOCK)completedBlock;\n\t```\n4. 取消一次下载\n\n\t```\n\t - (void)cancel:(Token)token;\n\t```\n\n2. \\- (void)start;\n3. \\- (void)cancel;\n\n---\n\n### WYWebResourceCache\n\n1. 下载解压\n\t根据资源包解压资源并将子资源放到相应位置, 应该单独开一个线程写文件, 并保持同步操作\n\n\t```\n\t- (void)storeData:(NSURL *)url path:(NSURL *)resourcePath extractDirectory:(NSURL *) extractDir completion:(BLOCK)block;\n\t```\n2. 删除特定的资源, 可能是资源包, 也可能是某个子资源\n\n\t```\n\t- (void)deleteResourceWithPath:(NSURL *)resourcePath;\n\t```\n\n---\n### WYWebResourceIndex\n\n1. 下载解压完成之后, 建立索引, 索引应该用url作为主键, 包含资源包的地址,\n\n\t```\n\t- (void)addResource:(NSURL *)url path:(NSURL *)pathUrl subResources:(NSDictonary\u003cSubResourceType, NSURL *\u003e *)subResources;\n\t```\n2. 删除一个资源包的索引\n\n\t```\n\t- (void)deleteResource:(NSURL *)url;\n\t```\n3. 根据资源包名字获取地址\n\n\t```\n\t- (NSURL *)getResourcePath:(NSURL *)url;\n\t```\n4. 获取子资源的地址\n\n\t```\n\t- (NSURL *)getSubResource:(SubResourceType)type WithName:(NSString *)resourceName;\n\t```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwyanassert%2Fwywebresource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwyanassert%2Fwywebresource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwyanassert%2Fwywebresource/lists"}